svg_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package svg
  2. import (
  3. "net/http"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/suite"
  8. "go.withmatt.com/httpheaders"
  9. "github.com/imgproxy/imgproxy/v3/config"
  10. "github.com/imgproxy/imgproxy/v3/imagedata"
  11. "github.com/imgproxy/imgproxy/v3/testutil"
  12. )
  13. type SvgTestSuite struct {
  14. suite.Suite
  15. }
  16. func (s *SvgTestSuite) SetupSuite() {
  17. config.Reset()
  18. err := imagedata.Init()
  19. s.Require().NoError(err)
  20. }
  21. func (s *SvgTestSuite) readTestFile(name string) *imagedata.ImageData {
  22. wd, err := os.Getwd()
  23. s.Require().NoError(err)
  24. data, err := os.ReadFile(filepath.Join(wd, "..", "testdata", name))
  25. s.Require().NoError(err)
  26. h := make(http.Header)
  27. h.Set(httpheaders.ContentType, "image/svg+xml")
  28. h.Set(httpheaders.CacheControl, "public, max-age=12345")
  29. d, err := imagedata.NewFromBytes(data, h)
  30. s.Require().NoError(err)
  31. return d
  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().True(testutil.ReadersEqual(s.T(), expected.Reader(), actual.Reader()))
  39. s.Require().Equal(origin.Headers, actual.Headers)
  40. }
  41. func TestSvg(t *testing.T) {
  42. suite.Run(t, new(SvgTestSuite))
  43. }