Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
199 lines (186 loc) · 5.1 KB
/
Copy pathmain_test.go
File metadata and controls
199 lines (186 loc) · 5.1 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"crypto/tls"
"encoding/json/v2"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestServeHTTP_Paths(t *testing.T) {
tests := []struct {
name string
method string
target string
requestBody string
isTLS bool
expectedPath string
expectedQuery url.Values
expectedMethod string
expectedScheme string
}{
{
name: "Root path",
method: "GET",
target: "/",
expectedPath: "/",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Simple path",
method: "GET",
target: "/hello",
expectedPath: "/hello",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Deep path",
method: "GET",
target: "/some/deep/path/to/resource",
expectedPath: "/some/deep/path/to/resource",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Path with trailing slash",
method: "GET",
target: "/hello/",
expectedPath: "/hello/",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Path with query parameters",
method: "GET",
target: "/search?q=golang&limit=10",
expectedPath: "/search",
expectedQuery: url.Values{"q": []string{"golang"}, "limit": []string{"10"}},
expectedMethod: "GET",
},
{
name: "Path with special characters",
method: "POST",
target: "/api/v1/users/info@example.com/profile",
requestBody: `{"status":"active"}`,
expectedPath: "/api/v1/users/info@example.com/profile",
expectedQuery: url.Values{},
expectedMethod: "POST",
},
{
name: "Path with spaces (URL encoded)",
method: "GET",
target: "/files/my%20document.pdf",
expectedPath: "/files/my document.pdf",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Path with unicode characters",
method: "GET",
target: "/path/%F0%9F%8C%9F/star", // URL encoded 🌟
expectedPath: "/path/🌟/star",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Relative paths in target",
method: "GET",
target: "/a/b/../c",
expectedPath: "/a/b/../c",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "Consecutive slashes",
method: "GET",
target: "//foo//bar",
expectedPath: "//foo//bar",
expectedQuery: url.Values{},
expectedMethod: "GET",
},
{
name: "TLS Request / HTTPS scheme",
method: "GET",
target: "/secure-path",
isTLS: true,
expectedPath: "/secure-path",
expectedQuery: url.Values{},
expectedMethod: "GET",
expectedScheme: "https",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var bodyReader io.Reader
if tc.requestBody != "" {
bodyReader = strings.NewReader(tc.requestBody)
}
req := httptest.NewRequest(tc.method, tc.target, bodyReader)
if tc.isTLS {
req.TLS = &tls.ConnectionState{}
}
rec := httptest.NewRecorder()
ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected status OK, got %d", rec.Code)
}
contentType := rec.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("expected Content-Type application/json, got %s", contentType)
}
cacheControl := rec.Header().Get("Cache-Control")
if cacheControl != "no-cache" {
t.Errorf("expected Cache-Control no-cache, got %s", cacheControl)
}
var resp reqStruct
err := json.Unmarshal(rec.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal response: %v", err)
}
if resp.Method != tc.expectedMethod {
t.Errorf("expected method %q, got %q", tc.expectedMethod, resp.Method)
}
if resp.URL.Path != tc.expectedPath {
t.Errorf("expected path %q, got %q (URL: %s)", tc.expectedPath, resp.URL.Path, resp.URL.String())
}
expectedScheme := tc.expectedScheme
if expectedScheme == "" {
expectedScheme = "http"
}
if resp.URL.Scheme != expectedScheme {
t.Errorf("expected scheme %q, got %q", expectedScheme, resp.URL.Scheme)
}
// Compare queries
for k, expectedVals := range tc.expectedQuery {
actualVals, ok := resp.Query[k]
if !ok {
t.Errorf("expected query parameter %q not found", k)
continue
}
if len(expectedVals) != len(actualVals) {
t.Errorf("expected parameter %q to have %d values, got %d", k, len(expectedVals), len(actualVals))
continue
}
for i, v := range expectedVals {
if actualVals[i] != v {
t.Errorf("expected parameter %q index %d to be %q, got %q", k, i, v, actualVals[i])
}
}
}
})
}
}
// Make sure we have a type matching the response structure.
// Note: We need to adapt to jsonv2's unmarshaling of url.URL.
type reqStruct struct {
Method string
Headers http.Header
Body string
ParsedBody map[string]any
URL *url.URL
Query url.Values
}