123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- package options
- import (
- "encoding/base64"
- "fmt"
- "net/http"
- "net/url"
- "regexp"
- "strings"
- "testing"
- "time"
- "github.com/imgproxy/imgproxy/v3/config"
- "github.com/imgproxy/imgproxy/v3/imagetype"
- "github.com/imgproxy/imgproxy/v3/options/keys"
- "github.com/imgproxy/imgproxy/v3/testutil"
- "github.com/imgproxy/imgproxy/v3/vips/color"
- "github.com/stretchr/testify/suite"
- )
- type ProcessingOptionsTestSuite struct {
- testutil.LazySuite
- config testutil.LazyObj[*Config]
- parser testutil.LazyObj[*Parser]
- }
- func (s *ProcessingOptionsTestSuite) SetupSuite() {
- s.config, _ = testutil.NewLazySuiteObj(
- s,
- func() (*Config, error) {
- c := NewDefaultConfig()
- return &c, nil
- },
- )
- s.parser, _ = testutil.NewLazySuiteObj(
- s,
- func() (*Parser, error) {
- return NewParser(s.config())
- },
- )
- }
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(originURL, imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithFilename() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(originURL, imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(originURL, imageURL)
- s.Require().Equal(imagetype.Unknown, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithReplacement() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal("http://images.dev/lorem/dolor/ipsum.jpg?param=value", imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePlainURL() {
- originURL := "http://images.dev/lorem/ipsum.jpg"
- path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
- po, imageURL, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(originURL, imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithoutExtension() {
- originURL := "http://images.dev/lorem/ipsum.jpg"
- path := fmt.Sprintf("/size:100:100/plain/%s", originURL)
- po, imageURL, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(originURL, imageURL)
- s.Require().Equal(imagetype.Unknown, Get(po, keys.Format, imagetype.Unknown))
- }
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(originURL, imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithBase() {
- s.config().BaseURL = "http://images.dev/"
- originURL := "lorem/ipsum.jpg"
- path := fmt.Sprintf("/size:100:100/plain/%s@png", originURL)
- po, imageURL, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithReplacement() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal("http://images.dev/lorem/dolor/ipsum.jpg", imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscapedWithBase() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(fmt.Sprintf("%s%s", s.config().BaseURL, originURL), imageURL)
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParseWithArgumentsSeparator() {
- s.config().ArgumentsSeparator = ","
- path := "/size,100,100,1/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(100, po.GetInt(keys.Width, 0))
- s.Require().Equal(100, po.GetInt(keys.Height, 0))
- s.Require().True(po.GetBool(keys.Enlarge, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathFormat() {
- path := "/format:webp/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(imagetype.WEBP, Get(po, keys.Format, imagetype.Unknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathResize() {
- path := "/resize:fill:100:200:1/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(ResizeFill, Get(po, keys.ResizingType, ResizeFit))
- s.Require().Equal(100, po.GetInt(keys.Width, 0))
- s.Require().Equal(200, po.GetInt(keys.Height, 0))
- s.Require().True(po.GetBool(keys.Enlarge, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathResizingType() {
- path := "/resizing_type:fill/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(ResizeFill, Get(po, keys.ResizingType, ResizeFit))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathSize() {
- path := "/size:100:200:1/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(100, po.GetInt(keys.Width, 0))
- s.Require().Equal(200, po.GetInt(keys.Height, 0))
- s.Require().True(po.GetBool(keys.Enlarge, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWidth() {
- path := "/width:100/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(100, po.GetInt(keys.Width, 0))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathHeight() {
- path := "/height:100/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(100, po.GetInt(keys.Height, 0))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathEnlarge() {
- path := "/enlarge:1/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.Enlarge, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathExtend() {
- path := "/extend:1:so:10:20/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.ExtendEnabled, false))
- s.Require().Equal(GravitySouth, Get(po, keys.ExtendGravityType, GravityUnknown))
- s.Require().InDelta(10.0, po.GetFloat(keys.ExtendGravityXOffset, 0.0), 0.0001)
- s.Require().InDelta(20.0, po.GetFloat(keys.ExtendGravityYOffset, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathExtendSmartGravity() {
- path := "/extend:1:sm/plain/http://images.dev/lorem/ipsum.jpg"
- _, _, err := s.parser().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 := s.parser().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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(GravitySouthEast, Get(po, keys.GravityType, GravityUnknown))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathGravityFocusPoint() {
- path := "/gravity:fp:0.5:0.75/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(GravityFocusPoint, Get(po, keys.GravityType, GravityUnknown))
- s.Require().InDelta(0.5, po.GetFloat(keys.GravityXOffset, 0.0), 0.0001)
- s.Require().InDelta(0.75, po.GetFloat(keys.GravityYOffset, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathGravityReplicate() {
- path := "/gravity:re/plain/http://images.dev/lorem/ipsum.jpg"
- _, _, err := s.parser().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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(100.0, po.GetFloat(keys.CropWidth, 0.0), 0.0001)
- s.Require().InDelta(200.0, po.GetFloat(keys.CropHeight, 0.0), 0.0001)
- s.Require().Equal(GravityUnknown, Get(po, keys.CropGravityType, GravityUnknown))
- s.Require().InDelta(0.0, po.GetFloat(keys.CropGravityXOffset, 0.0), 0.0001)
- s.Require().InDelta(0.0, po.GetFloat(keys.CropGravityYOffset, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathCropGravity() {
- path := "/crop:100:200:nowe:10:20/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(100.0, po.GetFloat(keys.CropWidth, 0.0), 0.0001)
- s.Require().InDelta(200.0, po.GetFloat(keys.CropHeight, 0.0), 0.0001)
- s.Require().Equal(GravityNorthWest, Get(po, keys.CropGravityType, GravityUnknown))
- s.Require().InDelta(10.0, po.GetFloat(keys.CropGravityXOffset, 0.0), 0.0001)
- s.Require().InDelta(20.0, po.GetFloat(keys.CropGravityYOffset, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathCropGravityReplicate() {
- path := "/crop:100:200:re/plain/http://images.dev/lorem/ipsum.jpg"
- _, _, err := s.parser().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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(55, po.GetInt(keys.Quality, 0))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathBackground() {
- path := "/background:128:129:130/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(
- color.RGB{R: 128, G: 129, B: 130},
- Get(po, keys.Background, color.RGB{}),
- )
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundHex() {
- path := "/background:ffddee/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(
- color.RGB{R: 0xff, G: 0xdd, B: 0xee},
- Get(po, keys.Background, color.RGB{}),
- )
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathBackgroundDisable() {
- path := "/background:fff/background:/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().False(po.Has(keys.Background))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathBlur() {
- path := "/blur:0.2/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(0.2, po.GetFloat(keys.Blur, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathSharpen() {
- path := "/sharpen:0.2/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(0.2, po.GetFloat(keys.Sharpen, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathDpr() {
- path := "/dpr:2/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(2.0, po.GetFloat(keys.Dpr, 1.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWatermark() {
- path := "/watermark:0.5:soea:10:20:0.6/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(0.5, po.GetFloat(keys.WatermarkOpacity, 0.0), 0.0001)
- s.Require().Equal(GravitySouthEast, Get(po, keys.WatermarkPosition, GravityUnknown))
- s.Require().InDelta(10.0, po.GetFloat(keys.WatermarkXOffset, 0.0), 0.0001)
- s.Require().InDelta(20.0, po.GetFloat(keys.WatermarkYOffset, 0.0), 0.0001)
- s.Require().InDelta(0.6, po.GetFloat(keys.WatermarkScale, 0.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathPreset() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(ResizeFill, Get(po, keys.ResizingType, ResizeFit))
- s.Require().InDelta(float32(0.2), po.GetFloat(keys.Blur, 0.0), 0.0001)
- s.Require().Equal(50, po.GetInt(keys.Quality, 0))
- s.Require().ElementsMatch([]string{"test1", "test2"}, Get(po, keys.UsedPresets, []string{}))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathPresetDefault() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(ResizeFill, Get(po, keys.ResizingType, ResizeFit))
- s.Require().InDelta(float32(0.2), po.GetFloat(keys.Blur, 0.0), 0.0001)
- s.Require().Equal(70, po.GetInt(keys.Quality, 0))
- s.Require().ElementsMatch([]string{"default"}, Get(po, keys.UsedPresets, []string{}))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathPresetLoopDetection() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().ElementsMatch([]string{"test1", "test2"}, Get(po, keys.UsedPresets, []string{}))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathCachebuster() {
- path := "/cachebuster:123/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal("123", Get(po, keys.CacheBuster, ""))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathStripMetadata() {
- path := "/strip_metadata:true/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.StripMetadata, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWebpDetection() {
- s.config().AutoWebp = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg"
- headers := http.Header{"Accept": []string{"image/webp"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.PreferWebP, false))
- s.Require().False(po.GetBool(keys.EnforceWebP, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWebpEnforce() {
- s.config().EnforceWebp = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Accept": []string{"image/webp"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.PreferWebP, false))
- s.Require().True(po.GetBool(keys.EnforceWebP, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathAvifDetection() {
- s.config().AutoAvif = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg"
- headers := http.Header{"Accept": []string{"image/avif"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.PreferAvif, false))
- s.Require().False(po.GetBool(keys.EnforceAvif, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathAvifEnforce() {
- s.config().EnforceAvif = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Accept": []string{"image/avif"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.PreferAvif, false))
- s.Require().True(po.GetBool(keys.EnforceAvif, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathJxlDetection() {
- s.config().AutoJxl = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg"
- headers := http.Header{"Accept": []string{"image/jxl"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.PreferJxl, false))
- s.Require().False(po.GetBool(keys.EnforceJxl, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathJxlEnforce() {
- s.config().EnforceJxl = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Accept": []string{"image/jxl"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().True(po.GetBool(keys.PreferJxl, false))
- s.Require().True(po.GetBool(keys.EnforceJxl, false))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeader() {
- s.config().EnableClientHints = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Width": []string{"100"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().Equal(100, po.GetInt(keys.Width, 0))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderDisabled() {
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Width": []string{"100"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().Equal(0, po.GetInt(keys.Width, 0))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWidthHeaderRedefine() {
- s.config().EnableClientHints = true
- path := "/width:150/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Width": []string{"100"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().Equal(150, po.GetInt(keys.Width, 0))
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathDprHeader() {
- s.config().EnableClientHints = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Dpr": []string{"2"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().InDelta(2.0, po.GetFloat(keys.Dpr, 1.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathDprHeaderDisabled() {
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{"Dpr": []string{"2"}}
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().InDelta(1.0, po.GetFloat(keys.Dpr, 1.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathWidthAndDprHeaderCombined() {
- s.config().EnableClientHints = true
- path := "/plain/http://images.dev/lorem/ipsum.jpg@png"
- headers := http.Header{
- "Width": []string{"100"},
- "Dpr": []string{"2"},
- }
- po, _, err := s.parser().ParsePath(path, headers)
- s.Require().NoError(err)
- s.Require().Equal(50, po.GetInt(keys.Width, 0))
- s.Require().InDelta(2.0, po.GetFloat(keys.Dpr, 1.0), 0.0001)
- }
- func (s *ProcessingOptionsTestSuite) TestParseSkipProcessing() {
- path := "/skp:jpg:png/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().ElementsMatch(
- []imagetype.Type{imagetype.JPEG, imagetype.PNG},
- Get(po, keys.SkipProcessing, []imagetype.Type(nil)),
- )
- }
- func (s *ProcessingOptionsTestSuite) TestParseSkipProcessingInvalid() {
- path := "/skp:jpg:png:bad_format/plain/http://images.dev/lorem/ipsum.jpg"
- _, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().Error(err)
- s.Require().Equal("Invalid image format in skip_processing: bad_format", err.Error())
- }
- func (s *ProcessingOptionsTestSuite) TestParseExpires() {
- path := "/exp:32503669200/plain/http://images.dev/lorem/ipsum.jpg"
- po, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().Equal(time.Unix(32503669200, 0), po.GetTime(keys.Expires))
- }
- func (s *ProcessingOptionsTestSuite) TestParseExpiresExpired() {
- path := "/exp:1609448400/plain/http://images.dev/lorem/ipsum.jpg"
- _, _, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().Error(err, "Expired URL")
- }
- func (s *ProcessingOptionsTestSuite) TestParsePathOnlyPresets() {
- s.config().OnlyPresets = true
- s.config().Presets = []string{
- "test1=blur:0.2",
- "test2=quality:50",
- }
- originURL := "http://images.dev/lorem/ipsum.jpg"
- path := "/test1:test2/plain/" + originURL + "@png"
- po, imageURL, err := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(0.2, po.GetFloat(keys.Blur, 0.0), 0.0001)
- s.Require().Equal(50, po.GetInt(keys.Quality, 0))
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- s.Require().Equal(originURL, imageURL)
- }
- func (s *ProcessingOptionsTestSuite) TestParseBase64URLOnlyPresets() {
- 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 := s.parser().ParsePath(path, make(http.Header))
- s.Require().NoError(err)
- s.Require().InDelta(0.2, po.GetFloat(keys.Blur, 0.0), 0.0001)
- s.Require().Equal(50, po.GetInt(keys.Quality, 0))
- s.Require().Equal(imagetype.PNG, Get(po, keys.Format, imagetype.Unknown))
- s.Require().Equal(originURL, imageURL)
- }
- func (s *ProcessingOptionsTestSuite) TestParseAllowedOptions() {
- originURL := "http://images.dev/lorem/ipsum.jpg?param=value"
- testCases := []struct {
- options string
- expectedError string
- }{
- {options: "w:100/h:200", expectedError: ""},
- {options: "w:100/h:200/blur:10", expectedError: "Forbidden processing option blur"},
- {options: "w:100/h:200/pr:test1", expectedError: ""},
- {options: "w:100/h:200/pr:test1/blur:10", expectedError: "Forbidden processing option blur"},
- }
- 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 := s.parser().ParsePath(path, make(http.Header))
- if len(tc.expectedError) > 0 {
- s.Require().Error(err)
- s.Require().Equal(tc.expectedError, err.Error())
- } else {
- s.Require().NoError(err)
- }
- })
- }
- }
- // func (s *ProcessingOptionsTestSuite) TestProcessingOptionsClone() {
- // now := time.Now()
- // // Create Options using parser
- // original := s.parser().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))
- }
|