processing_options_test.go 20 KB

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