DarthSim 3 лет назад
Родитель
Сommit
749d29bf44
2 измененных файлов с 80 добавлено и 50 удалено
  1. 9 2
      etag/etag_test.go
  2. 71 48
      processing_handler_test.go

+ 9 - 2
etag/etag_test.go

@@ -24,8 +24,8 @@ var (
 		Data: []byte("Hello Test"),
 	}
 
-	etagReq  = `"ATeSQpxYMfaZVBSmCh-zpE8682vBUrZ1qxXgQkxtntA/RImxvcmVtaXBzdW1kb2xvciI"`
-	etagData = `"ATeSQpxYMfaZVBSmCh-zpE8682vBUrZ1qxXgQkxtntA/DvyChhMNu_sFX7jrjoyrgQbnFwfoOVv7kzp_Fbs6hQBg"`
+	etagReq  string
+	etagData string
 )
 
 type EtagTestSuite struct {
@@ -36,6 +36,13 @@ type EtagTestSuite struct {
 
 func (s *EtagTestSuite) SetupSuite() {
 	logrus.SetOutput(ioutil.Discard)
+
+	s.h.SetActualProcessingOptions(po)
+	s.h.SetActualImageData(&imgWithETag)
+	etagReq = s.h.GenerateActualETag()
+
+	s.h.SetActualImageData(&imgWithoutETag)
+	etagData = s.h.GenerateActualETag()
 }
 
 func (s *EtagTestSuite) TeardownSuite() {

+ 71 - 48
processing_handler_test.go

@@ -2,19 +2,23 @@ package main
 
 import (
 	"bytes"
+	"fmt"
 	"io/ioutil"
 	"net/http"
 	"net/http/httptest"
 	"os"
 	"path/filepath"
 	"regexp"
+	"strings"
 	"testing"
 
 	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/config/configurators"
+	"github.com/imgproxy/imgproxy/v3/etag"
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/imagemeta"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
+	"github.com/imgproxy/imgproxy/v3/options"
 	"github.com/imgproxy/imgproxy/v3/router"
 	"github.com/imgproxy/imgproxy/v3/vips"
 	"github.com/sirupsen/logrus"
@@ -83,6 +87,30 @@ func (s *ProcessingHandlerTestSuite) readBody(res *http.Response) []byte {
 	return data
 }
 
+func (s *ProcessingHandlerTestSuite) sampleETagData(imgETag string) (string, *imagedata.ImageData, string) {
+	poStr := "rs:fill:4:4"
+
+	po := options.NewProcessingOptions()
+	po.ResizingType = options.ResizeFill
+	po.Width = 4
+	po.Height = 4
+
+	imgdata := imagedata.ImageData{
+		Type: imagetype.PNG,
+		Data: s.readTestFile("test1.png"),
+	}
+
+	if len(imgETag) != 0 {
+		imgdata.Headers = map[string]string{"ETag": imgETag}
+	}
+
+	var h etag.Handler
+
+	h.SetActualProcessingOptions(po)
+	h.SetActualImageData(&imgdata)
+	return poStr, &imgdata, h.GenerateActualETag()
+}
+
 func (s *ProcessingHandlerTestSuite) TestRequest() {
 	rw := s.send("/unsafe/rs:fill:4:4/plain/local:///test1.png")
 	res := rw.Result()
@@ -334,64 +362,60 @@ func (s *ProcessingHandlerTestSuite) TestETagDisabled() {
 func (s *ProcessingHandlerTestSuite) TestETagReqNoIfNotModified() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, etag := s.sampleETagData("loremipsumdolor")
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
 		assert.Empty(s.T(), r.Header.Get("If-None-Match"))
 
-		rw.Header().Set("ETag", `"loremipsumdolor"`)
+		rw.Header().Set("ETag", imgdata.Headers["ETag"])
 		rw.WriteHeader(200)
 		rw.Write(s.readTestFile("test1.png"))
 	}))
 	defer ts.Close()
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL))
 	res := rw.Result()
 
 	assert.Equal(s.T(), 200, res.StatusCode)
-	assert.Equal(
-		s.T(),
-		`"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/RImxvcmVtaXBzdW1kb2xvciI"`,
-		res.Header.Get("ETag"),
-	)
+	assert.Equal(s.T(), etag, res.Header.Get("ETag"))
 }
 
 func (s *ProcessingHandlerTestSuite) TestETagDataNoIfNotModified() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, etag := s.sampleETagData("")
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
 		assert.Empty(s.T(), r.Header.Get("If-None-Match"))
 
 		rw.WriteHeader(200)
-		rw.Write(s.readTestFile("test1.png"))
+		rw.Write(imgdata.Data)
 	}))
 	defer ts.Close()
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/" + ts.URL)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL))
 	res := rw.Result()
 
 	assert.Equal(s.T(), 200, res.StatusCode)
-	assert.Equal(
-		s.T(),
-		`"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/Dl29MNvkqdLEqPyFvMlh_NlPbRrsC0GDG_AUlmMdX6HA"`,
-		res.Header.Get("ETag"),
-	)
+	assert.Equal(s.T(), etag, res.Header.Get("ETag"))
 }
 
 func (s *ProcessingHandlerTestSuite) TestETagReqMatch() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, etag := s.sampleETagData(`"loremipsumdolor"`)
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
-		assert.Equal(s.T(), `"loremipsumdolor"`, r.Header.Get("If-None-Match"))
+		assert.Equal(s.T(), imgdata.Headers["ETag"], r.Header.Get("If-None-Match"))
 
 		rw.WriteHeader(304)
 	}))
 	defer ts.Close()
 
-	etag := `"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/RImxvcmVtaXBzdW1kb2xvciI"`
-
 	header := make(http.Header)
 	header.Set("If-None-Match", etag)
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/"+ts.URL, header)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
 	res := rw.Result()
 
 	assert.Equal(s.T(), 304, res.StatusCode)
@@ -401,20 +425,20 @@ func (s *ProcessingHandlerTestSuite) TestETagReqMatch() {
 func (s *ProcessingHandlerTestSuite) TestETagDataMatch() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, etag := s.sampleETagData("")
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
 		assert.Empty(s.T(), r.Header.Get("If-None-Match"))
 
 		rw.WriteHeader(200)
-		rw.Write(s.readTestFile("test1.png"))
+		rw.Write(imgdata.Data)
 	}))
 	defer ts.Close()
 
-	etag := `"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/Dl29MNvkqdLEqPyFvMlh_NlPbRrsC0GDG_AUlmMdX6HA"`
-
 	header := make(http.Header)
 	header.Set("If-None-Match", etag)
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/"+ts.URL, header)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
 	res := rw.Result()
 
 	assert.Equal(s.T(), 304, res.StatusCode)
@@ -424,78 +448,77 @@ func (s *ProcessingHandlerTestSuite) TestETagDataMatch() {
 func (s *ProcessingHandlerTestSuite) TestETagReqNotMatch() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, actualETag := s.sampleETagData(`"loremipsumdolor"`)
+	_, _, expectedETag := s.sampleETagData(`"loremipsum"`)
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
 		assert.Equal(s.T(), `"loremipsum"`, r.Header.Get("If-None-Match"))
 
-		rw.Header().Set("ETag", `"loremipsumdolor"`)
+		rw.Header().Set("ETag", imgdata.Headers["ETag"])
 		rw.WriteHeader(200)
-		rw.Write(s.readTestFile("test1.png"))
+		rw.Write(imgdata.Data)
 	}))
 	defer ts.Close()
 
 	header := make(http.Header)
-	header.Set("If-None-Match", `"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/RImxvcmVtaXBzdW0i"`)
+	header.Set("If-None-Match", expectedETag)
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/"+ts.URL, header)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
 	res := rw.Result()
 
 	assert.Equal(s.T(), 200, res.StatusCode)
-	assert.Equal(
-		s.T(),
-		`"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/RImxvcmVtaXBzdW1kb2xvciI"`,
-		res.Header.Get("ETag"),
-	)
+	assert.Equal(s.T(), actualETag, res.Header.Get("ETag"))
 }
 
 func (s *ProcessingHandlerTestSuite) TestETagDataNotMatch() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, actualETag := s.sampleETagData("")
+	// Change the data hash
+	expectedETag := actualETag[:strings.IndexByte(actualETag, '/')] + "/Dasdbefj"
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
 		assert.Empty(s.T(), r.Header.Get("If-None-Match"))
 
 		rw.WriteHeader(200)
-		rw.Write(s.readTestFile("test1.png"))
+		rw.Write(imgdata.Data)
 	}))
 	defer ts.Close()
 
 	header := make(http.Header)
-	header.Set("If-None-Match", `"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/Dl29MNvkqdLEq"`)
+	header.Set("If-None-Match", expectedETag)
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/"+ts.URL, header)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
 	res := rw.Result()
 
 	assert.Equal(s.T(), 200, res.StatusCode)
-	assert.Equal(
-		s.T(),
-		`"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/Dl29MNvkqdLEqPyFvMlh_NlPbRrsC0GDG_AUlmMdX6HA"`,
-		res.Header.Get("ETag"),
-	)
+	assert.Equal(s.T(), actualETag, res.Header.Get("ETag"))
 }
 
 func (s *ProcessingHandlerTestSuite) TestETagProcessingOptionsNotMatch() {
 	config.ETagEnabled = true
 
+	poStr, imgdata, actualETag := s.sampleETagData("")
+	// Change the processing options hash
+	expectedETag := "abcdefj" + actualETag[strings.IndexByte(actualETag, '/'):]
+
 	ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
 		assert.Empty(s.T(), r.Header.Get("If-None-Match"))
 
-		rw.Header().Set("ETag", `"loremipsumdolor"`)
+		rw.Header().Set("ETag", imgdata.Headers["ETag"])
 		rw.WriteHeader(200)
-		rw.Write(s.readTestFile("test1.png"))
+		rw.Write(imgdata.Data)
 	}))
 	defer ts.Close()
 
 	header := make(http.Header)
-	header.Set("If-None-Match", `"1Uuny6YTSUO08MMVZ/Dl29MNvkqdLEq"`)
+	header.Set("If-None-Match", expectedETag)
 
-	rw := s.send("/unsafe/rs:fill:4:4/plain/"+ts.URL, header)
+	rw := s.send(fmt.Sprintf("/unsafe/%s/plain/%s", poStr, ts.URL), header)
 	res := rw.Result()
 
 	assert.Equal(s.T(), 200, res.StatusCode)
-	assert.Equal(
-		s.T(),
-		`"1Uuny6YTSUO08MMVZyantp9oaoOcsaYibQoNbBTJEzk/RImxvcmVtaXBzdW1kb2xvciI"`,
-		res.Header.Get("ETag"),
-	)
+	assert.Equal(s.T(), actualETag, res.Header.Get("ETag"))
 }
 
 func TestProcessingHandler(t *testing.T) {