svg_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package svg
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/suite"
  7. "github.com/imgproxy/imgproxy/v3/config"
  8. "github.com/imgproxy/imgproxy/v3/imagedata"
  9. "github.com/imgproxy/imgproxy/v3/imagetype"
  10. )
  11. type SvgTestSuite struct {
  12. suite.Suite
  13. }
  14. func (s *SvgTestSuite) SetupSuite() {
  15. config.Reset()
  16. err := imagedata.Init()
  17. s.Require().NoError(err)
  18. }
  19. func (s *SvgTestSuite) readTestFile(name string) *imagedata.ImageData {
  20. wd, err := os.Getwd()
  21. s.Require().NoError(err)
  22. data, err := os.ReadFile(filepath.Join(wd, "..", "testdata", name))
  23. s.Require().NoError(err)
  24. return &imagedata.ImageData{
  25. Type: imagetype.SVG,
  26. Data: data,
  27. Headers: map[string]string{
  28. "Content-Type": "image/svg+xml",
  29. "Cache-Control": "public, max-age=12345",
  30. },
  31. }
  32. }
  33. func (s *SvgTestSuite) TestSanitize() {
  34. origin := s.readTestFile("test1.svg")
  35. expected := s.readTestFile("test1.sanitized.svg")
  36. actual, err := Sanitize(origin)
  37. s.Require().NoError(err)
  38. s.Require().Equal(string(expected.Data), string(actual.Data))
  39. s.Require().Equal(origin.Headers, actual.Headers)
  40. }
  41. func TestSvg(t *testing.T) {
  42. suite.Run(t, new(SvgTestSuite))
  43. }