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