path_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package handlers
  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. req := s.createRequest(tc.requestPath)
  81. req.Pattern = tc.pathPrefix
  82. path, signature, err := SplitPathSignature(req)
  83. if tc.expectedError {
  84. var ierr *ierrors.Error
  85. s.Require().Error(err)
  86. s.Require().ErrorAs(err, &ierr)
  87. s.Require().Equal(CategoryPathParsing, ierr.Category())
  88. return
  89. }
  90. s.Require().NoError(err)
  91. s.Require().Equal(tc.expectedPath, path)
  92. s.Require().Equal(tc.expectedSig, signature)
  93. })
  94. }
  95. }
  96. func (s *PathTestSuite) TestRedenormalizePathHTTPProtocol() {
  97. testCases := []struct {
  98. name string
  99. input string
  100. expected string
  101. }{
  102. {
  103. name: "HTTP",
  104. input: "/plain/http:/example.com/image.jpg",
  105. expected: "/plain/http://example.com/image.jpg",
  106. },
  107. {
  108. name: "HTTPS",
  109. input: "/plain/https:/example.com/image.jpg",
  110. expected: "/plain/https://example.com/image.jpg",
  111. },
  112. {
  113. name: "Local",
  114. input: "/plain/local:/image.jpg",
  115. expected: "/plain/local:///image.jpg",
  116. },
  117. {
  118. name: "NormalizedPath",
  119. input: "/plain/http://example.com/image.jpg",
  120. expected: "/plain/http://example.com/image.jpg",
  121. },
  122. {
  123. name: "ProtocolMissing",
  124. input: "/rs:fill:300:200/plain/example.com/image.jpg",
  125. expected: "/rs:fill:300:200/plain/example.com/image.jpg",
  126. },
  127. {
  128. name: "EmptyString",
  129. input: "",
  130. expected: "",
  131. },
  132. {
  133. name: "SingleSlash",
  134. input: "/",
  135. expected: "/",
  136. },
  137. {
  138. name: "NoPlainPrefix",
  139. input: "/http:/example.com/image.jpg",
  140. expected: "/http:/example.com/image.jpg",
  141. },
  142. {
  143. name: "NoProtocol",
  144. input: "/plain/example.com/image.jpg",
  145. expected: "/plain/example.com/image.jpg",
  146. },
  147. {
  148. name: "EndsWithProtocol",
  149. input: "/plain/http:",
  150. expected: "/plain/http:",
  151. },
  152. {
  153. name: "OnlyProtocol",
  154. input: "/plain/http:/test",
  155. expected: "/plain/http://test",
  156. },
  157. }
  158. for _, tc := range testCases {
  159. s.Run(tc.name, func() {
  160. result := redenormalizePath(tc.input)
  161. s.Equal(tc.expected, result)
  162. })
  163. }
  164. }