mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-09-28 20:43:54 +02:00
Support Last-Modified response header and support If-Modified-Since request header. (#1147)
* Always return Last-Modified and support If-Modified-Since. * IMGPROXY_USE_LAST_MODIFIED config setting. IMGPROXY_USE_LAST_MODIFIED (default false) when enabled will return the Last-Modified time of the upstream image and also allow the support of the If-Modified-Since request header (returning a 304 if the image hasn't been modified). If-Modified-Since allows If-None-Match to take precedence. * Fixes based on DarthSim's feedback. 1. Don't worry about nil maps. 2. Fix a test now that we use the config.LastModifiedEnabled (and move it's location it he test file to a more sane place). 3. Update GCS transport code based on the refactoring of DarthSim. In this iteration, we pull the Updated time from the GCS object attributes and format them as a string. We then parse it in the notmodified module. Seems a bit silly to do it this way. If we agree on the approach here, then AWS and Azure can follow. * Support azure, fs, s3, and swift. * Grab the headers for If-Modified-Since and Last-Modified before parsing them. * Add tests for last-modified for fs. * Support Last-Modified being passed when streaming an upstream file. * Tests for Last-Modified for GCS and Azure * Support s3 and swift tests. Sadly fakes3 doesn't support Last-Modified * Test against forked gofakes3
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/imgproxy/imgproxy/v3/config"
|
||||
"github.com/imgproxy/imgproxy/v3/config/configurators"
|
||||
@@ -550,6 +551,158 @@ func (s *ProcessingHandlerTestSuite) TestETagProcessingOptionsNotMatch() {
|
||||
require.Equal(s.T(), actualETag, res.Header.Get("ETag"))
|
||||
}
|
||||
|
||||
func (s *ProcessingHandlerTestSuite) TestLastModifiedEnabled() {
|
||||
config.LastModifiedEnabled = true
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(s.readTestFile("test1.png"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), "Wed, 21 Oct 2015 07:28:00 GMT", res.Header.Get("Last-Modified"))
|
||||
}
|
||||
|
||||
func (s *ProcessingHandlerTestSuite) TestLastModifiedDisabled() {
|
||||
config.LastModifiedEnabled = false
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(s.readTestFile("test1.png"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), "", res.Header.Get("Last-Modified"))
|
||||
}
|
||||
|
||||
func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqExactMatchLastModifiedDisabled() {
|
||||
config.LastModifiedEnabled = false
|
||||
data := s.readTestFile("test1.png")
|
||||
lastModified := "Wed, 21 Oct 2015 07:28:00 GMT"
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
modifiedSince := r.Header.Get("If-Modified-Since")
|
||||
require.Equal(s.T(), "", modifiedSince)
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(data)
|
||||
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
header := make(http.Header)
|
||||
header.Set("If-Modified-Since", lastModified)
|
||||
rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), 200, res.StatusCode)
|
||||
}
|
||||
func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqExactMatchLastModifiedEnabled() {
|
||||
config.LastModifiedEnabled = true
|
||||
lastModified := "Wed, 21 Oct 2015 07:28:00 GMT"
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
modifiedSince := r.Header.Get("If-Modified-Since")
|
||||
require.Equal(s.T(), lastModified, modifiedSince)
|
||||
rw.WriteHeader(304)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
header := make(http.Header)
|
||||
header.Set("If-Modified-Since", lastModified)
|
||||
rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), 304, res.StatusCode)
|
||||
}
|
||||
|
||||
func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareMoreRecentLastModifiedDisabled() {
|
||||
data := s.readTestFile("test1.png")
|
||||
config.LastModifiedEnabled = false
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
modifiedSince := r.Header.Get("If-Modified-Since")
|
||||
require.Equal(s.T(), modifiedSince, "")
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(data)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
recentTimestamp := "Thu, 25 Feb 2021 01:45:00 GMT"
|
||||
|
||||
header := make(http.Header)
|
||||
header.Set("If-Modified-Since", recentTimestamp)
|
||||
rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), 200, res.StatusCode)
|
||||
}
|
||||
func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareMoreRecentLastModifiedEnabled() {
|
||||
config.LastModifiedEnabled = true
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
fileLastModified, _ := time.Parse(http.TimeFormat, "Wed, 21 Oct 2015 07:28:00 GMT")
|
||||
modifiedSince := r.Header.Get("If-Modified-Since")
|
||||
parsedModifiedSince, err := time.Parse(http.TimeFormat, modifiedSince)
|
||||
require.Nil(s.T(), err)
|
||||
require.True(s.T(), fileLastModified.Before(parsedModifiedSince))
|
||||
rw.WriteHeader(304)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
recentTimestamp := "Thu, 25 Feb 2021 01:45:00 GMT"
|
||||
|
||||
header := make(http.Header)
|
||||
header.Set("If-Modified-Since", recentTimestamp)
|
||||
rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), 304, res.StatusCode)
|
||||
}
|
||||
func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareTooOldLastModifiedDisabled() {
|
||||
config.LastModifiedEnabled = false
|
||||
data := s.readTestFile("test1.png")
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
modifiedSince := r.Header.Get("If-Modified-Since")
|
||||
require.Equal(s.T(), modifiedSince, "")
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(data)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
oldTimestamp := "Tue, 01 Oct 2013 17:31:00 GMT"
|
||||
|
||||
header := make(http.Header)
|
||||
header.Set("If-Modified-Since", oldTimestamp)
|
||||
rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), 200, res.StatusCode)
|
||||
}
|
||||
func (s *ProcessingHandlerTestSuite) TestModifiedSinceReqCompareTooOldLastModifiedEnabled() {
|
||||
config.LastModifiedEnabled = true
|
||||
data := s.readTestFile("test1.png")
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
fileLastModified, _ := time.Parse(http.TimeFormat, "Wed, 21 Oct 2015 07:28:00 GMT")
|
||||
modifiedSince := r.Header.Get("If-Modified-Since")
|
||||
parsedModifiedSince, err := time.Parse(http.TimeFormat, modifiedSince)
|
||||
require.Nil(s.T(), err)
|
||||
require.True(s.T(), fileLastModified.After(parsedModifiedSince))
|
||||
rw.WriteHeader(200)
|
||||
rw.Write(data)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
oldTimestamp := "Tue, 01 Oct 2013 17:31:00 GMT"
|
||||
|
||||
header := make(http.Header)
|
||||
header.Set("If-Modified-Since", oldTimestamp)
|
||||
rw := s.send(fmt.Sprintf("/unsafe/plain/%s", ts.URL), header)
|
||||
res := rw.Result()
|
||||
|
||||
require.Equal(s.T(), 200, res.StatusCode)
|
||||
}
|
||||
func TestProcessingHandler(t *testing.T) {
|
||||
suite.Run(t, new(ProcessingHandlerTestSuite))
|
||||
}
|
||||
|
Reference in New Issue
Block a user