|
@@ -1,53 +1,54 @@
|
|
|
package svg
|
|
|
|
|
|
import (
|
|
|
- "os"
|
|
|
- "path/filepath"
|
|
|
+ "bytes"
|
|
|
+ "io"
|
|
|
"testing"
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
- "github.com/imgproxy/imgproxy/v3/config"
|
|
|
- "github.com/imgproxy/imgproxy/v3/fetcher"
|
|
|
"github.com/imgproxy/imgproxy/v3/imagedata"
|
|
|
+ "github.com/imgproxy/imgproxy/v3/imagetype"
|
|
|
"github.com/imgproxy/imgproxy/v3/testutil"
|
|
|
)
|
|
|
|
|
|
type SvgTestSuite struct {
|
|
|
- idf *imagedata.Factory
|
|
|
suite.Suite
|
|
|
+
|
|
|
+ testData *testutil.TestDataProvider
|
|
|
}
|
|
|
|
|
|
func (s *SvgTestSuite) SetupSuite() {
|
|
|
- config.Reset()
|
|
|
-
|
|
|
- c := fetcher.NewDefaultConfig()
|
|
|
- f, err := fetcher.New(&c)
|
|
|
- s.Require().NoError(err)
|
|
|
-
|
|
|
- s.idf = imagedata.NewFactory(f)
|
|
|
+ s.testData = testutil.NewTestDataProvider(s.T)
|
|
|
}
|
|
|
|
|
|
func (s *SvgTestSuite) readTestFile(name string) imagedata.ImageData {
|
|
|
- wd, err := os.Getwd()
|
|
|
- s.Require().NoError(err)
|
|
|
+ data := s.testData.Read(name)
|
|
|
+ return imagedata.NewFromBytesWithFormat(imagetype.SVG, data)
|
|
|
+}
|
|
|
|
|
|
- data, err := os.ReadFile(filepath.Join(wd, "..", "testdata", name))
|
|
|
+func (s *SvgTestSuite) compare(expected, actual imagedata.ImageData) {
|
|
|
+ expectedData, err := io.ReadAll(expected.Reader())
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
- d, err := s.idf.NewFromBytes(data)
|
|
|
+ actualData, err := io.ReadAll(actual.Reader())
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
- return d
|
|
|
+ // Trim whitespace to not care about ending newlines in test files
|
|
|
+ expectedData = bytes.TrimSpace(expectedData)
|
|
|
+ actualData = bytes.TrimSpace(actualData)
|
|
|
+
|
|
|
+ s.Require().Equal(expectedData, actualData)
|
|
|
}
|
|
|
|
|
|
func (s *SvgTestSuite) TestSanitize() {
|
|
|
origin := s.readTestFile("test1.svg")
|
|
|
expected := s.readTestFile("test1.sanitized.svg")
|
|
|
- actual, err := Sanitize(origin)
|
|
|
|
|
|
+ actual, err := Sanitize(origin)
|
|
|
s.Require().NoError(err)
|
|
|
- s.Require().True(testutil.ReadersEqual(s.T(), expected.Reader(), actual.Reader()))
|
|
|
+
|
|
|
+ s.compare(expected, actual)
|
|
|
}
|
|
|
|
|
|
func TestSvg(t *testing.T) {
|