summaryrefslogtreecommitdiffstats
path: root/routers/common/middleware_test.go
blob: f16b9374eca716def3d117557a19b99040905ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package common

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestStripSlashesMiddleware(t *testing.T) {
	type test struct {
		name         string
		expectedPath string
		inputPath    string
	}

	tests := []test{
		{
			name:         "path with multiple slashes",
			inputPath:    "https://github.com///go-gitea//gitea.git",
			expectedPath: "/go-gitea/gitea.git",
		},
		{
			name:         "path with no slashes",
			inputPath:    "https://github.com/go-gitea/gitea.git",
			expectedPath: "/go-gitea/gitea.git",
		},
		{
			name:         "path with slashes in the middle",
			inputPath:    "https://git.data.coop//halfd/new-website.git",
			expectedPath: "/halfd/new-website.git",
		},
		{
			name:         "path with slashes in the middle",
			inputPath:    "https://git.data.coop//halfd/new-website.git",
			expectedPath: "/halfd/new-website.git",
		},
		{
			name:         "path with slashes in the end",
			inputPath:    "/user2//repo1/",
			expectedPath: "/user2/repo1",
		},
		{
			name:         "path with slashes and query params",
			inputPath:    "/repo//migrate?service_type=3",
			expectedPath: "/repo/migrate",
		},
		{
			name:         "path with encoded slash",
			inputPath:    "/user2/%2F%2Frepo1",
			expectedPath: "/user2/%2F%2Frepo1",
		},
	}

	for _, tt := range tests {
		testMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			assert.Equal(t, tt.expectedPath, r.URL.Path)
		})

		// pass the test middleware to validate the changes
		handlerToTest := stripSlashesMiddleware(testMiddleware)
		// create a mock request to use
		req := httptest.NewRequest("GET", tt.inputPath, nil)
		// call the handler using a mock response recorder
		handlerToTest.ServeHTTP(httptest.NewRecorder(), req)
	}
}