1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-17 11:55:32 +02:00
imgproxy/transport/s3/s3_test.go
Ewan Higgs 4944dfab30
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
2023-05-03 21:21:46 +06:00

161 lines
4.4 KiB
Go

package s3
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/johannesboyne/gofakes3"
"github.com/johannesboyne/gofakes3/backend/s3mem"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/imgproxy/imgproxy/v3/config"
)
type S3TestSuite struct {
suite.Suite
server *httptest.Server
transport http.RoundTripper
etag string
lastModified time.Time
}
func (s *S3TestSuite) SetupSuite() {
backend := s3mem.New()
faker := gofakes3.New(backend)
s.server = httptest.NewServer(faker.Server())
config.S3Enabled = true
config.S3Endpoint = s.server.URL
os.Setenv("AWS_REGION", "eu-central-1")
os.Setenv("AWS_ACCESS_KEY_ID", "Foo")
os.Setenv("AWS_SECRET_ACCESS_KEY", "Bar")
var err error
s.transport, err = New()
require.Nil(s.T(), err)
svc := s.transport.(transport).svc
_, err = svc.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("test"),
})
require.Nil(s.T(), err)
_, err = svc.PutObject(&s3.PutObjectInput{
Body: bytes.NewReader(make([]byte, 32)),
Bucket: aws.String("test"),
Key: aws.String("foo/test.png"),
})
require.Nil(s.T(), err)
obj, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String("test"),
Key: aws.String("foo/test.png"),
})
require.Nil(s.T(), err)
defer obj.Body.Close()
s.etag = *obj.ETag
s.lastModified = *obj.LastModified
}
func (s *S3TestSuite) TearDownSuite() {
s.server.Close()
}
func (s *S3TestSuite) TestRoundTripWithETagDisabledReturns200() {
config.ETagEnabled = false
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), 200, response.StatusCode)
}
func (s *S3TestSuite) TestRoundTripWithETagEnabled() {
config.ETagEnabled = true
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), 200, response.StatusCode)
require.Equal(s.T(), s.etag, response.Header.Get("ETag"))
}
func (s *S3TestSuite) TestRoundTripWithIfNoneMatchReturns304() {
config.ETagEnabled = true
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
request.Header.Set("If-None-Match", s.etag)
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), http.StatusNotModified, response.StatusCode)
}
func (s *S3TestSuite) TestRoundTripWithUpdatedETagReturns200() {
config.ETagEnabled = true
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
request.Header.Set("If-None-Match", s.etag+"_wrong")
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), http.StatusOK, response.StatusCode)
}
func (s *S3TestSuite) TestRoundTripWithLastModifiedDisabledReturns200() {
config.LastModifiedEnabled = false
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), 200, response.StatusCode)
}
func (s *S3TestSuite) TestRoundTripWithLastModifiedEnabled() {
config.ETagEnabled = true
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), 200, response.StatusCode)
require.Equal(s.T(), s.lastModified.Format(http.TimeFormat), response.Header.Get("Last-Modified"))
}
// gofakes3 doesn't support If-Modified-Since (yet?)
func (s *S3TestSuite) TestRoundTripWithIfModifiedSinceReturns304() {
config.LastModifiedEnabled = true
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
request.Header.Set("If-Modified-Since", s.lastModified.Format(http.TimeFormat))
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), http.StatusNotModified, response.StatusCode)
}
func (s *S3TestSuite) TestRoundTripWithUpdatedLastModifiedReturns200() {
config.LastModifiedEnabled = true
request, _ := http.NewRequest("GET", "s3://test/foo/test.png", nil)
request.Header.Set("If-Modified-Since", s.lastModified.Add(-24*time.Hour).Format(http.TimeFormat))
response, err := s.transport.RoundTrip(request)
require.Nil(s.T(), err)
require.Equal(s.T(), http.StatusOK, response.StatusCode)
}
func TestS3Transport(t *testing.T) {
suite.Run(t, new(S3TestSuite))
}