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"
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().Equal(etagReq, s.h.GenerateActualETag())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestGenerateActualData() {
|
|
|
|
s.h.SetActualProcessingOptions(po)
|
|
|
|
s.h.SetActualImageData(&imgWithoutETag)
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().Equal(etagData, s.h.GenerateActualETag())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestGenerateExpectedReq() {
|
|
|
|
s.h.ParseExpectedETag(etagReq)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().Equal(etagReq, s.h.GenerateExpectedETag())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestGenerateExpectedData() {
|
|
|
|
s.h.ParseExpectedETag(etagData)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().Equal(etagData, s.h.GenerateExpectedETag())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestProcessingOptionsCheckSuccess() {
|
|
|
|
s.h.ParseExpectedETag(etagReq)
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().True(s.h.SetActualProcessingOptions(po))
|
|
|
|
s.Require().True(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)
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().False(s.h.SetActualProcessingOptions(po))
|
|
|
|
s.Require().False(s.h.ProcessingOptionsMatch())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestImageETagExpectedPresent() {
|
|
|
|
s.h.ParseExpectedETag(etagReq)
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
//nolint:testifylint // False-positive expected-actual
|
|
|
|
s.Require().Equal(imgWithETag.Headers["ETag"], s.h.ImageEtagExpected())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestImageETagExpectedBlank() {
|
|
|
|
s.h.ParseExpectedETag(etagData)
|
|
|
|
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().Empty(s.h.ImageEtagExpected())
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestImageDataCheckDataToDataSuccess() {
|
|
|
|
s.h.ParseExpectedETag(etagData)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().True(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)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().False(s.h.SetActualImageData(&imgWithoutETag))
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestImageDataCheckDataToReqSuccess() {
|
|
|
|
s.h.ParseExpectedETag(etagData)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().True(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)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().False(s.h.SetActualImageData(&imgWithETag))
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EtagTestSuite) TestImageDataCheckReqToDataFailure() {
|
|
|
|
s.h.ParseExpectedETag(etagReq)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().False(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)
|
2024-04-18 20:16:32 +02:00
|
|
|
s.Require().False(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))
|
|
|
|
}
|