1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/etag/etag_test.go

145 lines
3.6 KiB
Go
Raw Normal View History

2021-09-29 12:23:54 +02:00
package etag
import (
2022-10-28 17:15:13 +02:00
"io"
2021-09-29 12:23:54 +02:00
"os"
"strings"
"testing"
2021-12-07 11:34:35 +02:00
"github.com/imgproxy/imgproxy/v3/config"
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/imagedata"
"github.com/imgproxy/imgproxy/v3/options"
2021-09-29 12:23:54 +02:00
"github.com/sirupsen/logrus"
2022-07-18 15:48:50 +02:00
"github.com/stretchr/testify/require"
2021-09-29 12:23:54 +02:00
"github.com/stretchr/testify/suite"
)
var (
po = options.NewProcessingOptions()
imgWithETag = imagedata.ImageData{
Data: []byte("Hello Test"),
Headers: map[string]string{"ETag": `"loremipsumdolor"`},
}
imgWithoutETag = imagedata.ImageData{
Data: []byte("Hello Test"),
}
2021-12-07 11:34:35 +02:00
etagReq = `"yj0WO6sFU4GCciYUBWjzvvfqrBh869doeOC2Pp5EI1Y/RImxvcmVtaXBzdW1kb2xvciI"`
etagData = `"yj0WO6sFU4GCciYUBWjzvvfqrBh869doeOC2Pp5EI1Y/DvyChhMNu_sFX7jrjoyrgQbnFwfoOVv7kzp_Fbs6hQBg"`
2021-09-29 12:23:54 +02:00
)
type EtagTestSuite struct {
suite.Suite
h Handler
}
func (s *EtagTestSuite) SetupSuite() {
2022-10-28 17:15:13 +02:00
logrus.SetOutput(io.Discard)
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TeardownSuite() {
logrus.SetOutput(os.Stdout)
}
func (s *EtagTestSuite) SetupTest() {
s.h = Handler{}
2021-12-07 11:34:35 +02:00
config.Reset()
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestGenerateActualReq() {
s.h.SetActualProcessingOptions(po)
s.h.SetActualImageData(&imgWithETag)
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), etagReq, s.h.GenerateActualETag())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestGenerateActualData() {
s.h.SetActualProcessingOptions(po)
s.h.SetActualImageData(&imgWithoutETag)
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), etagData, s.h.GenerateActualETag())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestGenerateExpectedReq() {
s.h.ParseExpectedETag(etagReq)
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), etagReq, s.h.GenerateExpectedETag())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestGenerateExpectedData() {
s.h.ParseExpectedETag(etagData)
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), etagData, s.h.GenerateExpectedETag())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestProcessingOptionsCheckSuccess() {
s.h.ParseExpectedETag(etagReq)
2022-07-18 15:48:50 +02:00
require.True(s.T(), s.h.SetActualProcessingOptions(po))
require.True(s.T(), s.h.ProcessingOptionsMatch())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestProcessingOptionsCheckFailure() {
i := strings.Index(etagReq, "/")
wrongEtag := `"wrongpohash` + etagReq[i:]
s.h.ParseExpectedETag(wrongEtag)
2022-07-18 15:48:50 +02:00
require.False(s.T(), s.h.SetActualProcessingOptions(po))
require.False(s.T(), s.h.ProcessingOptionsMatch())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageETagExpectedPresent() {
s.h.ParseExpectedETag(etagReq)
2022-07-18 15:48:50 +02:00
require.Equal(s.T(), imgWithETag.Headers["ETag"], s.h.ImageEtagExpected())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageETagExpectedBlank() {
s.h.ParseExpectedETag(etagData)
2022-07-18 15:48:50 +02:00
require.Empty(s.T(), s.h.ImageEtagExpected())
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageDataCheckDataToDataSuccess() {
s.h.ParseExpectedETag(etagData)
2022-07-18 15:48:50 +02:00
require.True(s.T(), s.h.SetActualImageData(&imgWithoutETag))
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageDataCheckDataToDataFailure() {
i := strings.Index(etagData, "/")
wrongEtag := etagData[:i] + `/Dwrongimghash"`
s.h.ParseExpectedETag(wrongEtag)
2022-07-18 15:48:50 +02:00
require.False(s.T(), s.h.SetActualImageData(&imgWithoutETag))
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageDataCheckDataToReqSuccess() {
s.h.ParseExpectedETag(etagData)
2022-07-18 15:48:50 +02:00
require.True(s.T(), s.h.SetActualImageData(&imgWithETag))
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageDataCheckDataToReqFailure() {
i := strings.Index(etagData, "/")
wrongEtag := etagData[:i] + `/Dwrongimghash"`
s.h.ParseExpectedETag(wrongEtag)
2022-07-18 15:48:50 +02:00
require.False(s.T(), s.h.SetActualImageData(&imgWithETag))
2021-09-29 12:23:54 +02:00
}
func (s *EtagTestSuite) TestImageDataCheckReqToDataFailure() {
s.h.ParseExpectedETag(etagReq)
2022-07-18 15:48:50 +02:00
require.False(s.T(), s.h.SetActualImageData(&imgWithoutETag))
2021-09-29 12:23:54 +02:00
}
2021-12-07 11:34:35 +02:00
func (s *EtagTestSuite) TestETagBusterFailure() {
config.ETagBuster = "busted"
s.h.ParseExpectedETag(etagReq)
2022-07-18 15:48:50 +02:00
require.False(s.T(), s.h.SetActualImageData(&imgWithoutETag))
2021-12-07 11:34:35 +02:00
}
2021-09-29 12:23:54 +02:00
func TestEtag(t *testing.T) {
suite.Run(t, new(EtagTestSuite))
}