processing_options_test.go 19 KB

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