processing_options_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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) TestParsePathAdvancedGravity() {
  152. req := s.getRequest("http://example.com/unsafe/gravity:soea/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(), gravitySouthEast, po.Gravity.Type)
  157. }
  158. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedGravityFocuspoint() {
  159. req := s.getRequest("http://example.com/unsafe/gravity:fp:0.5:0.75/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(), gravityFocusPoint, po.Gravity.Type)
  164. assert.Equal(s.T(), 0.5, po.Gravity.X)
  165. assert.Equal(s.T(), 0.75, po.Gravity.Y)
  166. }
  167. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedQuality() {
  168. req := s.getRequest("http://example.com/unsafe/quality:55/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(), 55, po.Quality)
  173. }
  174. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBackground() {
  175. req := s.getRequest("http://example.com/unsafe/background:128:129:130/plain/http://images.dev/lorem/ipsum.jpg")
  176. ctx, err := parsePath(context.Background(), req)
  177. require.Nil(s.T(), err)
  178. po := getProcessingOptions(ctx)
  179. assert.True(s.T(), po.Flatten)
  180. assert.Equal(s.T(), uint8(128), po.Background.R)
  181. assert.Equal(s.T(), uint8(129), po.Background.G)
  182. assert.Equal(s.T(), uint8(130), po.Background.B)
  183. }
  184. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBackgroundHex() {
  185. req := s.getRequest("http://example.com/unsafe/background:ffddee/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(0xff), po.Background.R)
  191. assert.Equal(s.T(), uint8(0xdd), po.Background.G)
  192. assert.Equal(s.T(), uint8(0xee), po.Background.B)
  193. }
  194. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBackgroundDisable() {
  195. req := s.getRequest("http://example.com/unsafe/background:fff/background:/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.False(s.T(), po.Flatten)
  200. }
  201. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedBlur() {
  202. req := s.getRequest("http://example.com/unsafe/blur:0.2/plain/http://images.dev/lorem/ipsum.jpg")
  203. ctx, err := parsePath(context.Background(), req)
  204. require.Nil(s.T(), err)
  205. po := getProcessingOptions(ctx)
  206. assert.Equal(s.T(), float32(0.2), po.Blur)
  207. }
  208. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedSharpen() {
  209. req := s.getRequest("http://example.com/unsafe/sharpen:0.2/plain/http://images.dev/lorem/ipsum.jpg")
  210. ctx, err := parsePath(context.Background(), req)
  211. require.Nil(s.T(), err)
  212. po := getProcessingOptions(ctx)
  213. assert.Equal(s.T(), float32(0.2), po.Sharpen)
  214. }
  215. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedDpr() {
  216. req := s.getRequest("http://example.com/unsafe/dpr:2/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.Equal(s.T(), 2.0, po.Dpr)
  221. }
  222. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedWatermark() {
  223. req := s.getRequest("http://example.com/unsafe/watermark:0.5:soea:10:20:0.6/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.True(s.T(), po.Watermark.Enabled)
  228. assert.Equal(s.T(), gravitySouthEast, po.Watermark.Gravity)
  229. assert.Equal(s.T(), 10, po.Watermark.OffsetX)
  230. assert.Equal(s.T(), 20, po.Watermark.OffsetY)
  231. assert.Equal(s.T(), 0.6, po.Watermark.Scale)
  232. }
  233. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPreset() {
  234. conf.Presets["test1"] = urlOptions{
  235. urlOption{Name: "resizing_type", Args: []string{"fill"}},
  236. }
  237. conf.Presets["test2"] = urlOptions{
  238. urlOption{Name: "blur", Args: []string{"0.2"}},
  239. urlOption{Name: "quality", Args: []string{"50"}},
  240. }
  241. req := s.getRequest("http://example.com/unsafe/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
  242. ctx, err := parsePath(context.Background(), req)
  243. require.Nil(s.T(), err)
  244. po := getProcessingOptions(ctx)
  245. assert.Equal(s.T(), resizeFill, po.ResizingType)
  246. assert.Equal(s.T(), float32(0.2), po.Blur)
  247. assert.Equal(s.T(), 50, po.Quality)
  248. }
  249. func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
  250. conf.Presets["default"] = urlOptions{
  251. urlOption{Name: "resizing_type", Args: []string{"fill"}},
  252. urlOption{Name: "blur", Args: []string{"0.2"}},
  253. urlOption{Name: "quality", Args: []string{"50"}},
  254. }
  255. req := s.getRequest("http://example.com/unsafe/quality:70/plain/http://images.dev/lorem/ipsum.jpg")
  256. ctx, err := parsePath(context.Background(), req)
  257. require.Nil(s.T(), err)
  258. po := getProcessingOptions(ctx)
  259. assert.Equal(s.T(), resizeFill, po.ResizingType)
  260. assert.Equal(s.T(), float32(0.2), po.Blur)
  261. assert.Equal(s.T(), 70, po.Quality)
  262. }
  263. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedPresetLoopDetection() {
  264. conf.Presets["test1"] = urlOptions{
  265. urlOption{Name: "resizing_type", Args: []string{"fill"}},
  266. }
  267. conf.Presets["test2"] = urlOptions{
  268. urlOption{Name: "blur", Args: []string{"0.2"}},
  269. urlOption{Name: "quality", Args: []string{"50"}},
  270. }
  271. req := s.getRequest("http://example.com/unsafe/preset:test1:test2:test1/plain/http://images.dev/lorem/ipsum.jpg")
  272. ctx, err := parsePath(context.Background(), req)
  273. require.Nil(s.T(), err)
  274. po := getProcessingOptions(ctx)
  275. require.ElementsMatch(s.T(), po.UsedPresets, []string{"test1", "test2"})
  276. }
  277. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedCachebuster() {
  278. req := s.getRequest("http://example.com/unsafe/cachebuster:123/plain/http://images.dev/lorem/ipsum.jpg")
  279. ctx, err := parsePath(context.Background(), req)
  280. require.Nil(s.T(), err)
  281. po := getProcessingOptions(ctx)
  282. assert.Equal(s.T(), "123", po.CacheBuster)
  283. }
  284. func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
  285. conf.EnableWebpDetection = true
  286. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg")
  287. req.Header.Set("Accept", "image/webp")
  288. ctx, err := parsePath(context.Background(), req)
  289. require.Nil(s.T(), err)
  290. po := getProcessingOptions(ctx)
  291. assert.Equal(s.T(), true, po.PreferWebP)
  292. assert.Equal(s.T(), false, po.EnforceWebP)
  293. }
  294. func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
  295. conf.EnforceWebp = true
  296. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  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(), true, po.EnforceWebP)
  303. }
  304. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
  305. conf.EnableClientHints = true
  306. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  307. req.Header.Set("Width", "100")
  308. ctx, err := parsePath(context.Background(), req)
  309. require.Nil(s.T(), err)
  310. po := getProcessingOptions(ctx)
  311. assert.Equal(s.T(), 100, po.Width)
  312. }
  313. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
  314. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  315. req.Header.Set("Width", "100")
  316. ctx, err := parsePath(context.Background(), req)
  317. require.Nil(s.T(), err)
  318. po := getProcessingOptions(ctx)
  319. assert.Equal(s.T(), 0, po.Width)
  320. }
  321. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
  322. conf.EnableClientHints = true
  323. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  324. req.Header.Set("Width", "100")
  325. ctx, err := parsePath(context.Background(), req)
  326. require.Nil(s.T(), err)
  327. po := getProcessingOptions(ctx)
  328. assert.Equal(s.T(), 150, po.Width)
  329. }
  330. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeader() {
  331. conf.EnableClientHints = true
  332. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  333. req.Header.Set("Viewport-Width", "100")
  334. ctx, err := parsePath(context.Background(), req)
  335. require.Nil(s.T(), err)
  336. po := getProcessingOptions(ctx)
  337. assert.Equal(s.T(), 100, po.Width)
  338. }
  339. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeaderDisabled() {
  340. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  341. req.Header.Set("Viewport-Width", "100")
  342. ctx, err := parsePath(context.Background(), req)
  343. require.Nil(s.T(), err)
  344. po := getProcessingOptions(ctx)
  345. assert.Equal(s.T(), 0, po.Width)
  346. }
  347. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeaderRedefine() {
  348. conf.EnableClientHints = true
  349. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  350. req.Header.Set("Viewport-Width", "100")
  351. ctx, err := parsePath(context.Background(), req)
  352. require.Nil(s.T(), err)
  353. po := getProcessingOptions(ctx)
  354. assert.Equal(s.T(), 150, po.Width)
  355. }
  356. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
  357. conf.EnableClientHints = true
  358. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  359. req.Header.Set("DPR", "2")
  360. ctx, err := parsePath(context.Background(), req)
  361. require.Nil(s.T(), err)
  362. po := getProcessingOptions(ctx)
  363. assert.Equal(s.T(), 2.0, po.Dpr)
  364. }
  365. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
  366. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  367. req.Header.Set("DPR", "2")
  368. ctx, err := parsePath(context.Background(), req)
  369. require.Nil(s.T(), err)
  370. po := getProcessingOptions(ctx)
  371. assert.Equal(s.T(), 1.0, po.Dpr)
  372. }
  373. func (s *ProcessingOptionsTestSuite) TestParsePathSigned() {
  374. conf.Keys = []securityKey{securityKey("test-key")}
  375. conf.Salts = []securityKey{securityKey("test-salt")}
  376. conf.AllowInsecure = false
  377. req := s.getRequest("http://example.com/HcvNognEV1bW6f8zRqxNYuOkV0IUf1xloRb57CzbT4g/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  378. _, err := parsePath(context.Background(), req)
  379. require.Nil(s.T(), err)
  380. }
  381. func (s *ProcessingOptionsTestSuite) TestParsePathSignedInvalid() {
  382. conf.Keys = []securityKey{securityKey("test-key")}
  383. conf.Salts = []securityKey{securityKey("test-salt")}
  384. conf.AllowInsecure = false
  385. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  386. _, err := parsePath(context.Background(), req)
  387. require.Error(s.T(), err)
  388. assert.Equal(s.T(), errInvalidSignature.Error(), err.Error())
  389. }
  390. func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
  391. conf.OnlyPresets = true
  392. conf.Presets["test1"] = urlOptions{
  393. urlOption{Name: "blur", Args: []string{"0.2"}},
  394. }
  395. conf.Presets["test2"] = urlOptions{
  396. urlOption{Name: "quality", Args: []string{"50"}},
  397. }
  398. req := s.getRequest("http://example.com/unsafe/test1:test2/plain/http://images.dev/lorem/ipsum.jpg")
  399. ctx, err := parsePath(context.Background(), req)
  400. require.Nil(s.T(), err)
  401. po := getProcessingOptions(ctx)
  402. assert.Equal(s.T(), float32(0.2), po.Blur)
  403. assert.Equal(s.T(), 50, po.Quality)
  404. }
  405. func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
  406. conf.OnlyPresets = true
  407. conf.Presets["test1"] = urlOptions{
  408. urlOption{Name: "blur", Args: []string{"0.2"}},
  409. }
  410. conf.Presets["test2"] = urlOptions{
  411. urlOption{Name: "quality", Args: []string{"50"}},
  412. }
  413. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  414. req := s.getRequest(fmt.Sprintf("http://example.com/unsafe/test1:test2/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))))
  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 TestProcessingOptions(t *testing.T) {
  422. suite.Run(t, new(ProcessingOptionsTestSuite))
  423. }