path_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package processing
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/imgproxy/imgproxy/v3/ierrors"
  7. "github.com/stretchr/testify/suite"
  8. )
  9. type PathTestSuite struct {
  10. suite.Suite
  11. }
  12. func TestPathTestSuite(t *testing.T) {
  13. suite.Run(t, new(PathTestSuite))
  14. }
  15. func (s *PathTestSuite) createRequest(path string) *http.Request {
  16. return httptest.NewRequest("GET", path, nil)
  17. }
  18. func (s *PathTestSuite) TestParsePath() {
  19. testCases := []struct {
  20. name string
  21. pathPrefix string
  22. requestPath string
  23. expectedPath string
  24. expectedSig string
  25. expectedError bool
  26. }{
  27. {
  28. name: "BasicPath",
  29. requestPath: "/dummy_signature/rs:fill:300:200/plain/http://example.com/image.jpg",
  30. expectedPath: "rs:fill:300:200/plain/http://example.com/image.jpg",
  31. expectedSig: "dummy_signature",
  32. expectedError: false,
  33. },
  34. {
  35. name: "PathWithQueryParams",
  36. requestPath: "/dummy_signature/rs:fill:300:200/plain/http://example.com/image.jpg?param1=value1&param2=value2",
  37. expectedPath: "rs:fill:300:200/plain/http://example.com/image.jpg",
  38. expectedSig: "dummy_signature",
  39. expectedError: false,
  40. },
  41. {
  42. name: "PathWithPrefix",
  43. pathPrefix: "/imgproxy",
  44. requestPath: "/imgproxy/dummy_signature/rs:fill:300:200/plain/http://example.com/image.jpg",
  45. expectedPath: "rs:fill:300:200/plain/http://example.com/image.jpg",
  46. expectedSig: "dummy_signature",
  47. expectedError: false,
  48. },
  49. {
  50. name: "PathWithRedenormalization",
  51. requestPath: "/dummy_signature/rs:fill:300:200/plain/https:/example.com/path/to/image.jpg",
  52. expectedPath: "rs:fill:300:200/plain/https://example.com/path/to/image.jpg",
  53. expectedSig: "dummy_signature",
  54. expectedError: false,
  55. },
  56. {
  57. name: "NoSignatureSeparator",
  58. requestPath: "/invalid_path_without_slash",
  59. expectedPath: "",
  60. expectedSig: "",
  61. expectedError: true,
  62. },
  63. {
  64. name: "EmptyPath",
  65. requestPath: "/",
  66. expectedPath: "",
  67. expectedSig: "",
  68. expectedError: true,
  69. },
  70. {
  71. name: "OnlySignature",
  72. requestPath: "/signature_only",
  73. expectedPath: "",
  74. expectedSig: "",
  75. expectedError: true,
  76. },
  77. }
  78. for _, tc := range testCases {
  79. s.Run(tc.name, func() {
  80. config := &Config{
  81. PathPrefix: tc.pathPrefix,
  82. }
  83. req := s.createRequest(tc.requestPath)
  84. path, signature, err := splitPathSignature(req, config)
  85. if tc.expectedError {
  86. var ierr *ierrors.Error
  87. s.Require().Error(err)
  88. s.Require().ErrorAs(err, &ierr)
  89. s.Require().Equal(categoryPathParsing, ierr.Category())
  90. return
  91. }
  92. s.Require().NoError(err)
  93. s.Require().Equal(tc.expectedPath, path)
  94. s.Require().Equal(tc.expectedSig, signature)
  95. })
  96. }
  97. }
  98. func (s *PathTestSuite) TestRedenormalizePathHTTPProtocol() {
  99. testCases := []struct {
  100. name string
  101. input string
  102. expected string
  103. }{
  104. {
  105. name: "HTTP",
  106. input: "/plain/http:/example.com/image.jpg",
  107. expected: "/plain/http://example.com/image.jpg",
  108. },
  109. {
  110. name: "HTTPS",
  111. input: "/plain/https:/example.com/image.jpg",
  112. expected: "/plain/https://example.com/image.jpg",
  113. },
  114. {
  115. name: "Local",
  116. input: "/plain/local:/image.jpg",
  117. expected: "/plain/local:///image.jpg",
  118. },
  119. {
  120. name: "NormalizedPath",
  121. input: "/plain/http://example.com/image.jpg",
  122. expected: "/plain/http://example.com/image.jpg",
  123. },
  124. {
  125. name: "ProtocolMissing",
  126. input: "/rs:fill:300:200/plain/example.com/image.jpg",
  127. expected: "/rs:fill:300:200/plain/example.com/image.jpg",
  128. },
  129. {
  130. name: "EmptyString",
  131. input: "",
  132. expected: "",
  133. },
  134. {
  135. name: "SingleSlash",
  136. input: "/",
  137. expected: "/",
  138. },
  139. {
  140. name: "NoPlainPrefix",
  141. input: "/http:/example.com/image.jpg",
  142. expected: "/http:/example.com/image.jpg",
  143. },
  144. {
  145. name: "NoProtocol",
  146. input: "/plain/example.com/image.jpg",
  147. expected: "/plain/example.com/image.jpg",
  148. },
  149. {
  150. name: "EndsWithProtocol",
  151. input: "/plain/http:",
  152. expected: "/plain/http:",
  153. },
  154. {
  155. name: "OnlyProtocol",
  156. input: "/plain/http:/test",
  157. expected: "/plain/http://test",
  158. },
  159. }
  160. for _, tc := range testCases {
  161. s.Run(tc.name, func() {
  162. result := redenormalizePath(tc.input)
  163. s.Equal(tc.expected, result)
  164. })
  165. }
  166. }