processing_options_test.go 19 KB

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