processing_options_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. package main
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/stretchr/testify/suite"
  12. )
  13. type ProcessingOptionsTestSuite struct{ MainTestSuite }
  14. func (s *ProcessingOptionsTestSuite) getRequest(url string) *http.Request {
  15. req, _ := http.NewRequest("GET", url, nil)
  16. return req
  17. }
  18. func (s *ProcessingOptionsTestSuite) TestParseBase64URL() {
  19. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  20. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))))
  21. ctx, err := parsePath(context.Background(), req)
  22. require.Nil(s.T(), err)
  23. assert.Equal(s.T(), imageURL, getImageURL(ctx))
  24. assert.Equal(s.T(), imageTypePNG, getProcessingOptions(ctx).Format)
  25. }
  26. func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithoutExtension() {
  27. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  28. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/%s", base64.RawURLEncoding.EncodeToString([]byte(imageURL))))
  29. ctx, err := parsePath(context.Background(), req)
  30. require.Nil(s.T(), err)
  31. assert.Equal(s.T(), imageURL, getImageURL(ctx))
  32. assert.Equal(s.T(), imageTypeUnknown, getProcessingOptions(ctx).Format)
  33. }
  34. func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
  35. conf.BaseURL = "http://images.dev/"
  36. imageURL := "lorem/ipsum.jpg?param=value"
  37. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))))
  38. ctx, err := parsePath(context.Background(), req)
  39. require.Nil(s.T(), err)
  40. assert.Equal(s.T(), fmt.Sprintf("%s%s", conf.BaseURL, imageURL), getImageURL(ctx))
  41. assert.Equal(s.T(), imageTypePNG, getProcessingOptions(ctx).Format)
  42. }
  43. func (s *ProcessingOptionsTestSuite) TestParsePlainURL() {
  44. imageURL := "http://images.dev/lorem/ipsum.jpg"
  45. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", imageURL))
  46. ctx, err := parsePath(context.Background(), req)
  47. require.Nil(s.T(), err)
  48. assert.Equal(s.T(), imageURL, getImageURL(ctx))
  49. assert.Equal(s.T(), imageTypePNG, getProcessingOptions(ctx).Format)
  50. }
  51. func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithoutExtension() {
  52. imageURL := "http://images.dev/lorem/ipsum.jpg"
  53. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s", imageURL))
  54. ctx, err := parsePath(context.Background(), req)
  55. require.Nil(s.T(), err)
  56. assert.Equal(s.T(), imageURL, getImageURL(ctx))
  57. assert.Equal(s.T(), imageTypeUnknown, getProcessingOptions(ctx).Format)
  58. }
  59. func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscaped() {
  60. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  61. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", url.PathEscape(imageURL)))
  62. ctx, err := parsePath(context.Background(), req)
  63. require.Nil(s.T(), err)
  64. assert.Equal(s.T(), imageURL, getImageURL(ctx))
  65. assert.Equal(s.T(), imageTypePNG, getProcessingOptions(ctx).Format)
  66. }
  67. func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithBase() {
  68. conf.BaseURL = "http://images.dev/"
  69. imageURL := "lorem/ipsum.jpg"
  70. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", imageURL))
  71. ctx, err := parsePath(context.Background(), req)
  72. require.Nil(s.T(), err)
  73. assert.Equal(s.T(), fmt.Sprintf("%s%s", conf.BaseURL, imageURL), getImageURL(ctx))
  74. assert.Equal(s.T(), imageTypePNG, getProcessingOptions(ctx).Format)
  75. }
  76. func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscapedWithBase() {
  77. conf.BaseURL = "http://images.dev/"
  78. imageURL := "lorem/ipsum.jpg?param=value"
  79. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", url.PathEscape(imageURL)))
  80. ctx, err := parsePath(context.Background(), req)
  81. require.Nil(s.T(), err)
  82. assert.Equal(s.T(), fmt.Sprintf("%s%s", conf.BaseURL, imageURL), getImageURL(ctx))
  83. assert.Equal(s.T(), imageTypePNG, getProcessingOptions(ctx).Format)
  84. }
  85. func (s *ProcessingOptionsTestSuite) TestParseURLAllowedSource() {
  86. conf.AllowedSources = []string{"local://", "http://images.dev/"}
  87. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg")
  88. _, err := parsePath(context.Background(), req)
  89. require.Nil(s.T(), err)
  90. }
  91. func (s *ProcessingOptionsTestSuite) TestParseURLNotAllowedSource() {
  92. conf.AllowedSources = []string{"local://", "http://images.dev/"}
  93. req := s.getRequest("http://example.com/unsafe/plain/s3://images/lorem/ipsum.jpg")
  94. _, err := parsePath(context.Background(), req)
  95. require.Error(s.T(), err)
  96. }
  97. func (s *ProcessingOptionsTestSuite) TestParsePathBasic() {
  98. req := s.getRequest("http://example.com/unsafe/fill/100/200/noea/1/plain/http://images.dev/lorem/ipsum.jpg@png")
  99. ctx, err := parsePath(context.Background(), req)
  100. require.Nil(s.T(), err)
  101. po := getProcessingOptions(ctx)
  102. assert.Equal(s.T(), resizeFill, po.ResizingType)
  103. assert.Equal(s.T(), 100, po.Width)
  104. assert.Equal(s.T(), 200, po.Height)
  105. assert.Equal(s.T(), gravityNorthEast, po.Gravity.Type)
  106. assert.True(s.T(), po.Enlarge)
  107. assert.Equal(s.T(), imageTypePNG, po.Format)
  108. }
  109. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedFormat() {
  110. req := s.getRequest("http://example.com/unsafe/format:webp/plain/http://images.dev/lorem/ipsum.jpg")
  111. ctx, err := parsePath(context.Background(), req)
  112. require.Nil(s.T(), err)
  113. po := getProcessingOptions(ctx)
  114. assert.Equal(s.T(), imageTypeWEBP, po.Format)
  115. }
  116. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedResize() {
  117. req := s.getRequest("http://example.com/unsafe/resize:fill:100:200:1/plain/http://images.dev/lorem/ipsum.jpg")
  118. ctx, err := parsePath(context.Background(), req)
  119. require.Nil(s.T(), err)
  120. po := getProcessingOptions(ctx)
  121. assert.Equal(s.T(), resizeFill, po.ResizingType)
  122. assert.Equal(s.T(), 100, po.Width)
  123. assert.Equal(s.T(), 200, po.Height)
  124. assert.True(s.T(), po.Enlarge)
  125. }
  126. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedResizingType() {
  127. req := s.getRequest("http://example.com/unsafe/resizing_type:fill/plain/http://images.dev/lorem/ipsum.jpg")
  128. ctx, err := parsePath(context.Background(), req)
  129. require.Nil(s.T(), err)
  130. po := getProcessingOptions(ctx)
  131. assert.Equal(s.T(), resizeFill, po.ResizingType)
  132. }
  133. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedSize() {
  134. req := s.getRequest("http://example.com/unsafe/size:100:200:1/plain/http://images.dev/lorem/ipsum.jpg")
  135. ctx, err := parsePath(context.Background(), req)
  136. require.Nil(s.T(), err)
  137. po := getProcessingOptions(ctx)
  138. assert.Equal(s.T(), 100, po.Width)
  139. assert.Equal(s.T(), 200, po.Height)
  140. assert.True(s.T(), po.Enlarge)
  141. }
  142. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedWidth() {
  143. req := s.getRequest("http://example.com/unsafe/width:100/plain/http://images.dev/lorem/ipsum.jpg")
  144. ctx, err := parsePath(context.Background(), req)
  145. require.Nil(s.T(), err)
  146. po := getProcessingOptions(ctx)
  147. assert.Equal(s.T(), 100, po.Width)
  148. }
  149. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedHeight() {
  150. req := s.getRequest("http://example.com/unsafe/height:100/plain/http://images.dev/lorem/ipsum.jpg")
  151. ctx, err := parsePath(context.Background(), req)
  152. require.Nil(s.T(), err)
  153. po := getProcessingOptions(ctx)
  154. assert.Equal(s.T(), 100, po.Height)
  155. }
  156. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedEnlarge() {
  157. req := s.getRequest("http://example.com/unsafe/enlarge:1/plain/http://images.dev/lorem/ipsum.jpg")
  158. ctx, err := parsePath(context.Background(), req)
  159. require.Nil(s.T(), err)
  160. po := getProcessingOptions(ctx)
  161. assert.True(s.T(), po.Enlarge)
  162. }
  163. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedExtend() {
  164. req := s.getRequest("http://example.com/unsafe/extend:1:so:10:20/plain/http://images.dev/lorem/ipsum.jpg")
  165. ctx, err := parsePath(context.Background(), req)
  166. require.Nil(s.T(), err)
  167. po := getProcessingOptions(ctx)
  168. assert.Equal(s.T(), true, po.Extend.Enabled)
  169. assert.Equal(s.T(), gravitySouth, po.Extend.Gravity.Type)
  170. assert.Equal(s.T(), 10.0, po.Extend.Gravity.X)
  171. assert.Equal(s.T(), 20.0, po.Extend.Gravity.Y)
  172. }
  173. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedGravity() {
  174. req := s.getRequest("http://example.com/unsafe/gravity:soea/plain/http://images.dev/lorem/ipsum.jpg")
  175. ctx, err := parsePath(context.Background(), req)
  176. require.Nil(s.T(), err)
  177. po := getProcessingOptions(ctx)
  178. assert.Equal(s.T(), gravitySouthEast, po.Gravity.Type)
  179. }
  180. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedGravityFocuspoint() {
  181. req := s.getRequest("http://example.com/unsafe/gravity:fp:0.5:0.75/plain/http://images.dev/lorem/ipsum.jpg")
  182. ctx, err := parsePath(context.Background(), req)
  183. require.Nil(s.T(), err)
  184. po := getProcessingOptions(ctx)
  185. assert.Equal(s.T(), gravityFocusPoint, po.Gravity.Type)
  186. assert.Equal(s.T(), 0.5, po.Gravity.X)
  187. assert.Equal(s.T(), 0.75, po.Gravity.Y)
  188. }
  189. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedQuality() {
  190. req := s.getRequest("http://example.com/unsafe/quality:55/plain/http://images.dev/lorem/ipsum.jpg")
  191. ctx, err := parsePath(context.Background(), req)
  192. require.Nil(s.T(), err)
  193. po := getProcessingOptions(ctx)
  194. assert.Equal(s.T(), 55, po.Quality)
  195. }
  196. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBackground() {
  197. req := s.getRequest("http://example.com/unsafe/background:128:129:130/plain/http://images.dev/lorem/ipsum.jpg")
  198. ctx, err := parsePath(context.Background(), req)
  199. require.Nil(s.T(), err)
  200. po := getProcessingOptions(ctx)
  201. assert.True(s.T(), po.Flatten)
  202. assert.Equal(s.T(), uint8(128), po.Background.R)
  203. assert.Equal(s.T(), uint8(129), po.Background.G)
  204. assert.Equal(s.T(), uint8(130), po.Background.B)
  205. }
  206. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBackgroundHex() {
  207. req := s.getRequest("http://example.com/unsafe/background:ffddee/plain/http://images.dev/lorem/ipsum.jpg")
  208. ctx, err := parsePath(context.Background(), req)
  209. require.Nil(s.T(), err)
  210. po := getProcessingOptions(ctx)
  211. assert.True(s.T(), po.Flatten)
  212. assert.Equal(s.T(), uint8(0xff), po.Background.R)
  213. assert.Equal(s.T(), uint8(0xdd), po.Background.G)
  214. assert.Equal(s.T(), uint8(0xee), po.Background.B)
  215. }
  216. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBackgroundDisable() {
  217. req := s.getRequest("http://example.com/unsafe/background:fff/background:/plain/http://images.dev/lorem/ipsum.jpg")
  218. ctx, err := parsePath(context.Background(), req)
  219. require.Nil(s.T(), err)
  220. po := getProcessingOptions(ctx)
  221. assert.False(s.T(), po.Flatten)
  222. }
  223. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBlur() {
  224. req := s.getRequest("http://example.com/unsafe/blur:0.2/plain/http://images.dev/lorem/ipsum.jpg")
  225. ctx, err := parsePath(context.Background(), req)
  226. require.Nil(s.T(), err)
  227. po := getProcessingOptions(ctx)
  228. assert.Equal(s.T(), float32(0.2), po.Blur)
  229. }
  230. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedSharpen() {
  231. req := s.getRequest("http://example.com/unsafe/sharpen:0.2/plain/http://images.dev/lorem/ipsum.jpg")
  232. ctx, err := parsePath(context.Background(), req)
  233. require.Nil(s.T(), err)
  234. po := getProcessingOptions(ctx)
  235. assert.Equal(s.T(), float32(0.2), po.Sharpen)
  236. }
  237. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedDpr() {
  238. req := s.getRequest("http://example.com/unsafe/dpr:2/plain/http://images.dev/lorem/ipsum.jpg")
  239. ctx, err := parsePath(context.Background(), req)
  240. require.Nil(s.T(), err)
  241. po := getProcessingOptions(ctx)
  242. assert.Equal(s.T(), 2.0, po.Dpr)
  243. }
  244. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedWatermark() {
  245. req := s.getRequest("http://example.com/unsafe/watermark:0.5:soea:10:20:0.6/plain/http://images.dev/lorem/ipsum.jpg")
  246. ctx, err := parsePath(context.Background(), req)
  247. require.Nil(s.T(), err)
  248. po := getProcessingOptions(ctx)
  249. assert.True(s.T(), po.Watermark.Enabled)
  250. assert.Equal(s.T(), gravitySouthEast, po.Watermark.Gravity.Type)
  251. assert.Equal(s.T(), 10.0, po.Watermark.Gravity.X)
  252. assert.Equal(s.T(), 20.0, po.Watermark.Gravity.Y)
  253. assert.Equal(s.T(), 0.6, po.Watermark.Scale)
  254. }
  255. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() {
  256. conf.Presets["test1"] = urlOptions{
  257. urlOption{Name: "resizing_type", Args: []string{"fill"}},
  258. }
  259. conf.Presets["test2"] = urlOptions{
  260. urlOption{Name: "blur", Args: []string{"0.2"}},
  261. urlOption{Name: "quality", Args: []string{"50"}},
  262. }
  263. req := s.getRequest("http://example.com/unsafe/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
  264. ctx, err := parsePath(context.Background(), req)
  265. require.Nil(s.T(), err)
  266. po := getProcessingOptions(ctx)
  267. assert.Equal(s.T(), resizeFill, po.ResizingType)
  268. assert.Equal(s.T(), float32(0.2), po.Blur)
  269. assert.Equal(s.T(), 50, po.Quality)
  270. }
  271. func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
  272. conf.Presets["default"] = urlOptions{
  273. urlOption{Name: "resizing_type", Args: []string{"fill"}},
  274. urlOption{Name: "blur", Args: []string{"0.2"}},
  275. urlOption{Name: "quality", Args: []string{"50"}},
  276. }
  277. req := s.getRequest("http://example.com/unsafe/quality:70/plain/http://images.dev/lorem/ipsum.jpg")
  278. ctx, err := parsePath(context.Background(), req)
  279. require.Nil(s.T(), err)
  280. po := getProcessingOptions(ctx)
  281. assert.Equal(s.T(), resizeFill, po.ResizingType)
  282. assert.Equal(s.T(), float32(0.2), po.Blur)
  283. assert.Equal(s.T(), 70, po.Quality)
  284. }
  285. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPresetLoopDetection() {
  286. conf.Presets["test1"] = urlOptions{
  287. urlOption{Name: "resizing_type", Args: []string{"fill"}},
  288. }
  289. conf.Presets["test2"] = urlOptions{
  290. urlOption{Name: "blur", Args: []string{"0.2"}},
  291. urlOption{Name: "quality", Args: []string{"50"}},
  292. }
  293. req := s.getRequest("http://example.com/unsafe/preset:test1:test2:test1/plain/http://images.dev/lorem/ipsum.jpg")
  294. ctx, err := parsePath(context.Background(), req)
  295. require.Nil(s.T(), err)
  296. po := getProcessingOptions(ctx)
  297. require.ElementsMatch(s.T(), po.UsedPresets, []string{"test1", "test2"})
  298. }
  299. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedCachebuster() {
  300. req := s.getRequest("http://example.com/unsafe/cachebuster:123/plain/http://images.dev/lorem/ipsum.jpg")
  301. ctx, err := parsePath(context.Background(), req)
  302. require.Nil(s.T(), err)
  303. po := getProcessingOptions(ctx)
  304. assert.Equal(s.T(), "123", po.CacheBuster)
  305. }
  306. func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
  307. conf.EnableWebpDetection = true
  308. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg")
  309. req.Header.Set("Accept", "image/webp")
  310. ctx, err := parsePath(context.Background(), req)
  311. require.Nil(s.T(), err)
  312. po := getProcessingOptions(ctx)
  313. assert.Equal(s.T(), true, po.PreferWebP)
  314. assert.Equal(s.T(), false, po.EnforceWebP)
  315. }
  316. func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
  317. conf.EnforceWebp = true
  318. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  319. req.Header.Set("Accept", "image/webp")
  320. ctx, err := parsePath(context.Background(), req)
  321. require.Nil(s.T(), err)
  322. po := getProcessingOptions(ctx)
  323. assert.Equal(s.T(), true, po.PreferWebP)
  324. assert.Equal(s.T(), true, po.EnforceWebP)
  325. }
  326. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
  327. conf.EnableClientHints = true
  328. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  329. req.Header.Set("Width", "100")
  330. ctx, err := parsePath(context.Background(), req)
  331. require.Nil(s.T(), err)
  332. po := getProcessingOptions(ctx)
  333. assert.Equal(s.T(), 100, po.Width)
  334. }
  335. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
  336. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  337. req.Header.Set("Width", "100")
  338. ctx, err := parsePath(context.Background(), req)
  339. require.Nil(s.T(), err)
  340. po := getProcessingOptions(ctx)
  341. assert.Equal(s.T(), 0, po.Width)
  342. }
  343. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
  344. conf.EnableClientHints = true
  345. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  346. req.Header.Set("Width", "100")
  347. ctx, err := parsePath(context.Background(), req)
  348. require.Nil(s.T(), err)
  349. po := getProcessingOptions(ctx)
  350. assert.Equal(s.T(), 150, po.Width)
  351. }
  352. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeader() {
  353. conf.EnableClientHints = true
  354. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  355. req.Header.Set("Viewport-Width", "100")
  356. ctx, err := parsePath(context.Background(), req)
  357. require.Nil(s.T(), err)
  358. po := getProcessingOptions(ctx)
  359. assert.Equal(s.T(), 100, po.Width)
  360. }
  361. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeaderDisabled() {
  362. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  363. req.Header.Set("Viewport-Width", "100")
  364. ctx, err := parsePath(context.Background(), req)
  365. require.Nil(s.T(), err)
  366. po := getProcessingOptions(ctx)
  367. assert.Equal(s.T(), 0, po.Width)
  368. }
  369. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeaderRedefine() {
  370. conf.EnableClientHints = true
  371. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  372. req.Header.Set("Viewport-Width", "100")
  373. ctx, err := parsePath(context.Background(), req)
  374. require.Nil(s.T(), err)
  375. po := getProcessingOptions(ctx)
  376. assert.Equal(s.T(), 150, po.Width)
  377. }
  378. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
  379. conf.EnableClientHints = true
  380. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  381. req.Header.Set("DPR", "2")
  382. ctx, err := parsePath(context.Background(), req)
  383. require.Nil(s.T(), err)
  384. po := getProcessingOptions(ctx)
  385. assert.Equal(s.T(), 2.0, po.Dpr)
  386. }
  387. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
  388. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  389. req.Header.Set("DPR", "2")
  390. ctx, err := parsePath(context.Background(), req)
  391. require.Nil(s.T(), err)
  392. po := getProcessingOptions(ctx)
  393. assert.Equal(s.T(), 1.0, po.Dpr)
  394. }
  395. func (s *ProcessingOptionsTestSuite) TestParsePathSigned() {
  396. conf.Keys = []securityKey{securityKey("test-key")}
  397. conf.Salts = []securityKey{securityKey("test-salt")}
  398. conf.AllowInsecure = false
  399. req := s.getRequest("http://example.com/HcvNognEV1bW6f8zRqxNYuOkV0IUf1xloRb57CzbT4g/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  400. _, err := parsePath(context.Background(), req)
  401. require.Nil(s.T(), err)
  402. }
  403. func (s *ProcessingOptionsTestSuite) TestParsePathSignedInvalid() {
  404. conf.Keys = []securityKey{securityKey("test-key")}
  405. conf.Salts = []securityKey{securityKey("test-salt")}
  406. conf.AllowInsecure = false
  407. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  408. _, err := parsePath(context.Background(), req)
  409. require.Error(s.T(), err)
  410. assert.Equal(s.T(), errInvalidSignature.Error(), err.Error())
  411. }
  412. func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
  413. conf.OnlyPresets = true
  414. conf.Presets["test1"] = urlOptions{
  415. urlOption{Name: "blur", Args: []string{"0.2"}},
  416. }
  417. conf.Presets["test2"] = urlOptions{
  418. urlOption{Name: "quality", Args: []string{"50"}},
  419. }
  420. req := s.getRequest("http://example.com/unsafe/test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
  421. ctx, err := parsePath(context.Background(), req)
  422. require.Nil(s.T(), err)
  423. po := getProcessingOptions(ctx)
  424. assert.Equal(s.T(), float32(0.2), po.Blur)
  425. assert.Equal(s.T(), 50, po.Quality)
  426. }
  427. func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
  428. conf.OnlyPresets = true
  429. conf.Presets["test1"] = urlOptions{
  430. urlOption{Name: "blur", Args: []string{"0.2"}},
  431. }
  432. conf.Presets["test2"] = urlOptions{
  433. urlOption{Name: "quality", Args: []string{"50"}},
  434. }
  435. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  436. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/test1:test2/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))))
  437. ctx, err := parsePath(context.Background(), req)
  438. require.Nil(s.T(), err)
  439. po := getProcessingOptions(ctx)
  440. assert.Equal(s.T(), float32(0.2), po.Blur)
  441. assert.Equal(s.T(), 50, po.Quality)
  442. }
  443. func TestProcessingOptions(t *testing.T) {
  444. suite.Run(t, new(ProcessingOptionsTestSuite))
  445. }