processing_options_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package options
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/imgproxy/imgproxy/v3/config"
  12. "github.com/imgproxy/imgproxy/v3/imagetype"
  13. "github.com/imgproxy/imgproxy/v3/security"
  14. "github.com/imgproxy/imgproxy/v3/testutil"
  15. "github.com/stretchr/testify/suite"
  16. )
  17. type ProcessingOptionsTestSuite struct {
  18. testutil.LazySuite
  19. securityCfg testutil.LazyObj[*security.Config]
  20. security testutil.LazyObj[*security.Checker]
  21. config testutil.LazyObj[*Config]
  22. factory testutil.LazyObj[*Factory]
  23. }
  24. func (s *ProcessingOptionsTestSuite) SetupSuite() {
  25. s.config, _ = testutil.NewLazySuiteObj(
  26. s,
  27. func() (*Config, error) {
  28. c := NewDefaultConfig()
  29. return &c, nil
  30. },
  31. )
  32. s.securityCfg, _ = testutil.NewLazySuiteObj(
  33. s,
  34. func() (*security.Config, error) {
  35. c := security.NewDefaultConfig()
  36. return &c, nil
  37. },
  38. )
  39. s.security, _ = testutil.NewLazySuiteObj(
  40. s,
  41. func() (*security.Checker, error) {
  42. return security.New(s.securityCfg())
  43. },
  44. )
  45. s.factory, _ = testutil.NewLazySuiteObj(
  46. s,
  47. func() (*Factory, error) {
  48. return NewFactory(s.config(), s.security())
  49. },
  50. )
  51. }
  52. func (s *ProcessingOptionsTestSuite) SetupSubTest() {
  53. s.ResetLazyObjects()
  54. }
  55. func (s *ProcessingOptionsTestSuite) TestParseBase64URL() {
  56. originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  57. path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  58. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  59. s.Require().NoError(err)
  60. s.Require().Equal(originURL, imageURL)
  61. s.Require().Equal(imagetype.PNG, po.Format)
  62. }
  63. func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithFilename() {
  64. s.config().Base64URLIncludesFilename = true
  65. originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  66. path := fmt.Sprintf("/size:100:100/%s.png/puppy.jpg", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  67. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  68. s.Require().NoError(err)
  69. s.Require().Equal(originURL, imageURL)
  70. s.Require().Equal(imagetype.PNG, po.Format)
  71. }
  72. func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithoutExtension() {
  73. originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  74. path := fmt.Sprintf("/size:100:100/%s", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  75. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  76. s.Require().NoError(err)
  77. s.Require().Equal(originURL, imageURL)
  78. s.Require().Equal(imagetype.Unknown, po.Format)
  79. }
  80. func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
  81. s.config().BaseURL = "http://images.dev/"
  82. originURL := "lorem/ipsum.jpg?param=value"
  83. path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  84. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  85. s.Require().NoError(err)
  86. s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
  87. s.Require().Equal(imagetype.PNG, po.Format)
  88. }
  89. func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithReplacement() {
  90. s.config().URLReplacements = []config.URLReplacement{
  91. {Regexp: regexp.MustCompile("^test://([^/]*)/"), Replacement: "test2://images.dev/${1}/dolor/"},
  92. {Regexp: regexp.MustCompile("^test2://"), Replacement: "http://"},
  93. }
  94. originURL := "test://lorem/ipsum.jpg?param=value"
  95. path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  96. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  97. s.Require().NoError(err)
  98. s.Require().Equal("http://images.dev/lorem/dolor/ipsum.jpg?param=value", imageURL)
  99. s.Require().Equal(imagetype.PNG, po.Format)
  100. }
  101. func (s *ProcessingOptionsTestSuite) TestParsePlainURL() {
  102. originURL := "http://images.dev/lorem/ipsum.jpg"
  103. path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
  104. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  105. s.Require().NoError(err)
  106. s.Require().Equal(originURL, imageURL)
  107. s.Require().Equal(imagetype.PNG, po.Format)
  108. }
  109. func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithoutExtension() {
  110. originURL := "http://images.dev/lorem/ipsum.jpg"
  111. path := fmt.Sprintf("/size:100:100/plain/%s", originURL)
  112. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  113. s.Require().NoError(err)
  114. s.Require().Equal(originURL, imageURL)
  115. s.Require().Equal(imagetype.Unknown, po.Format)
  116. }
  117. func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscaped() {
  118. originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  119. path := fmt.Sprintf("/size:100:100/plain/%s@png", url.PathEscape(originURL))
  120. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  121. s.Require().NoError(err)
  122. s.Require().Equal(originURL, imageURL)
  123. s.Require().Equal(imagetype.PNG, po.Format)
  124. }
  125. func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithBase() {
  126. s.config().BaseURL = "http://images.dev/"
  127. originURL := "lorem/ipsum.jpg"
  128. path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
  129. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  130. s.Require().NoError(err)
  131. s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
  132. s.Require().Equal(imagetype.PNG, po.Format)
  133. }
  134. func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithReplacement() {
  135. s.config().URLReplacements = []config.URLReplacement{
  136. {Regexp: regexp.MustCompile("^test://([^/]*)/"), Replacement: "test2://images.dev/${1}/dolor/"},
  137. {Regexp: regexp.MustCompile("^test2://"), Replacement: "http://"},
  138. }
  139. originURL := "test://lorem/ipsum.jpg"
  140. path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
  141. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  142. s.Require().NoError(err)
  143. s.Require().Equal("http://images.dev/lorem/dolor/ipsum.jpg", imageURL)
  144. s.Require().Equal(imagetype.PNG, po.Format)
  145. }
  146. func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscapedWithBase() {
  147. s.config().BaseURL = "http://images.dev/"
  148. originURL := "lorem/ipsum.jpg?param=value"
  149. path := fmt.Sprintf("/size:100:100/plain/%s@png", url.PathEscape(originURL))
  150. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  151. s.Require().NoError(err)
  152. s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
  153. s.Require().Equal(imagetype.PNG, po.Format)
  154. }
  155. func (s *ProcessingOptionsTestSuite) TestParseWithArgumentsSeparator() {
  156. s.config().ArgumentsSeparator = ","
  157. path := "/size,100,100,1/plain/http://images.dev/lorem/ipsum.jpg"
  158. po, _, err := s.factory().ParsePath(path, make(http.Header))
  159. s.Require().NoError(err)
  160. s.Require().Equal(100, po.Width)
  161. s.Require().Equal(100, po.Height)
  162. s.Require().True(po.Enlarge)
  163. }
  164. func (s *ProcessingOptionsTestSuite) TestParsePathFormat() {
  165. path := "/format:webp/plain/http://images.dev/lorem/ipsum.jpg"
  166. po, _, err := s.factory().ParsePath(path, make(http.Header))
  167. s.Require().NoError(err)
  168. s.Require().Equal(imagetype.WEBP, po.Format)
  169. }
  170. func (s *ProcessingOptionsTestSuite) TestParsePathResize() {
  171. path := "/resize:fill:100:200:1/plain/http://images.dev/lorem/ipsum.jpg"
  172. po, _, err := s.factory().ParsePath(path, make(http.Header))
  173. s.Require().NoError(err)
  174. s.Require().Equal(ResizeFill, po.ResizingType)
  175. s.Require().Equal(100, po.Width)
  176. s.Require().Equal(200, po.Height)
  177. s.Require().True(po.Enlarge)
  178. }
  179. func (s *ProcessingOptionsTestSuite) TestParsePathResizingType() {
  180. path := "/resizing_type:fill/plain/http://images.dev/lorem/ipsum.jpg"
  181. po, _, err := s.factory().ParsePath(path, make(http.Header))
  182. s.Require().NoError(err)
  183. s.Require().Equal(ResizeFill, po.ResizingType)
  184. }
  185. func (s *ProcessingOptionsTestSuite) TestParsePathSize() {
  186. path := "/size:100:200:1/plain/http://images.dev/lorem/ipsum.jpg"
  187. po, _, err := s.factory().ParsePath(path, make(http.Header))
  188. s.Require().NoError(err)
  189. s.Require().Equal(100, po.Width)
  190. s.Require().Equal(200, po.Height)
  191. s.Require().True(po.Enlarge)
  192. }
  193. func (s *ProcessingOptionsTestSuite) TestParsePathWidth() {
  194. path := "/width:100/plain/http://images.dev/lorem/ipsum.jpg"
  195. po, _, err := s.factory().ParsePath(path, make(http.Header))
  196. s.Require().NoError(err)
  197. s.Require().Equal(100, po.Width)
  198. }
  199. func (s *ProcessingOptionsTestSuite) TestParsePathHeight() {
  200. path := "/height:100/plain/http://images.dev/lorem/ipsum.jpg"
  201. po, _, err := s.factory().ParsePath(path, make(http.Header))
  202. s.Require().NoError(err)
  203. s.Require().Equal(100, po.Height)
  204. }
  205. func (s *ProcessingOptionsTestSuite) TestParsePathEnlarge() {
  206. path := "/enlarge:1/plain/http://images.dev/lorem/ipsum.jpg"
  207. po, _, err := s.factory().ParsePath(path, make(http.Header))
  208. s.Require().NoError(err)
  209. s.Require().True(po.Enlarge)
  210. }
  211. func (s *ProcessingOptionsTestSuite) TestParsePathExtend() {
  212. path := "/extend:1:so:10:20/plain/http://images.dev/lorem/ipsum.jpg"
  213. po, _, err := s.factory().ParsePath(path, make(http.Header))
  214. s.Require().NoError(err)
  215. s.Require().True(po.Extend.Enabled)
  216. s.Require().Equal(GravitySouth, po.Extend.Gravity.Type)
  217. s.Require().InDelta(10.0, po.Extend.Gravity.X, 0.0001)
  218. s.Require().InDelta(20.0, po.Extend.Gravity.Y, 0.0001)
  219. }
  220. func (s *ProcessingOptionsTestSuite) TestParsePathExtendSmartGravity() {
  221. path := "/extend:1:sm/plain/http://images.dev/lorem/ipsum.jpg"
  222. _, _, err := s.factory().ParsePath(path, make(http.Header))
  223. s.Require().Error(err)
  224. }
  225. func (s *ProcessingOptionsTestSuite) TestParsePathExtendReplicateGravity() {
  226. path := "/extend:1:re/plain/http://images.dev/lorem/ipsum.jpg"
  227. _, _, err := s.factory().ParsePath(path, make(http.Header))
  228. s.Require().Error(err)
  229. }
  230. func (s *ProcessingOptionsTestSuite) TestParsePathGravity() {
  231. path := "/gravity:soea/plain/http://images.dev/lorem/ipsum.jpg"
  232. po, _, err := s.factory().ParsePath(path, make(http.Header))
  233. s.Require().NoError(err)
  234. s.Require().Equal(GravitySouthEast, po.Gravity.Type)
  235. }
  236. func (s *ProcessingOptionsTestSuite) TestParsePathGravityFocusPoint() {
  237. path := "/gravity:fp:0.5:0.75/plain/http://images.dev/lorem/ipsum.jpg"
  238. po, _, err := s.factory().ParsePath(path, make(http.Header))
  239. s.Require().NoError(err)
  240. s.Require().Equal(GravityFocusPoint, po.Gravity.Type)
  241. s.Require().InDelta(0.5, po.Gravity.X, 0.0001)
  242. s.Require().InDelta(0.75, po.Gravity.Y, 0.0001)
  243. }
  244. func (s *ProcessingOptionsTestSuite) TestParsePathGravityReplicate() {
  245. path := "/gravity:re/plain/http://images.dev/lorem/ipsum.jpg"
  246. _, _, err := s.factory().ParsePath(path, make(http.Header))
  247. s.Require().Error(err)
  248. }
  249. func (s *ProcessingOptionsTestSuite) TestParsePathCrop() {
  250. path := "/crop:100:200/plain/http://images.dev/lorem/ipsum.jpg"
  251. po, _, err := s.factory().ParsePath(path, make(http.Header))
  252. s.Require().NoError(err)
  253. s.Require().InDelta(100.0, po.Crop.Width, 0.0001)
  254. s.Require().InDelta(200.0, po.Crop.Height, 0.0001)
  255. s.Require().Equal(GravityUnknown, po.Crop.Gravity.Type)
  256. s.Require().InDelta(0.0, po.Crop.Gravity.X, 0.0001)
  257. s.Require().InDelta(0.0, po.Crop.Gravity.Y, 0.0001)
  258. }
  259. func (s *ProcessingOptionsTestSuite) TestParsePathCropGravity() {
  260. path := "/crop:100:200:nowe:10:20/plain/http://images.dev/lorem/ipsum.jpg"
  261. po, _, err := s.factory().ParsePath(path, make(http.Header))
  262. s.Require().NoError(err)
  263. s.Require().InDelta(100.0, po.Crop.Width, 0.0001)
  264. s.Require().InDelta(200.0, po.Crop.Height, 0.0001)
  265. s.Require().Equal(GravityNorthWest, po.Crop.Gravity.Type)
  266. s.Require().InDelta(10.0, po.Crop.Gravity.X, 0.0001)
  267. s.Require().InDelta(20.0, po.Crop.Gravity.Y, 0.0001)
  268. }
  269. func (s *ProcessingOptionsTestSuite) TestParsePathCropGravityReplicate() {
  270. path := "/crop:100:200:re/plain/http://images.dev/lorem/ipsum.jpg"
  271. _, _, err := s.factory().ParsePath(path, make(http.Header))
  272. s.Require().Error(err)
  273. }
  274. func (s *ProcessingOptionsTestSuite) TestParsePathQuality() {
  275. path := "/quality:55/plain/http://images.dev/lorem/ipsum.jpg"
  276. po, _, err := s.factory().ParsePath(path, make(http.Header))
  277. s.Require().NoError(err)
  278. s.Require().Equal(55, po.Quality)
  279. }
  280. func (s *ProcessingOptionsTestSuite) TestParsePathBackground() {
  281. path := "/background:128:129:130/plain/http://images.dev/lorem/ipsum.jpg"
  282. po, _, err := s.factory().ParsePath(path, make(http.Header))
  283. s.Require().NoError(err)
  284. s.Require().True(po.Flatten)
  285. s.Require().Equal(uint8(128), po.Background.R)
  286. s.Require().Equal(uint8(129), po.Background.G)
  287. s.Require().Equal(uint8(130), po.Background.B)
  288. }
  289. func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundHex() {
  290. path := "/background:ffddee/plain/http://images.dev/lorem/ipsum.jpg"
  291. po, _, err := s.factory().ParsePath(path, make(http.Header))
  292. s.Require().NoError(err)
  293. s.Require().True(po.Flatten)
  294. s.Require().Equal(uint8(0xff), po.Background.R)
  295. s.Require().Equal(uint8(0xdd), po.Background.G)
  296. s.Require().Equal(uint8(0xee), po.Background.B)
  297. }
  298. func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundDisable() {
  299. path := "/background:fff/background:/plain/http://images.dev/lorem/ipsum.jpg"
  300. po, _, err := s.factory().ParsePath(path, make(http.Header))
  301. s.Require().NoError(err)
  302. s.Require().False(po.Flatten)
  303. }
  304. func (s *ProcessingOptionsTestSuite) TestParsePathBlur() {
  305. path := "/blur:0.2/plain/http://images.dev/lorem/ipsum.jpg"
  306. po, _, err := s.factory().ParsePath(path, make(http.Header))
  307. s.Require().NoError(err)
  308. s.Require().InDelta(float32(0.2), po.Blur, 0.0001)
  309. }
  310. func (s *ProcessingOptionsTestSuite) TestParsePathSharpen() {
  311. path := "/sharpen:0.2/plain/http://images.dev/lorem/ipsum.jpg"
  312. po, _, err := s.factory().ParsePath(path, make(http.Header))
  313. s.Require().NoError(err)
  314. s.Require().InDelta(float32(0.2), po.Sharpen, 0.0001)
  315. }
  316. func (s *ProcessingOptionsTestSuite) TestParsePathDpr() {
  317. path := "/dpr:2/plain/http://images.dev/lorem/ipsum.jpg"
  318. po, _, err := s.factory().ParsePath(path, make(http.Header))
  319. s.Require().NoError(err)
  320. s.Require().InDelta(2.0, po.Dpr, 0.0001)
  321. }
  322. func (s *ProcessingOptionsTestSuite) TestParsePathWatermark() {
  323. path := "/watermark:0.5:soea:10:20:0.6/plain/http://images.dev/lorem/ipsum.jpg"
  324. po, _, err := s.factory().ParsePath(path, make(http.Header))
  325. s.Require().NoError(err)
  326. s.Require().True(po.Watermark.Enabled)
  327. s.Require().Equal(GravitySouthEast, po.Watermark.Position.Type)
  328. s.Require().InDelta(10.0, po.Watermark.Position.X, 0.0001)
  329. s.Require().InDelta(20.0, po.Watermark.Position.Y, 0.0001)
  330. s.Require().InDelta(0.6, po.Watermark.Scale, 0.0001)
  331. }
  332. func (s *ProcessingOptionsTestSuite) TestParsePathPreset() {
  333. s.config().Presets = []string{
  334. "test1=resizing_type:fill",
  335. "test2=blur:0.2/quality:50",
  336. }
  337. path := "/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg"
  338. po, _, err := s.factory().ParsePath(path, make(http.Header))
  339. s.Require().NoError(err)
  340. s.Require().Equal(ResizeFill, po.ResizingType)
  341. s.Require().InDelta(float32(0.2), po.Blur, 0.0001)
  342. s.Require().Equal(50, po.Quality)
  343. }
  344. func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
  345. s.config().Presets = []string{
  346. "default=resizing_type:fill/blur:0.2/quality:50",
  347. }
  348. path := "/quality:70/plain/http://images.dev/lorem/ipsum.jpg"
  349. po, _, err := s.factory().ParsePath(path, make(http.Header))
  350. s.Require().NoError(err)
  351. s.Require().Equal(ResizeFill, po.ResizingType)
  352. s.Require().InDelta(float32(0.2), po.Blur, 0.0001)
  353. s.Require().Equal(70, po.Quality)
  354. }
  355. func (s *ProcessingOptionsTestSuite) TestParsePathPresetLoopDetection() {
  356. s.config().Presets = []string{
  357. "test1=resizing_type:fill/preset:test2",
  358. "test2=blur:0.2/preset:test1",
  359. }
  360. path := "/preset:test1/plain/http://images.dev/lorem/ipsum.jpg"
  361. po, _, err := s.factory().ParsePath(path, make(http.Header))
  362. s.Require().NoError(err)
  363. s.Require().ElementsMatch(po.UsedPresets, []string{"test1", "test2"})
  364. }
  365. func (s *ProcessingOptionsTestSuite) TestParsePathCachebuster() {
  366. path := "/cachebuster:123/plain/http://images.dev/lorem/ipsum.jpg"
  367. po, _, err := s.factory().ParsePath(path, make(http.Header))
  368. s.Require().NoError(err)
  369. s.Require().Equal("123", po.CacheBuster)
  370. }
  371. func (s *ProcessingOptionsTestSuite) TestParsePathStripMetadata() {
  372. path := "/strip_metadata:true/plain/http://images.dev/lorem/ipsum.jpg"
  373. po, _, err := s.factory().ParsePath(path, make(http.Header))
  374. s.Require().NoError(err)
  375. s.Require().True(po.StripMetadata)
  376. }
  377. func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
  378. s.config().AutoWebp = true
  379. path := "/plain/http://images.dev/lorem/ipsum.jpg"
  380. headers := http.Header{"Accept": []string{"image/webp"}}
  381. po, _, err := s.factory().ParsePath(path, headers)
  382. s.Require().NoError(err)
  383. s.Require().True(po.PreferWebP)
  384. s.Require().False(po.EnforceWebP)
  385. }
  386. func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
  387. s.config().EnforceWebp = true
  388. path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
  389. headers := http.Header{"Accept": []string{"image/webp"}}
  390. po, _, err := s.factory().ParsePath(path, headers)
  391. s.Require().NoError(err)
  392. s.Require().True(po.PreferWebP)
  393. s.Require().True(po.EnforceWebP)
  394. }
  395. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
  396. s.config().EnableClientHints = true
  397. path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
  398. headers := http.Header{"Width": []string{"100"}}
  399. po, _, err := s.factory().ParsePath(path, headers)
  400. s.Require().NoError(err)
  401. s.Require().Equal(100, po.Width)
  402. }
  403. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
  404. path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
  405. headers := http.Header{"Width": []string{"100"}}
  406. po, _, err := s.factory().ParsePath(path, headers)
  407. s.Require().NoError(err)
  408. s.Require().Equal(0, po.Width)
  409. }
  410. func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
  411. s.config().EnableClientHints = true
  412. path := "/width:150/plain/http://images.dev/lorem/ipsum.jpg@png"
  413. headers := http.Header{"Width": []string{"100"}}
  414. po, _, err := s.factory().ParsePath(path, headers)
  415. s.Require().NoError(err)
  416. s.Require().Equal(150, po.Width)
  417. }
  418. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
  419. s.config().EnableClientHints = true
  420. path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
  421. headers := http.Header{"Dpr": []string{"2"}}
  422. po, _, err := s.factory().ParsePath(path, headers)
  423. s.Require().NoError(err)
  424. s.Require().InDelta(2.0, po.Dpr, 0.0001)
  425. }
  426. func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
  427. path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
  428. headers := http.Header{"Dpr": []string{"2"}}
  429. po, _, err := s.factory().ParsePath(path, headers)
  430. s.Require().NoError(err)
  431. s.Require().InDelta(1.0, po.Dpr, 0.0001)
  432. }
  433. func (s *ProcessingOptionsTestSuite) TestParseSkipProcessing() {
  434. path := "/skp:jpg:png/plain/http://images.dev/lorem/ipsum.jpg"
  435. po, _, err := s.factory().ParsePath(path, make(http.Header))
  436. s.Require().NoError(err)
  437. s.Require().Equal([]imagetype.Type{imagetype.JPEG, imagetype.PNG}, po.SkipProcessingFormats)
  438. }
  439. func (s *ProcessingOptionsTestSuite) TestParseSkipProcessingInvalid() {
  440. path := "/skp:jpg:png:bad_format/plain/http://images.dev/lorem/ipsum.jpg"
  441. _, _, err := s.factory().ParsePath(path, make(http.Header))
  442. s.Require().Error(err)
  443. s.Require().Equal("Invalid image format in skip processing: bad_format", err.Error())
  444. }
  445. func (s *ProcessingOptionsTestSuite) TestParseExpires() {
  446. path := "/exp:32503669200/plain/http://images.dev/lorem/ipsum.jpg"
  447. _, _, err := s.factory().ParsePath(path, make(http.Header))
  448. s.Require().NoError(err)
  449. }
  450. func (s *ProcessingOptionsTestSuite) TestParseExpiresExpired() {
  451. path := "/exp:1609448400/plain/http://images.dev/lorem/ipsum.jpg"
  452. _, _, err := s.factory().ParsePath(path, make(http.Header))
  453. s.Require().Error(err, "Expired URL")
  454. }
  455. func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
  456. s.config().OnlyPresets = true
  457. s.config().Presets = []string{
  458. "test1=blur:0.2",
  459. "test2=quality:50",
  460. }
  461. path := "/test1:test2/plain/http://images.dev/lorem/ipsum.jpg"
  462. po, _, err := s.factory().ParsePath(path, make(http.Header))
  463. s.Require().NoError(err)
  464. s.Require().InDelta(float32(0.2), po.Blur, 0.0001)
  465. s.Require().Equal(50, po.Quality)
  466. }
  467. func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
  468. s.config().OnlyPresets = true
  469. s.config().Presets = []string{
  470. "test1=blur:0.2",
  471. "test2=quality:50",
  472. }
  473. originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  474. path := fmt.Sprintf("/test1:test2/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  475. po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
  476. s.Require().NoError(err)
  477. s.Require().InDelta(float32(0.2), po.Blur, 0.0001)
  478. s.Require().Equal(50, po.Quality)
  479. s.Require().Equal(originURL, imageURL)
  480. }
  481. func (s *ProcessingOptionsTestSuite) TestParseAllowedOptions() {
  482. originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  483. testCases := []struct {
  484. options string
  485. expectedError string
  486. }{
  487. {options: "w:100/h:200", expectedError: ""},
  488. {options: "w:100/h:200/blur:10", expectedError: "Forbidden processing option blur"},
  489. {options: "w:100/h:200/pr:test1", expectedError: ""},
  490. {options: "w:100/h:200/pr:test1/blur:10", expectedError: "Forbidden processing option blur"},
  491. }
  492. for _, tc := range testCases {
  493. s.Run(strings.ReplaceAll(tc.options, "/", "_"), func() {
  494. s.config().AllowedProcessingOptions = []string{"w", "h", "pr"}
  495. s.config().Presets = []string{
  496. "test1=blur:0.2",
  497. }
  498. path := fmt.Sprintf("/%s/%s.png", tc.options, base64.RawURLEncoding.EncodeToString([]byte(originURL)))
  499. _, _, err := s.factory().ParsePath(path, make(http.Header))
  500. if len(tc.expectedError) > 0 {
  501. s.Require().Error(err)
  502. s.Require().Equal(tc.expectedError, err.Error())
  503. } else {
  504. s.Require().NoError(err)
  505. }
  506. })
  507. }
  508. }
  509. func (s *ProcessingOptionsTestSuite) TestProcessingOptionsClone() {
  510. now := time.Now()
  511. // Create ProcessingOptions using factory
  512. original := s.factory().NewProcessingOptions()
  513. original.SkipProcessingFormats = []imagetype.Type{
  514. imagetype.PNG, imagetype.JPEG,
  515. }
  516. original.UsedPresets = []string{"preset1", "preset2"}
  517. original.Expires = &now
  518. // Clone the original
  519. cloned := original.clone()
  520. testutil.EqualButNotSame(s.T(), original, cloned)
  521. }
  522. func TestProcessingOptions(t *testing.T) {
  523. suite.Run(t, new(ProcessingOptionsTestSuite))
  524. }