processing_options_test.go 19 KB

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