processing_options_test.go 25 KB

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