processing_options_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. ctx := fasthttp.RequestCtx{
  16. Request: fasthttp.Request{},
  17. Response: fasthttp.Response{},
  18. }
  19. ctx.Request.SetRequestURI(url)
  20. return &ctx
  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. ctx, err := parsePath(context.Background(), req)
  298. require.Nil(s.T(), err)
  299. po := getProcessingOptions(ctx)
  300. require.ElementsMatch(s.T(), po.UsedPresets, []string{"test1", "test2"})
  301. }
  302. func (s *ProcessingOptionsTestSuite) TestParsePathAdvancedCachebuster() {
  303. req := s.getRequest("http://example.com/unsafe/cachebuster:123/plain/http://images.dev/lorem/ipsum.jpg")
  304. ctx, err := parsePath(context.Background(), req)
  305. require.Nil(s.T(), err)
  306. po := getProcessingOptions(ctx)
  307. assert.Equal(s.T(), "123", po.CacheBuster)
  308. }
  309. func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
  310. conf.EnableWebpDetection = true
  311. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg")
  312. req.Request.Header.Set("Accept", "image/webp")
  313. ctx, err := parsePath(context.Background(), req)
  314. require.Nil(s.T(), err)
  315. po := getProcessingOptions(ctx)
  316. assert.Equal(s.T(), imageTypeWEBP, po.Format)
  317. }
  318. func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetectionRedefine() {
  319. conf.EnableWebpDetection = true
  320. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  321. req.Request.Header.Set("Accept", "image/webp")
  322. ctx, err := parsePath(context.Background(), req)
  323. require.Nil(s.T(), err)
  324. po := getProcessingOptions(ctx)
  325. assert.Equal(s.T(), imageTypePNG, po.Format)
  326. }
  327. func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
  328. conf.EnforceWebp = true
  329. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  330. req.Request.Header.Set("Accept", "image/webp")
  331. ctx, err := parsePath(context.Background(), req)
  332. require.Nil(s.T(), err)
  333. po := getProcessingOptions(ctx)
  334. assert.Equal(s.T(), imageTypeWEBP, po.Format)
  335. }
  336. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
  337. conf.EnableClientHints = true
  338. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  339. req.Request.Header.Set("Width", "100")
  340. ctx, err := parsePath(context.Background(), req)
  341. require.Nil(s.T(), err)
  342. po := getProcessingOptions(ctx)
  343. assert.Equal(s.T(), 100, po.Width)
  344. }
  345. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
  346. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  347. req.Request.Header.Set("Width", "100")
  348. ctx, err := parsePath(context.Background(), req)
  349. require.Nil(s.T(), err)
  350. po := getProcessingOptions(ctx)
  351. assert.Equal(s.T(), 0, po.Width)
  352. }
  353. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
  354. conf.EnableClientHints = true
  355. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  356. req.Request.Header.Set("Width", "100")
  357. ctx, err := parsePath(context.Background(), req)
  358. require.Nil(s.T(), err)
  359. po := getProcessingOptions(ctx)
  360. assert.Equal(s.T(), 150, po.Width)
  361. }
  362. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeader() {
  363. conf.EnableClientHints = true
  364. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  365. req.Request.Header.Set("Viewport-Width", "100")
  366. ctx, err := parsePath(context.Background(), req)
  367. require.Nil(s.T(), err)
  368. po := getProcessingOptions(ctx)
  369. assert.Equal(s.T(), 100, po.Width)
  370. }
  371. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeaderDisabled() {
  372. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  373. req.Request.Header.Set("Viewport-Width", "100")
  374. ctx, err := parsePath(context.Background(), req)
  375. require.Nil(s.T(), err)
  376. po := getProcessingOptions(ctx)
  377. assert.Equal(s.T(), 0, po.Width)
  378. }
  379. func (s *ProcessingOptionsTestSuite) TestParsePathViewportWidthHeaderRedefine() {
  380. conf.EnableClientHints = true
  381. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  382. req.Request.Header.Set("Viewport-Width", "100")
  383. ctx, err := parsePath(context.Background(), req)
  384. require.Nil(s.T(), err)
  385. po := getProcessingOptions(ctx)
  386. assert.Equal(s.T(), 150, po.Width)
  387. }
  388. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
  389. conf.EnableClientHints = true
  390. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  391. req.Request.Header.Set("DPR", "2")
  392. ctx, err := parsePath(context.Background(), req)
  393. require.Nil(s.T(), err)
  394. po := getProcessingOptions(ctx)
  395. assert.Equal(s.T(), 2.0, po.Dpr)
  396. }
  397. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
  398. req := s.getRequest("http://example.com/unsafe/plain/http://images.dev/lorem/ipsum.jpg@png")
  399. req.Request.Header.Set("DPR", "2")
  400. ctx, err := parsePath(context.Background(), req)
  401. require.Nil(s.T(), err)
  402. po := getProcessingOptions(ctx)
  403. assert.Equal(s.T(), 1.0, po.Dpr)
  404. }
  405. func (s *ProcessingOptionsTestSuite) TestParsePathSigned() {
  406. conf.Keys = []securityKey{securityKey("test-key")}
  407. conf.Salts = []securityKey{securityKey("test-salt")}
  408. conf.AllowInsecure = false
  409. req := s.getRequest("http://example.com/HcvNognEV1bW6f8zRqxNYuOkV0IUf1xloRb57CzbT4g/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  410. _, err := parsePath(context.Background(), req)
  411. require.Nil(s.T(), err)
  412. }
  413. func (s *ProcessingOptionsTestSuite) TestParsePathSignedInvalid() {
  414. conf.Keys = []securityKey{securityKey("test-key")}
  415. conf.Salts = []securityKey{securityKey("test-salt")}
  416. conf.AllowInsecure = false
  417. req := s.getRequest("http://example.com/unsafe/width:150/plain/http://images.dev/lorem/ipsum.jpg@png")
  418. _, err := parsePath(context.Background(), req)
  419. require.Error(s.T(), err)
  420. assert.Equal(s.T(), errInvalidSignature.Error(), err.Error())
  421. }
  422. func TestProcessingOptions(t *testing.T) {
  423. suite.Run(t, new(ProcessingOptionsTestSuite))
  424. }