processing_handler_test.go 21 KB

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