processing_handler_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path/filepath"
  10. "regexp"
  11. "strings"
  12. "testing"
  13. "time"
  14. "github.com/sirupsen/logrus"
  15. "github.com/stretchr/testify/suite"
  16. "github.com/imgproxy/imgproxy/v3/config"
  17. "github.com/imgproxy/imgproxy/v3/config/configurators"
  18. "github.com/imgproxy/imgproxy/v3/etag"
  19. "github.com/imgproxy/imgproxy/v3/imagedata"
  20. "github.com/imgproxy/imgproxy/v3/imagemeta"
  21. "github.com/imgproxy/imgproxy/v3/imagetype"
  22. "github.com/imgproxy/imgproxy/v3/options"
  23. "github.com/imgproxy/imgproxy/v3/router"
  24. "github.com/imgproxy/imgproxy/v3/svg"
  25. "github.com/imgproxy/imgproxy/v3/vips"
  26. )
  27. type ProcessingHandlerTestSuite struct {
  28. suite.Suite
  29. router *router.Router
  30. }
  31. func (s *ProcessingHandlerTestSuite) SetupSuite() {
  32. config.Reset()
  33. wd, err := os.Getwd()
  34. s.Require().NoError(err)
  35. s.T().Setenv("IMGPROXY_LOCAL_FILESYSTEM_ROOT", filepath.Join(wd, "/testdata"))
  36. s.T().Setenv("IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT", "0")
  37. err = initialize()
  38. s.Require().NoError(err)
  39. logrus.SetOutput(io.Discard)
  40. s.router = buildRouter()
  41. }
  42. func (s *ProcessingHandlerTestSuite) TeardownSuite() {
  43. shutdown()
  44. logrus.SetOutput(os.Stdout)
  45. }
  46. func (s *ProcessingHandlerTestSuite) SetupTest() {
  47. // We don't need config.LocalFileSystemRoot anymore as it is used
  48. // only during initialization
  49. config.Reset()
  50. config.AllowLoopbackSourceAddresses = true
  51. }
  52. func (s *ProcessingHandlerTestSuite) send(path string, header ...http.Header) *httptest.ResponseRecorder {
  53. req := httptest.NewRequest(http.MethodGet, path, nil)
  54. rw := httptest.NewRecorder()
  55. if len(header) > 0 {
  56. req.Header = header[0]
  57. }
  58. s.router.ServeHTTP(rw, req)
  59. return rw
  60. }
  61. func (s *ProcessingHandlerTestSuite) readTestFile(name string) []byte {
  62. wd, err := os.Getwd()
  63. s.Require().NoError(err)
  64. data, err := os.ReadFile(filepath.Join(wd, "testdata", name))
  65. s.Require().NoError(err)
  66. return data
  67. }
  68. func (s *ProcessingHandlerTestSuite) readBody(res *http.Response) []byte {
  69. data, err := io.ReadAll(res.Body)
  70. s.Require().NoError(err)
  71. return data
  72. }
  73. func (s *ProcessingHandlerTestSuite) sampleETagData(imgETag string) (string, *imagedata.ImageData, string) {
  74. poStr := "rs:fill:4:4"
  75. po := options.NewProcessingOptions()
  76. po.ResizingType = options.ResizeFill
  77. po.Width = 4
  78. po.Height = 4
  79. imgdata := imagedata.ImageData{
  80. Type: imagetype.PNG,
  81. Data: s.readTestFile("test1.png"),
  82. }
  83. if len(imgETag) != 0 {
  84. imgdata.Headers = map[string]string{"ETag": imgETag}
  85. }
  86. var h etag.Handler
  87. h.SetActualProcessingOptions(po)
  88. h.SetActualImageData(&imgdata)
  89. return poStr, &imgdata, h.GenerateActualETag()
  90. }
  91. func (s *ProcessingHandlerTestSuite) TestRequest() {
  92. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png")
  93. res := rw.Result()
  94. s.Require().Equal(200, res.StatusCode)
  95. s.Require().Equal("image/png", res.Header.Get("Content-Type"))
  96. meta, err := imagemeta.DecodeMeta(res.Body)
  97. s.Require().NoError(err)
  98. s.Require().Equal(imagetype.PNG, meta.Format())
  99. s.Require().Equal(4, meta.Width())
  100. s.Require().Equal(4, meta.Height())
  101. }
  102. func (s *ProcessingHandlerTestSuite) TestSignatureValidationFailure() {
  103. config.Keys = [][]byte{[]byte("test-key")}
  104. config.Salts = [][]byte{[]byte("test-salt")}
  105. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png")
  106. res := rw.Result()
  107. s.Require().Equal(403, res.StatusCode)
  108. }
  109. func (s *ProcessingHandlerTestSuite) TestSignatureValidationSuccess() {
  110. config.Keys = [][]byte{[]byte("test-key")}
  111. config.Salts = [][]byte{[]byte("test-salt")}
  112. rw := s.send("/My9d3xq_PYpVHsPrCyww0Kh1w5KZeZhIlWhsa4az1TI/rs:fill:4:4/plain/local:///test1.png")
  113. res := rw.Result()
  114. s.Require().Equal(200, res.StatusCode)
  115. }
  116. func (s *ProcessingHandlerTestSuite) TestSourceValidation() {
  117. imagedata.RedirectAllRequestsTo("local:///test1.png")
  118. defer imagedata.StopRedirectingRequests()
  119. tt := []struct {
  120. name string
  121. allowedSources []string
  122. requestPath string
  123. expectedError bool
  124. }{
  125. {
  126. name: "match http URL without wildcard",
  127. allowedSources: []string{"local://", "http://images.dev/"},
  128. requestPath: "/unsafe/plain/http://images.dev/lorem/ipsum.jpg",
  129. expectedError: false,
  130. },
  131. {
  132. name: "match http URL with wildcard in hostname single level",
  133. allowedSources: []string{"local://", "http://*.mycdn.dev/"},
  134. requestPath: "/unsafe/plain/http://a-1.mycdn.dev/lorem/ipsum.jpg",
  135. expectedError: false,
  136. },
  137. {
  138. name: "match http URL with wildcard in hostname multiple levels",
  139. allowedSources: []string{"local://", "http://*.mycdn.dev/"},
  140. requestPath: "/unsafe/plain/http://a-1.b-2.mycdn.dev/lorem/ipsum.jpg",
  141. expectedError: false,
  142. },
  143. {
  144. name: "no match s3 URL with allowed local and http URLs",
  145. allowedSources: []string{"local://", "http://images.dev/"},
  146. requestPath: "/unsafe/plain/s3://images/lorem/ipsum.jpg",
  147. expectedError: true,
  148. },
  149. {
  150. name: "no match http URL with wildcard in hostname including slash",
  151. allowedSources: []string{"local://", "http://*.mycdn.dev/"},
  152. requestPath: "/unsafe/plain/http://other.dev/.mycdn.dev/lorem/ipsum.jpg",
  153. expectedError: true,
  154. },
  155. }
  156. for _, tc := range tt {
  157. s.Run(tc.name, func() {
  158. exps := make([]*regexp.Regexp, len(tc.allowedSources))
  159. for i, pattern := range tc.allowedSources {
  160. exps[i] = configurators.RegexpFromPattern(pattern)
  161. }
  162. config.AllowedSources = exps
  163. rw := s.send(tc.requestPath)
  164. res := rw.Result()
  165. if tc.expectedError {
  166. s.Require().Equal(404, res.StatusCode)
  167. } else {
  168. s.Require().Equal(200, res.StatusCode)
  169. }
  170. })
  171. }
  172. }
  173. func (s *ProcessingHandlerTestSuite) TestSourceNetworkValidation() {
  174. data := s.readTestFile("test1.png")
  175. server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  176. rw.WriteHeader(200)
  177. rw.Write(data)
  178. }))
  179. defer server.Close()
  180. var rw *httptest.ResponseRecorder
  181. u := fmt.Sprintf("/unsafe/rs:fill:4:4/plain/%s/test1.png", server.URL)
  182. rw = s.send(u)
  183. s.Require().Equal(200, rw.Result().StatusCode)
  184. config.AllowLoopbackSourceAddresses = false
  185. rw = s.send(u)
  186. s.Require().Equal(404, rw.Result().StatusCode)
  187. }
  188. func (s *ProcessingHandlerTestSuite) TestSourceFormatNotSupported() {
  189. vips.DisableLoadSupport(imagetype.PNG)
  190. defer vips.ResetLoadSupport()
  191. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png")
  192. res := rw.Result()
  193. s.Require().Equal(422, res.StatusCode)
  194. }
  195. func (s *ProcessingHandlerTestSuite) TestResultingFormatNotSupported() {
  196. vips.DisableSaveSupport(imagetype.PNG)
  197. defer vips.ResetSaveSupport()
  198. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png@png")
  199. res := rw.Result()
  200. s.Require().Equal(422, res.StatusCode)
  201. }
  202. func (s *ProcessingHandlerTestSuite) TestSkipProcessingConfig() {
  203. config.SkipProcessingFormats = []imagetype.Type{imagetype.PNG}
  204. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png")
  205. res := rw.Result()
  206. s.Require().Equal(200, res.StatusCode)
  207. actual := s.readBody(res)
  208. expected := s.readTestFile("test1.png")
  209. s.Require().True(bytes.Equal(expected, actual))
  210. }
  211. func (s *ProcessingHandlerTestSuite) TestSkipProcessingPO() {
  212. rw := s.send("/unsafe/rs:fill:4:4/skp:png/plain/local:///test1.png")
  213. res := rw.Result()
  214. s.Require().Equal(200, res.StatusCode)
  215. actual := s.readBody(res)
  216. expected := s.readTestFile("test1.png")
  217. s.Require().True(bytes.Equal(expected, actual))
  218. }
  219. func (s *ProcessingHandlerTestSuite) TestSkipProcessingSameFormat() {
  220. config.SkipProcessingFormats = []imagetype.Type{imagetype.PNG}
  221. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png@png")
  222. res := rw.Result()
  223. s.Require().Equal(200, res.StatusCode)
  224. actual := s.readBody(res)
  225. expected := s.readTestFile("test1.png")
  226. s.Require().True(bytes.Equal(expected, actual))
  227. }
  228. func (s *ProcessingHandlerTestSuite) TestSkipProcessingDifferentFormat() {
  229. config.SkipProcessingFormats = []imagetype.Type{imagetype.PNG}
  230. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png@jpg")
  231. res := rw.Result()
  232. s.Require().Equal(200, res.StatusCode)
  233. actual := s.readBody(res)
  234. expected := s.readTestFile("test1.png")
  235. s.Require().False(bytes.Equal(expected, actual))
  236. }
  237. func (s *ProcessingHandlerTestSuite) TestSkipProcessingSVG() {
  238. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.svg")
  239. res := rw.Result()
  240. s.Require().Equal(200, res.StatusCode)
  241. actual := s.readBody(res)
  242. expected, err := svg.Sanitize(&imagedata.ImageData{Data: s.readTestFile("test1.svg")})
  243. s.Require().NoError(err)
  244. s.Require().True(bytes.Equal(expected.Data, actual))
  245. }
  246. func (s *ProcessingHandlerTestSuite) TestNotSkipProcessingSVGToJPG() {
  247. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.svg@jpg")
  248. res := rw.Result()
  249. s.Require().Equal(200, res.StatusCode)
  250. actual := s.readBody(res)
  251. expected := s.readTestFile("test1.svg")
  252. s.Require().False(bytes.Equal(expected, actual))
  253. }
  254. func (s *ProcessingHandlerTestSuite) TestErrorSavingToSVG() {
  255. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png@svg")
  256. res := rw.Result()
  257. s.Require().Equal(422, res.StatusCode)
  258. }
  259. func (s *ProcessingHandlerTestSuite) TestCacheControlPassthroughCacheControl() {
  260. config.CacheControlPassthrough = true
  261. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  262. rw.Header().Set("Cache-Control", "max-age=1234, public")
  263. rw.Header().Set("Expires", time.Now().Add(time.Hour).UTC().Format(http.TimeFormat))
  264. rw.WriteHeader(200)
  265. rw.Write(s.readTestFile("test1.png"))
  266. }))
  267. defer ts.Close()
  268. rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
  269. res := rw.Result()
  270. s.Require().Equal("max-age=1234, public", res.Header.Get("Cache-Control"))
  271. s.Require().Empty(res.Header.Get("Expires"))
  272. }
  273. func (s *ProcessingHandlerTestSuite) TestCacheControlPassthroughExpires() {
  274. config.CacheControlPassthrough = true
  275. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  276. rw.Header().Set("Expires", time.Now().Add(1239*time.Second).UTC().Format(http.TimeFormat))
  277. rw.WriteHeader(200)
  278. rw.Write(s.readTestFile("test1.png"))
  279. }))
  280. defer ts.Close()
  281. rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
  282. res := rw.Result()
  283. // Use regex to allow some delay
  284. s.Require().Regexp("max-age=123[0-9], public", res.Header.Get("Cache-Control"))
  285. s.Require().Empty(res.Header.Get("Expires"))
  286. }
  287. func (s *ProcessingHandlerTestSuite) TestCacheControlPassthroughDisabled() {
  288. config.CacheControlPassthrough = false
  289. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  290. rw.Header().Set("Cache-Control", "max-age=1234, public")
  291. rw.Header().Set("Expires", time.Now().Add(time.Hour).UTC().Format(http.TimeFormat))
  292. rw.WriteHeader(200)
  293. rw.Write(s.readTestFile("test1.png"))
  294. }))
  295. defer ts.Close()
  296. rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
  297. res := rw.Result()
  298. s.Require().NotEqual("max-age=1234, public", res.Header.Get("Cache-Control"))
  299. s.Require().Empty(res.Header.Get("Expires"))
  300. }
  301. func (s *ProcessingHandlerTestSuite) TestETagDisabled() {
  302. config.ETagEnabled = false
  303. rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png")
  304. res := rw.Result()
  305. s.Require().Equal(200, res.StatusCode)
  306. s.Require().Empty(res.Header.Get("ETag"))
  307. }
  308. func (s *ProcessingHandlerTestSuite) TestETagReqNoIfNotModified() {
  309. config.ETagEnabled = true
  310. poStr, imgdata, etag := s.sampleETagData("loremipsumdolor")
  311. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  312. s.Empty(r.Header.Get("If-None-Match"))
  313. rw.Header().Set("ETag", imgdata.Headers["ETag"])
  314. rw.WriteHeader(200)
  315. rw.Write(s.readTestFile("test1.png"))
  316. }))
  317. defer ts.Close()
  318. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL))
  319. res := rw.Result()
  320. s.Require().Equal(200, res.StatusCode)
  321. s.Require().Equal(etag, res.Header.Get("ETag"))
  322. }
  323. func (s *ProcessingHandlerTestSuite) TestETagDataNoIfNotModified() {
  324. config.ETagEnabled = true
  325. poStr, imgdata, etag := s.sampleETagData("")
  326. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  327. s.Empty(r.Header.Get("If-None-Match"))
  328. rw.WriteHeader(200)
  329. rw.Write(imgdata.Data)
  330. }))
  331. defer ts.Close()
  332. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL))
  333. res := rw.Result()
  334. s.Require().Equal(200, res.StatusCode)
  335. s.Require().Equal(etag, res.Header.Get("ETag"))
  336. }
  337. func (s *ProcessingHandlerTestSuite) TestETagReqMatch() {
  338. config.ETagEnabled = true
  339. poStr, imgdata, etag := s.sampleETagData(`"loremipsumdolor"`)
  340. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  341. s.Equal(imgdata.Headers["ETag"], r.Header.Get("If-None-Match"))
  342. rw.WriteHeader(304)
  343. }))
  344. defer ts.Close()
  345. header := make(http.Header)
  346. header.Set("If-None-Match", etag)
  347. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
  348. res := rw.Result()
  349. s.Require().Equal(304, res.StatusCode)
  350. s.Require().Equal(etag, res.Header.Get("ETag"))
  351. }
  352. func (s *ProcessingHandlerTestSuite) TestETagDataMatch() {
  353. config.ETagEnabled = true
  354. poStr, imgdata, etag := s.sampleETagData("")
  355. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  356. s.Empty(r.Header.Get("If-None-Match"))
  357. rw.WriteHeader(200)
  358. rw.Write(imgdata.Data)
  359. }))
  360. defer ts.Close()
  361. header := make(http.Header)
  362. header.Set("If-None-Match", etag)
  363. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
  364. res := rw.Result()
  365. s.Require().Equal(304, res.StatusCode)
  366. s.Require().Equal(etag, res.Header.Get("ETag"))
  367. }
  368. func (s *ProcessingHandlerTestSuite) TestETagReqNotMatch() {
  369. config.ETagEnabled = true
  370. poStr, imgdata, actualETag := s.sampleETagData(`"loremipsumdolor"`)
  371. _, _, expectedETag := s.sampleETagData(`"loremipsum"`)
  372. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  373. s.Equal(`"loremipsum"`, r.Header.Get("If-None-Match"))
  374. rw.Header().Set("ETag", imgdata.Headers["ETag"])
  375. rw.WriteHeader(200)
  376. rw.Write(imgdata.Data)
  377. }))
  378. defer ts.Close()
  379. header := make(http.Header)
  380. header.Set("If-None-Match", expectedETag)
  381. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
  382. res := rw.Result()
  383. s.Require().Equal(200, res.StatusCode)
  384. s.Require().Equal(actualETag, res.Header.Get("ETag"))
  385. }
  386. func (s *ProcessingHandlerTestSuite) TestETagDataNotMatch() {
  387. config.ETagEnabled = true
  388. poStr, imgdata, actualETag := s.sampleETagData("")
  389. // Change the data hash
  390. expectedETag := actualETag[:strings.IndexByte(actualETag, '/')] + "/Dasdbefj"
  391. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  392. s.Empty(r.Header.Get("If-None-Match"))
  393. rw.WriteHeader(200)
  394. rw.Write(imgdata.Data)
  395. }))
  396. defer ts.Close()
  397. header := make(http.Header)
  398. header.Set("If-None-Match", expectedETag)
  399. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
  400. res := rw.Result()
  401. s.Require().Equal(200, res.StatusCode)
  402. s.Require().Equal(actualETag, res.Header.Get("ETag"))
  403. }
  404. func (s *ProcessingHandlerTestSuite) TestETagProcessingOptionsNotMatch() {
  405. config.ETagEnabled = true
  406. poStr, imgdata, actualETag := s.sampleETagData("")
  407. // Change the processing options hash
  408. expectedETag := "abcdefj" + actualETag[strings.IndexByte(actualETag, '/'):]
  409. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  410. s.Empty(r.Header.Get("If-None-Match"))
  411. rw.Header().Set("ETag", imgdata.Headers["ETag"])
  412. rw.WriteHeader(200)
  413. rw.Write(imgdata.Data)
  414. }))
  415. defer ts.Close()
  416. header := make(http.Header)
  417. header.Set("If-None-Match", expectedETag)
  418. rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
  419. res := rw.Result()
  420. s.Require().Equal(200, res.StatusCode)
  421. s.Require().Equal(actualETag, res.Header.Get("ETag"))
  422. }
  423. func (s *ProcessingHandlerTestSuite) TestLastModifiedEnabled() {
  424. config.LastModifiedEnabled = true
  425. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  426. rw.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
  427. rw.WriteHeader(200)
  428. rw.Write(s.readTestFile("test1.png"))
  429. }))
  430. defer ts.Close()
  431. rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
  432. res := rw.Result()
  433. s.Require().Equal("Wed, 21 Oct 2015 07:28:00 GMT", res.Header.Get("Last-Modified"))
  434. }
  435. func (s *ProcessingHandlerTestSuite) TestLastModifiedDisabled() {
  436. config.LastModifiedEnabled = false
  437. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  438. rw.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
  439. rw.WriteHeader(200)
  440. rw.Write(s.readTestFile("test1.png"))
  441. }))
  442. defer ts.Close()
  443. rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
  444. res := rw.Result()
  445. s.Require().Empty(res.Header.Get("Last-Modified"))
  446. }
  447. func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqExactMatchLastModifiedDisabled() {
  448. config.LastModifiedEnabled = false
  449. data := s.readTestFile("test1.png")
  450. lastModified := "Wed, 21 Oct 2015 07:28:00 GMT"
  451. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  452. modifiedSince := r.Header.Get("If-Modified-Since")
  453. s.Empty(modifiedSince)
  454. rw.WriteHeader(200)
  455. rw.Write(data)
  456. }))
  457. defer ts.Close()
  458. header := make(http.Header)
  459. header.Set("If-Modified-Since", lastModified)
  460. rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
  461. res := rw.Result()
  462. s.Require().Equal(200, res.StatusCode)
  463. }
  464. func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqExactMatchLastModifiedEnabled() {
  465. config.LastModifiedEnabled = true
  466. lastModified := "Wed, 21 Oct 2015 07:28:00 GMT"
  467. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  468. modifiedSince := r.Header.Get("If-Modified-Since")
  469. s.Equal(lastModified, modifiedSince)
  470. rw.WriteHeader(304)
  471. }))
  472. defer ts.Close()
  473. header := make(http.Header)
  474. header.Set("If-Modified-Since", lastModified)
  475. rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
  476. res := rw.Result()
  477. s.Require().Equal(304, res.StatusCode)
  478. }
  479. func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareMoreRecentLastModifiedDisabled() {
  480. data := s.readTestFile("test1.png")
  481. config.LastModifiedEnabled = false
  482. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  483. modifiedSince := r.Header.Get("If-Modified-Since")
  484. s.Empty(modifiedSince)
  485. rw.WriteHeader(200)
  486. rw.Write(data)
  487. }))
  488. defer ts.Close()
  489. recentTimestamp := "Thu, 25 Feb 2021 01:45:00 GMT"
  490. header := make(http.Header)
  491. header.Set("If-Modified-Since", recentTimestamp)
  492. rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
  493. res := rw.Result()
  494. s.Require().Equal(200, res.StatusCode)
  495. }
  496. func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareMoreRecentLastModifiedEnabled() {
  497. config.LastModifiedEnabled = true
  498. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  499. fileLastModified, _ := time.Parse(http.TimeFormat, "Wed, 21 Oct 2015 07:28:00 GMT")
  500. modifiedSince := r.Header.Get("If-Modified-Since")
  501. parsedModifiedSince, err := time.Parse(http.TimeFormat, modifiedSince)
  502. s.NoError(err)
  503. s.True(fileLastModified.Before(parsedModifiedSince))
  504. rw.WriteHeader(304)
  505. }))
  506. defer ts.Close()
  507. recentTimestamp := "Thu, 25 Feb 2021 01:45:00 GMT"
  508. header := make(http.Header)
  509. header.Set("If-Modified-Since", recentTimestamp)
  510. rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
  511. res := rw.Result()
  512. s.Require().Equal(304, res.StatusCode)
  513. }
  514. func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareTooOldLastModifiedDisabled() {
  515. config.LastModifiedEnabled = false
  516. data := s.readTestFile("test1.png")
  517. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  518. modifiedSince := r.Header.Get("If-Modified-Since")
  519. s.Empty(modifiedSince)
  520. rw.WriteHeader(200)
  521. rw.Write(data)
  522. }))
  523. defer ts.Close()
  524. oldTimestamp := "Tue, 01 Oct 2013 17:31:00 GMT"
  525. header := make(http.Header)
  526. header.Set("If-Modified-Since", oldTimestamp)
  527. rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
  528. res := rw.Result()
  529. s.Require().Equal(200, res.StatusCode)
  530. }
  531. func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareTooOldLastModifiedEnabled() {
  532. config.LastModifiedEnabled = true
  533. data := s.readTestFile("test1.png")
  534. ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
  535. fileLastModified, _ := time.Parse(http.TimeFormat, "Wed, 21 Oct 2015 07:28:00 GMT")
  536. modifiedSince := r.Header.Get("If-Modified-Since")
  537. parsedModifiedSince, err := time.Parse(http.TimeFormat, modifiedSince)
  538. s.NoError(err)
  539. s.True(fileLastModified.After(parsedModifiedSince))
  540. rw.WriteHeader(200)
  541. rw.Write(data)
  542. }))
  543. defer ts.Close()
  544. oldTimestamp := "Tue, 01 Oct 2013 17:31:00 GMT"
  545. header := make(http.Header)
  546. header.Set("If-Modified-Since", oldTimestamp)
  547. rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
  548. res := rw.Result()
  549. s.Require().Equal(200, res.StatusCode)
  550. }
  551. func (s *ProcessingHandlerTestSuite) TestAlwaysRasterizeSvg() {
  552. config.AlwaysRasterizeSvg = true
  553. rw := s.send("/unsafe/rs:fill:40:40/plain/local:///test1.svg")
  554. res := rw.Result()
  555. s.Require().Equal(200, res.StatusCode)
  556. s.Require().Equal("image/png", res.Header.Get("Content-Type"))
  557. }
  558. func (s *ProcessingHandlerTestSuite) TestAlwaysRasterizeSvgWithEnforceAvif() {
  559. config.AlwaysRasterizeSvg = true
  560. config.EnforceWebp = true
  561. rw := s.send("/unsafe/plain/local:///test1.svg", http.Header{"Accept": []string{"image/webp"}})
  562. res := rw.Result()
  563. s.Require().Equal(200, res.StatusCode)
  564. s.Require().Equal("image/webp", res.Header.Get("Content-Type"))
  565. }
  566. func (s *ProcessingHandlerTestSuite) TestAlwaysRasterizeSvgDisabled() {
  567. config.AlwaysRasterizeSvg = false
  568. config.EnforceWebp = true
  569. rw := s.send("/unsafe/plain/local:///test1.svg")
  570. res := rw.Result()
  571. s.Require().Equal(200, res.StatusCode)
  572. s.Require().Equal("image/svg+xml", res.Header.Get("Content-Type"))
  573. }
  574. func (s *ProcessingHandlerTestSuite) TestAlwaysRasterizeSvgWithFormat() {
  575. config.AlwaysRasterizeSvg = true
  576. config.SkipProcessingFormats = []imagetype.Type{imagetype.SVG}
  577. rw := s.send("/unsafe/plain/local:///test1.svg@svg")
  578. res := rw.Result()
  579. s.Require().Equal(200, res.StatusCode)
  580. s.Require().Equal("image/svg+xml", res.Header.Get("Content-Type"))
  581. }
  582. func TestProcessingHandler(t *testing.T) {
  583. suite.Run(t, new(ProcessingHandlerTestSuite))
  584. }