1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2026-04-24 19:54:05 +02:00
Files
imgproxy/processing/suite_test.go
2026-02-03 13:10:28 +01:00

168 lines
4.3 KiB
Go

package processing
import (
"fmt"
"github.com/imgproxy/imgproxy/v3/auximageprovider"
"github.com/imgproxy/imgproxy/v3/fetcher"
"github.com/imgproxy/imgproxy/v3/imagedata"
"github.com/imgproxy/imgproxy/v3/logger"
"github.com/imgproxy/imgproxy/v3/options"
"github.com/imgproxy/imgproxy/v3/options/keys"
"github.com/imgproxy/imgproxy/v3/security"
"github.com/imgproxy/imgproxy/v3/testutil"
"github.com/imgproxy/imgproxy/v3/vips"
)
type testSuite struct {
testutil.LazySuite
TestData *testutil.TestDataProvider
ImageDataFactory testutil.LazyObj[*imagedata.Factory]
SecurityConfig testutil.LazyObj[*security.Config]
Security testutil.LazyObj[*security.Checker]
Config testutil.LazyObj[*Config]
WatermarkConfig testutil.LazyObj[*auximageprovider.StaticConfig]
WatermarkProvider testutil.LazyObj[auximageprovider.Provider]
Processor testutil.LazyObj[*Processor]
ImageMatcher testutil.LazyObj[*testutil.ImageHashCacheMatcher]
}
type optsFactory interface {
Set(o *options.Options)
String() string
}
type testCase[T optsFactory] struct {
opts T
outSize testSize
outInterpretation vips.Interpretation
}
type testCaseParams interface {
OutSize() testSize
OutInterpretation() vips.Interpretation
}
func (c testCase[T]) OutSize() testSize {
return c.outSize
}
func (c testCase[T]) OutInterpretation() vips.Interpretation {
return c.outInterpretation
}
type testSize struct {
width int
height int
}
func (c testSize) Set(o *options.Options) {
o.Set(keys.Width, c.width)
o.Set(keys.Height, c.height)
}
func (c testSize) String() string {
return fmt.Sprintf("%dx%d", c.width, c.height)
}
func (s *testSuite) SetupSuite() {
vipsCfg := vips.NewDefaultConfig()
s.Require().NoError(vips.Init(&vipsCfg))
logger.Mute()
s.TestData = testutil.NewTestDataProvider(s.T)
s.ImageDataFactory, _ = testutil.NewLazySuiteObj(s, func() (*imagedata.Factory, error) {
c := fetcher.NewDefaultConfig()
f, err := fetcher.New(&c)
if err != nil {
return nil, err
}
return imagedata.NewFactory(f, nil), nil
})
s.SecurityConfig, _ = testutil.NewLazySuiteObj(s, func() (*security.Config, error) {
c := security.NewDefaultConfig()
c.MaxSrcResolution = 10 * 1024 * 1024
c.MaxSrcFileSize = 10 * 1024 * 1024
c.MaxAnimationFrames = 100
c.MaxAnimationFrameResolution = 10 * 1024 * 1024
return &c, nil
})
s.Security, _ = testutil.NewLazySuiteObj(s, func() (*security.Checker, error) {
return security.New(s.SecurityConfig())
})
s.Config, _ = testutil.NewLazySuiteObj(s, func() (*Config, error) {
c := NewDefaultConfig()
return &c, nil
})
s.WatermarkConfig, _ = testutil.NewLazySuiteObj(s, func() (*auximageprovider.StaticConfig, error) {
return &auximageprovider.StaticConfig{
Path: s.TestData.Path("geometry.png"),
}, nil
})
s.WatermarkProvider, _ = testutil.NewLazySuiteObj(s, func() (auximageprovider.Provider, error) {
return auximageprovider.NewStaticProvider(
s.T().Context(),
s.WatermarkConfig(),
"watermark",
s.ImageDataFactory(),
)
})
s.Processor, _ = testutil.NewLazySuiteObj(s, func() (*Processor, error) {
return New(s.Config(), s.Security(), s.WatermarkProvider())
})
s.ImageMatcher, _ = testutil.NewLazySuiteObj(s, func() (*testutil.ImageHashCacheMatcher, error) {
return testutil.NewImageHashCacheMatcher(s.TestData, testutil.HashTypeSHA256), nil
})
}
func (s *testSuite) TearDownSuite() {
logger.Unmute()
}
func (s *testSuite) processImageAndCheck(
imgdata imagedata.ImageData,
o *options.Options,
tc testCaseParams,
) {
result, err := s.Processor().ProcessImage(s.T().Context(), imgdata, o)
s.Require().NoError(err)
s.Require().NotNil(result)
outSize := tc.OutSize()
outInterpretation := tc.OutInterpretation()
s.Require().Equal(result.ResultWidth, outSize.width, "Width mismatch")
s.Require().Equal(result.ResultHeight, outSize.height, "Height mismatch")
s.ImageMatcher().ImageMatches(s.T(), result.OutData.Reader(), "test", 0)
if outInterpretation == vips.InterpretationMultiBand {
return
}
// Load the result image to check its interpretation
resultImg := new(vips.Image)
defer resultImg.Clear()
err = resultImg.Load(result.OutData, 1, 1.0, 1)
s.Require().NoError(err)
// Check the interpretation
actualInterpretation := resultImg.Type()
s.Require().Equal(outInterpretation, actualInterpretation)
}