You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware_test.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package common
  4. import (
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestStripSlashesMiddleware(t *testing.T) {
  11. type test struct {
  12. name string
  13. expectedPath string
  14. inputPath string
  15. }
  16. tests := []test{
  17. {
  18. name: "path with multiple slashes",
  19. inputPath: "https://github.com///go-gitea//gitea.git",
  20. expectedPath: "/go-gitea/gitea.git",
  21. },
  22. {
  23. name: "path with no slashes",
  24. inputPath: "https://github.com/go-gitea/gitea.git",
  25. expectedPath: "/go-gitea/gitea.git",
  26. },
  27. {
  28. name: "path with slashes in the middle",
  29. inputPath: "https://git.data.coop//halfd/new-website.git",
  30. expectedPath: "/halfd/new-website.git",
  31. },
  32. {
  33. name: "path with slashes in the middle",
  34. inputPath: "https://git.data.coop//halfd/new-website.git",
  35. expectedPath: "/halfd/new-website.git",
  36. },
  37. {
  38. name: "path with slashes in the end",
  39. inputPath: "/user2//repo1/",
  40. expectedPath: "/user2/repo1",
  41. },
  42. {
  43. name: "path with slashes and query params",
  44. inputPath: "/repo//migrate?service_type=3",
  45. expectedPath: "/repo/migrate",
  46. },
  47. {
  48. name: "path with encoded slash",
  49. inputPath: "/user2/%2F%2Frepo1",
  50. expectedPath: "/user2/%2F%2Frepo1",
  51. },
  52. }
  53. for _, tt := range tests {
  54. testMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  55. assert.Equal(t, tt.expectedPath, r.URL.Path)
  56. })
  57. // pass the test middleware to validate the changes
  58. handlerToTest := stripSlashesMiddleware(testMiddleware)
  59. // create a mock request to use
  60. req := httptest.NewRequest("GET", tt.inputPath, nil)
  61. // call the handler using a mock response recorder
  62. handlerToTest.ServeHTTP(httptest.NewRecorder(), req)
  63. }
  64. }