svg_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package svg
  2. import (
  3. "bytes"
  4. "io"
  5. "testing"
  6. "github.com/stretchr/testify/suite"
  7. "github.com/imgproxy/imgproxy/v3/imagedata"
  8. "github.com/imgproxy/imgproxy/v3/imagetype"
  9. "github.com/imgproxy/imgproxy/v3/testutil"
  10. )
  11. type SvgTestSuite struct {
  12. suite.Suite
  13. testData *testutil.TestDataProvider
  14. }
  15. func (s *SvgTestSuite) SetupSuite() {
  16. s.testData = testutil.NewTestDataProvider(s.T)
  17. }
  18. func (s *SvgTestSuite) readTestFile(name string) imagedata.ImageData {
  19. data := s.testData.Read(name)
  20. return imagedata.NewFromBytesWithFormat(imagetype.SVG, data)
  21. }
  22. func (s *SvgTestSuite) compare(expected, actual imagedata.ImageData) {
  23. expectedData, err := io.ReadAll(expected.Reader())
  24. s.Require().NoError(err)
  25. actualData, err := io.ReadAll(actual.Reader())
  26. s.Require().NoError(err)
  27. // Trim whitespace to not care about ending newlines in test files
  28. expectedData = bytes.TrimSpace(expectedData)
  29. actualData = bytes.TrimSpace(actualData)
  30. s.Require().Equal(expectedData, actualData)
  31. }
  32. func (s *SvgTestSuite) TestSanitize() {
  33. origin := s.readTestFile("test1.svg")
  34. expected := s.readTestFile("test1.sanitized.svg")
  35. actual, err := Sanitize(origin)
  36. s.Require().NoError(err)
  37. s.compare(expected, actual)
  38. }
  39. func TestSvg(t *testing.T) {
  40. suite.Run(t, new(SvgTestSuite))
  41. }