|
@@ -8,25 +8,65 @@ import (
|
|
|
"regexp"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
-
|
|
|
- "github.com/stretchr/testify/suite"
|
|
|
+ "time"
|
|
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
|
"github.com/imgproxy/imgproxy/v3/imagetype"
|
|
|
+ "github.com/imgproxy/imgproxy/v3/security"
|
|
|
+ "github.com/imgproxy/imgproxy/v3/testutil"
|
|
|
+ "github.com/stretchr/testify/suite"
|
|
|
)
|
|
|
|
|
|
-type ProcessingOptionsTestSuite struct{ suite.Suite }
|
|
|
+type ProcessingOptionsTestSuite struct {
|
|
|
+ testutil.LazySuite
|
|
|
+
|
|
|
+ securityCfg testutil.LazyObj[*security.Config]
|
|
|
+ security testutil.LazyObj[*security.Checker]
|
|
|
+
|
|
|
+ config testutil.LazyObj[*Config]
|
|
|
+ factory testutil.LazyObj[*Factory]
|
|
|
+}
|
|
|
+
|
|
|
+func (s *ProcessingOptionsTestSuite) SetupSuite() {
|
|
|
+ s.config, _ = testutil.NewLazySuiteObj(
|
|
|
+ s,
|
|
|
+ func() (*Config, error) {
|
|
|
+ c := NewDefaultConfig()
|
|
|
+ return &c, nil
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ s.securityCfg, _ = testutil.NewLazySuiteObj(
|
|
|
+ s,
|
|
|
+ func() (*security.Config, error) {
|
|
|
+ c := security.NewDefaultConfig()
|
|
|
+ return &c, nil
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ s.security, _ = testutil.NewLazySuiteObj(
|
|
|
+ s,
|
|
|
+ func() (*security.Checker, error) {
|
|
|
+ return security.New(s.securityCfg())
|
|
|
+ },
|
|
|
+ )
|
|
|
|
|
|
-func (s *ProcessingOptionsTestSuite) SetupTest() {
|
|
|
- config.Reset()
|
|
|
- // Reset presets
|
|
|
- presets = make(map[string]urlOptions)
|
|
|
+ s.factory, _ = testutil.NewLazySuiteObj(
|
|
|
+ s,
|
|
|
+ func() (*Factory, error) {
|
|
|
+ return NewFactory(s.config(), s.security())
|
|
|
+ },
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+func (s *ProcessingOptionsTestSuite) SetupSubTest() {
|
|
|
+ s.ResetLazyObjects()
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseBase64URL() {
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal(originURL, imageURL)
|
|
@@ -34,11 +74,11 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URL() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithFilename() {
|
|
|
- config.Base64URLIncludesFilename = true
|
|
|
+ s.config().Base64URLIncludesFilename = true
|
|
|
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/%s.png/puppy.jpg", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal(originURL, imageURL)
|
|
@@ -48,7 +88,7 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithFilename() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithoutExtension() {
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/%s", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal(originURL, imageURL)
|
|
@@ -56,26 +96,26 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithoutExtension() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
|
|
|
- config.BaseURL = "http://images.dev/"
|
|
|
+ s.config().BaseURL = "http://images.dev/"
|
|
|
|
|
|
originURL := "lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
- s.Require().Equal(fmt.Sprintf("%s%s", config.BaseURL, originURL), imageURL)
|
|
|
+ s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
|
|
|
s.Require().Equal(imagetype.PNG, po.Format)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithReplacement() {
|
|
|
- config.URLReplacements = []config.URLReplacement{
|
|
|
+ s.config().URLReplacements = []config.URLReplacement{
|
|
|
{Regexp: regexp.MustCompile("^test://([^/]*)/"), Replacement: "test2://images.dev/${1}/dolor/"},
|
|
|
{Regexp: regexp.MustCompile("^test2://"), Replacement: "http://"},
|
|
|
}
|
|
|
|
|
|
originURL := "test://lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal("http://images.dev/lorem/dolor/ipsum.jpg?param=value", imageURL)
|
|
@@ -85,7 +125,7 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithReplacement() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePlainURL() {
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg"
|
|
|
path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal(originURL, imageURL)
|
|
@@ -96,7 +136,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithoutExtension() {
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg"
|
|
|
path := fmt.Sprintf("/size:100:100/plain/%s", originURL)
|
|
|
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal(originURL, imageURL)
|
|
@@ -105,7 +145,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithoutExtension() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscaped() {
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/plain/%s@png", url.PathEscape(originURL))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal(originURL, imageURL)
|
|
@@ -113,26 +153,26 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscaped() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithBase() {
|
|
|
- config.BaseURL = "http://images.dev/"
|
|
|
+ s.config().BaseURL = "http://images.dev/"
|
|
|
|
|
|
originURL := "lorem/ipsum.jpg"
|
|
|
path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
- s.Require().Equal(fmt.Sprintf("%s%s", config.BaseURL, originURL), imageURL)
|
|
|
+ s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
|
|
|
s.Require().Equal(imagetype.PNG, po.Format)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithReplacement() {
|
|
|
- config.URLReplacements = []config.URLReplacement{
|
|
|
+ s.config().URLReplacements = []config.URLReplacement{
|
|
|
{Regexp: regexp.MustCompile("^test://([^/]*)/"), Replacement: "test2://images.dev/${1}/dolor/"},
|
|
|
{Regexp: regexp.MustCompile("^test2://"), Replacement: "http://"},
|
|
|
}
|
|
|
|
|
|
originURL := "test://lorem/ipsum.jpg"
|
|
|
path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
s.Require().Equal("http://images.dev/lorem/dolor/ipsum.jpg", imageURL)
|
|
@@ -140,22 +180,22 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithReplacement() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscapedWithBase() {
|
|
|
- config.BaseURL = "http://images.dev/"
|
|
|
+ s.config().BaseURL = "http://images.dev/"
|
|
|
|
|
|
originURL := "lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/size:100:100/plain/%s@png", url.PathEscape(originURL))
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
- s.Require().Equal(fmt.Sprintf("%s%s", config.BaseURL, originURL), imageURL)
|
|
|
+ s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
|
|
|
s.Require().Equal(imagetype.PNG, po.Format)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseWithArgumentsSeparator() {
|
|
|
- config.ArgumentsSeparator = ","
|
|
|
+ s.config().ArgumentsSeparator = ","
|
|
|
|
|
|
path := "/size,100,100,1/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -164,27 +204,9 @@ func (s *ProcessingOptionsTestSuite) TestParseWithArgumentsSeparator() {
|
|
|
s.Require().True(po.Enlarge)
|
|
|
}
|
|
|
|
|
|
-// func (s *ProcessingOptionsTestSuite) TestParseURLAllowedSource() {
|
|
|
-// config.AllowedSources = []string{"local://", "http://images.dev/"}
|
|
|
-
|
|
|
-// path := "/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
-// _, _, err := ParsePath(path, make(http.Header))
|
|
|
-
|
|
|
-// s.Require().NoError(err)
|
|
|
-// }
|
|
|
-
|
|
|
-// func (s *ProcessingOptionsTestSuite) TestParseURLNotAllowedSource() {
|
|
|
-// config.AllowedSources = []string{"local://", "http://images.dev/"}
|
|
|
-
|
|
|
-// path := "/plain/s3://images/lorem/ipsum.jpg"
|
|
|
-// _, _, err := ParsePath(path, make(http.Header))
|
|
|
-
|
|
|
-// s.Require().Error(err)
|
|
|
-// }
|
|
|
-
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathFormat() {
|
|
|
path := "/format:webp/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -193,7 +215,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathFormat() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathResize() {
|
|
|
path := "/resize:fill:100:200:1/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -205,7 +227,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathResize() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathResizingType() {
|
|
|
path := "/resizing_type:fill/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -214,7 +236,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathResizingType() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathSize() {
|
|
|
path := "/size:100:200:1/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -225,7 +247,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathSize() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWidth() {
|
|
|
path := "/width:100/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -234,7 +256,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWidth() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathHeight() {
|
|
|
path := "/height:100/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -243,7 +265,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathHeight() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathEnlarge() {
|
|
|
path := "/enlarge:1/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -252,7 +274,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathEnlarge() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathExtend() {
|
|
|
path := "/extend:1:so:10:20/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -264,21 +286,21 @@ func (s *ProcessingOptionsTestSuite) TestParsePathExtend() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathExtendSmartGravity() {
|
|
|
path := "/extend:1:sm/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().Error(err)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathExtendReplicateGravity() {
|
|
|
path := "/extend:1:re/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().Error(err)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathGravity() {
|
|
|
path := "/gravity:soea/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -287,7 +309,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathGravity() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathGravityFocusPoint() {
|
|
|
path := "/gravity:fp:0.5:0.75/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -298,14 +320,14 @@ func (s *ProcessingOptionsTestSuite) TestParsePathGravityFocusPoint() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathGravityReplicate() {
|
|
|
path := "/gravity:re/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().Error(err)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathCrop() {
|
|
|
path := "/crop:100:200/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -318,7 +340,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathCrop() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathCropGravity() {
|
|
|
path := "/crop:100:200:nowe:10:20/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -331,14 +353,14 @@ func (s *ProcessingOptionsTestSuite) TestParsePathCropGravity() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathCropGravityReplicate() {
|
|
|
path := "/crop:100:200:re/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().Error(err)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathQuality() {
|
|
|
path := "/quality:55/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -347,7 +369,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathQuality() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathBackground() {
|
|
|
path := "/background:128:129:130/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -359,7 +381,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathBackground() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundHex() {
|
|
|
path := "/background:ffddee/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -371,7 +393,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundHex() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundDisable() {
|
|
|
path := "/background:fff/background:/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -380,7 +402,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundDisable() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathBlur() {
|
|
|
path := "/blur:0.2/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -389,7 +411,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathBlur() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathSharpen() {
|
|
|
path := "/sharpen:0.2/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -397,7 +419,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathSharpen() {
|
|
|
}
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathDpr() {
|
|
|
path := "/dpr:2/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -405,7 +427,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathDpr() {
|
|
|
}
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWatermark() {
|
|
|
path := "/watermark:0.5:soea:10:20:0.6/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -417,17 +439,13 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWatermark() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathPreset() {
|
|
|
- presets["test1"] = urlOptions{
|
|
|
- urlOption{Name: "resizing_type", Args: []string{"fill"}},
|
|
|
- }
|
|
|
-
|
|
|
- presets["test2"] = urlOptions{
|
|
|
- urlOption{Name: "blur", Args: []string{"0.2"}},
|
|
|
- urlOption{Name: "quality", Args: []string{"50"}},
|
|
|
+ s.config().Presets = []string{
|
|
|
+ "test1=resizing_type:fill",
|
|
|
+ "test2=blur:0.2/quality:50",
|
|
|
}
|
|
|
|
|
|
path := "/preset:test1:test2/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -437,14 +455,12 @@ func (s *ProcessingOptionsTestSuite) TestParsePathPreset() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
|
|
|
- presets["default"] = urlOptions{
|
|
|
- urlOption{Name: "resizing_type", Args: []string{"fill"}},
|
|
|
- urlOption{Name: "blur", Args: []string{"0.2"}},
|
|
|
- urlOption{Name: "quality", Args: []string{"50"}},
|
|
|
+ s.config().Presets = []string{
|
|
|
+ "default=resizing_type:fill/blur:0.2/quality:50",
|
|
|
}
|
|
|
|
|
|
path := "/quality:70/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -454,18 +470,13 @@ func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathPresetLoopDetection() {
|
|
|
- presets["test1"] = urlOptions{
|
|
|
- urlOption{Name: "resizing_type", Args: []string{"fill"}},
|
|
|
- urlOption{Name: "preset", Args: []string{"test2"}},
|
|
|
- }
|
|
|
-
|
|
|
- presets["test2"] = urlOptions{
|
|
|
- urlOption{Name: "blur", Args: []string{"0.2"}},
|
|
|
- urlOption{Name: "preset", Args: []string{"test1"}},
|
|
|
+ s.config().Presets = []string{
|
|
|
+ "test1=resizing_type:fill/preset:test2",
|
|
|
+ "test2=blur:0.2/preset:test1",
|
|
|
}
|
|
|
|
|
|
path := "/preset:test1/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -474,7 +485,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathPresetLoopDetection() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathCachebuster() {
|
|
|
path := "/cachebuster:123/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -483,7 +494,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathCachebuster() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathStripMetadata() {
|
|
|
path := "/strip_metadata:true/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -491,11 +502,11 @@ func (s *ProcessingOptionsTestSuite) TestParsePathStripMetadata() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
|
|
|
- config.AutoWebp = true
|
|
|
+ s.config().AutoWebp = true
|
|
|
|
|
|
path := "/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
headers := http.Header{"Accept": []string{"image/webp"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -504,11 +515,11 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
|
|
|
- config.EnforceWebp = true
|
|
|
+ s.config().EnforceWebp = true
|
|
|
|
|
|
path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
|
|
|
headers := http.Header{"Accept": []string{"image/webp"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -517,11 +528,11 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
|
|
|
- config.EnableClientHints = true
|
|
|
+ s.config().EnableClientHints = true
|
|
|
|
|
|
path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
|
|
|
headers := http.Header{"Width": []string{"100"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -531,7 +542,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
|
|
|
path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
|
|
|
headers := http.Header{"Width": []string{"100"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -539,11 +550,11 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
|
|
|
- config.EnableClientHints = true
|
|
|
+ s.config().EnableClientHints = true
|
|
|
|
|
|
path := "/width:150/plain/http://images.dev/lorem/ipsum.jpg@png"
|
|
|
headers := http.Header{"Width": []string{"100"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -551,11 +562,11 @@ func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
|
|
|
- config.EnableClientHints = true
|
|
|
+ s.config().EnableClientHints = true
|
|
|
|
|
|
path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
|
|
|
headers := http.Header{"Dpr": []string{"2"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -565,7 +576,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
|
|
|
path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
|
|
|
headers := http.Header{"Dpr": []string{"2"}}
|
|
|
- po, _, err := ParsePath(path, headers)
|
|
|
+ po, _, err := s.factory().ParsePath(path, headers)
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -575,7 +586,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseSkipProcessing() {
|
|
|
path := "/skp:jpg:png/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -585,7 +596,7 @@ func (s *ProcessingOptionsTestSuite) TestParseSkipProcessing() {
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseSkipProcessingInvalid() {
|
|
|
path := "/skp:jpg:png:bad_format/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().Error(err)
|
|
|
s.Require().Equal("Invalid image format in skip processing: bad_format", err.Error())
|
|
@@ -593,30 +604,28 @@ func (s *ProcessingOptionsTestSuite) TestParseSkipProcessingInvalid() {
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseExpires() {
|
|
|
path := "/exp:32503669200/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseExpiresExpired() {
|
|
|
path := "/exp:1609448400/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().Error(err, "Expired URL")
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
|
|
|
- config.OnlyPresets = true
|
|
|
- presets["test1"] = urlOptions{
|
|
|
- urlOption{Name: "blur", Args: []string{"0.2"}},
|
|
|
- }
|
|
|
- presets["test2"] = urlOptions{
|
|
|
- urlOption{Name: "quality", Args: []string{"50"}},
|
|
|
+ s.config().OnlyPresets = true
|
|
|
+ s.config().Presets = []string{
|
|
|
+ "test1=blur:0.2",
|
|
|
+ "test2=quality:50",
|
|
|
}
|
|
|
|
|
|
path := "/test1:test2/plain/http://images.dev/lorem/ipsum.jpg"
|
|
|
|
|
|
- po, _, err := ParsePath(path, make(http.Header))
|
|
|
+ po, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -625,18 +634,16 @@ func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
|
|
|
- config.OnlyPresets = true
|
|
|
- presets["test1"] = urlOptions{
|
|
|
- urlOption{Name: "blur", Args: []string{"0.2"}},
|
|
|
- }
|
|
|
- presets["test2"] = urlOptions{
|
|
|
- urlOption{Name: "quality", Args: []string{"50"}},
|
|
|
+ s.config().OnlyPresets = true
|
|
|
+ s.config().Presets = []string{
|
|
|
+ "test1=blur:0.2",
|
|
|
+ "test2=quality:50",
|
|
|
}
|
|
|
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
|
|
|
path := fmt.Sprintf("/test1:test2/%s.png", base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
|
|
|
- po, imageURL, err := ParsePath(path, make(http.Header))
|
|
|
+ po, imageURL, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
@@ -646,12 +653,6 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
|
|
|
}
|
|
|
|
|
|
func (s *ProcessingOptionsTestSuite) TestParseAllowedOptions() {
|
|
|
- config.AllowedProcessingOptions = []string{"w", "h", "pr"}
|
|
|
-
|
|
|
- presets["test1"] = urlOptions{
|
|
|
- urlOption{Name: "blur", Args: []string{"0.2"}},
|
|
|
- }
|
|
|
-
|
|
|
originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
|
|
|
|
|
|
testCases := []struct {
|
|
@@ -666,8 +667,13 @@ func (s *ProcessingOptionsTestSuite) TestParseAllowedOptions() {
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
s.Run(strings.ReplaceAll(tc.options, "/", "_"), func() {
|
|
|
+ s.config().AllowedProcessingOptions = []string{"w", "h", "pr"}
|
|
|
+ s.config().Presets = []string{
|
|
|
+ "test1=blur:0.2",
|
|
|
+ }
|
|
|
+
|
|
|
path := fmt.Sprintf("/%s/%s.png", tc.options, base64.RawURLEncoding.EncodeToString([]byte(originURL)))
|
|
|
- _, _, err := ParsePath(path, make(http.Header))
|
|
|
+ _, _, err := s.factory().ParsePath(path, make(http.Header))
|
|
|
|
|
|
if len(tc.expectedError) > 0 {
|
|
|
s.Require().Error(err)
|
|
@@ -679,6 +685,23 @@ func (s *ProcessingOptionsTestSuite) TestParseAllowedOptions() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (s *ProcessingOptionsTestSuite) TestProcessingOptionsClone() {
|
|
|
+ now := time.Now()
|
|
|
+
|
|
|
+ // Create ProcessingOptions using factory
|
|
|
+ original := s.factory().NewProcessingOptions()
|
|
|
+ original.SkipProcessingFormats = []imagetype.Type{
|
|
|
+ imagetype.PNG, imagetype.JPEG,
|
|
|
+ }
|
|
|
+ original.UsedPresets = []string{"preset1", "preset2"}
|
|
|
+ original.Expires = &now
|
|
|
+
|
|
|
+ // Clone the original
|
|
|
+ cloned := original.clone()
|
|
|
+
|
|
|
+ testutil.EqualButNotSame(s.T(), original, cloned)
|
|
|
+}
|
|
|
+
|
|
|
func TestProcessingOptions(t *testing.T) {
|
|
|
suite.Run(t, new(ProcessingOptionsTestSuite))
|
|
|
}
|