-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_test.go
More file actions
344 lines (299 loc) · 10.2 KB
/
Copy pathrequest_test.go
File metadata and controls
344 lines (299 loc) · 10.2 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package http2
import (
"testing"
)
// TestStatusCodeConstants validates HTTP status code constants
func TestStatusCodeConstants(t *testing.T) {
tests := []struct {
name string
constant int
expected int
}{
// 1xx Informational
{"StatusContinue", StatusContinue, 100},
{"StatusSwitchingProtocols", StatusSwitchingProtocols, 101},
{"StatusProcessing", StatusProcessing, 102},
{"StatusEarlyHints", StatusEarlyHints, 103},
// 2xx Success
{"StatusOK", StatusOK, 200},
{"StatusCreated", StatusCreated, 201},
{"StatusAccepted", StatusAccepted, 202},
{"StatusNonAuthoritativeInfo", StatusNonAuthoritativeInfo, 203},
{"StatusNoContent", StatusNoContent, 204},
{"StatusResetContent", StatusResetContent, 205},
{"StatusPartialContent", StatusPartialContent, 206},
{"StatusMultiStatus", StatusMultiStatus, 207},
{"StatusAlreadyReported", StatusAlreadyReported, 208},
{"StatusIMUsed", StatusIMUsed, 226},
// 3xx Redirection
{"StatusMultipleChoices", StatusMultipleChoices, 300},
{"StatusMovedPermanently", StatusMovedPermanently, 301},
{"StatusFound", StatusFound, 302},
{"StatusSeeOther", StatusSeeOther, 303},
{"StatusNotModified", StatusNotModified, 304},
{"StatusUseProxy", StatusUseProxy, 305},
{"StatusTemporaryRedirect", StatusTemporaryRedirect, 307},
{"StatusPermanentRedirect", StatusPermanentRedirect, 308},
// 4xx Client Error
{"StatusBadRequest", StatusBadRequest, 400},
{"StatusUnauthorized", StatusUnauthorized, 401},
{"StatusPaymentRequired", StatusPaymentRequired, 402},
{"StatusForbidden", StatusForbidden, 403},
{"StatusNotFound", StatusNotFound, 404},
{"StatusMethodNotAllowed", StatusMethodNotAllowed, 405},
{"StatusNotAcceptable", StatusNotAcceptable, 406},
{"StatusProxyAuthRequired", StatusProxyAuthRequired, 407},
{"StatusRequestTimeout", StatusRequestTimeout, 408},
{"StatusConflict", StatusConflict, 409},
{"StatusGone", StatusGone, 410},
{"StatusLengthRequired", StatusLengthRequired, 411},
{"StatusPreconditionFailed", StatusPreconditionFailed, 412},
{"StatusRequestEntityTooLarge", StatusRequestEntityTooLarge, 413},
{"StatusRequestURITooLong", StatusRequestURITooLong, 414},
{"StatusUnsupportedMediaType", StatusUnsupportedMediaType, 415},
{"StatusRequestedRangeNotSatisfiable", StatusRequestedRangeNotSatisfiable, 416},
{"StatusExpectationFailed", StatusExpectationFailed, 417},
{"StatusTeapot", StatusTeapot, 418},
{"StatusMisdirectedRequest", StatusMisdirectedRequest, 421},
{"StatusUnprocessableEntity", StatusUnprocessableEntity, 422},
{"StatusLocked", StatusLocked, 423},
{"StatusFailedDependency", StatusFailedDependency, 424},
{"StatusTooEarly", StatusTooEarly, 425},
{"StatusUpgradeRequired", StatusUpgradeRequired, 426},
{"StatusPreconditionRequired", StatusPreconditionRequired, 428},
{"StatusTooManyRequests", StatusTooManyRequests, 429},
{"StatusRequestHeaderFieldsTooLarge", StatusRequestHeaderFieldsTooLarge, 431},
{"StatusUnavailableForLegalReasons", StatusUnavailableForLegalReasons, 451},
// 5xx Server Error
{"StatusInternalServerError", StatusInternalServerError, 500},
{"StatusNotImplemented", StatusNotImplemented, 501},
{"StatusBadGateway", StatusBadGateway, 502},
{"StatusServiceUnavailable", StatusServiceUnavailable, 503},
{"StatusGatewayTimeout", StatusGatewayTimeout, 504},
{"StatusHTTPVersionNotSupported", StatusHTTPVersionNotSupported, 505},
{"StatusVariantAlsoNegotiates", StatusVariantAlsoNegotiates, 506},
{"StatusInsufficientStorage", StatusInsufficientStorage, 507},
{"StatusLoopDetected", StatusLoopDetected, 508},
{"StatusNotExtended", StatusNotExtended, 510},
{"StatusNetworkAuthenticationRequired", StatusNetworkAuthenticationRequired, 511},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.constant != tt.expected {
t.Errorf("%s = %d, want %d", tt.name, tt.constant, tt.expected)
}
})
}
}
// TestNewRequest tests request creation
func TestNewRequest(t *testing.T) {
tests := []struct {
name string
method string
path string
wantMethod string
wantPath string
}{
{"GET request", "GET", "/api/users", "GET", "/api/users"},
{"POST request", "POST", "/api/data", "POST", "/api/data"},
{"PUT request", "PUT", "/api/resource/123", "PUT", "/api/resource/123"},
{"DELETE request", "DELETE", "/api/resource/456", "DELETE", "/api/resource/456"},
{"Root path", "GET", "/", "GET", "/"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := NewRequest(tt.method, tt.path)
if req.Method != tt.wantMethod {
t.Errorf("Method = %s, want %s", req.Method, tt.wantMethod)
}
if req.Path != tt.wantPath {
t.Errorf("Path = %s, want %s", req.Path, tt.wantPath)
}
if req.Headers == nil {
t.Error("Headers map should be initialized")
}
if len(req.Headers) != 0 {
t.Errorf("Headers should be empty, got %d headers", len(req.Headers))
}
})
}
}
// TestRequestWithHeader tests adding a single header
func TestRequestWithHeader(t *testing.T) {
req := NewRequest("GET", "/test")
// Add first header
req = req.WithHeader("content-type", "application/json")
if req.Headers["content-type"] != "application/json" {
t.Error("Expected content-type header to be set")
}
// Add second header
req = req.WithHeader("user-agent", "http2-client/1.0")
if req.Headers["user-agent"] != "http2-client/1.0" {
t.Error("Expected user-agent header to be set")
}
// Override existing header
req = req.WithHeader("content-type", "text/plain")
if req.Headers["content-type"] != "text/plain" {
t.Error("Expected content-type header to be overridden")
}
if len(req.Headers) != 2 {
t.Errorf("Expected 2 headers, got %d", len(req.Headers))
}
}
// TestRequestWithHeaderNilMap tests adding header when map is nil
func TestRequestWithHeaderNilMap(t *testing.T) {
req := &Request{
Method: "GET",
Path: "/test",
Headers: nil, // Explicitly nil
}
req = req.WithHeader("test-header", "test-value")
if req.Headers == nil {
t.Error("Headers map should be initialized")
}
if req.Headers["test-header"] != "test-value" {
t.Error("Expected test-header to be set")
}
}
// TestRequestAddHeaders tests adding multiple headers
func TestRequestAddHeaders(t *testing.T) {
req := NewRequest("POST", "/api/data")
headers := map[string]string{
"content-type": "application/json",
"content-length": "256",
"authorization": "Bearer token123",
}
req = req.AddHeaders(headers)
for name, value := range headers {
if req.Headers[name] != value {
t.Errorf("Expected header %s = %s, got %s", name, value, req.Headers[name])
}
}
if len(req.Headers) != 3 {
t.Errorf("Expected 3 headers, got %d", len(req.Headers))
}
}
// TestRequestAddHeadersNilMap tests adding headers when map is nil
func TestRequestAddHeadersNilMap(t *testing.T) {
req := &Request{
Method: "GET",
Path: "/test",
Headers: nil,
}
headers := map[string]string{
"header1": "value1",
"header2": "value2",
}
req = req.AddHeaders(headers)
if req.Headers == nil {
t.Error("Headers map should be initialized")
}
if len(req.Headers) != 2 {
t.Errorf("Expected 2 headers, got %d", len(req.Headers))
}
}
// TestRequestWithBody tests setting request body
func TestRequestWithBody(t *testing.T) {
req := NewRequest("POST", "/api/data")
body := []byte(`{"name": "test", "value": 123}`)
req = req.WithBody(body)
if string(req.Body) != string(body) {
t.Errorf("Body = %s, want %s", req.Body, body)
}
}
// TestRequestWithAuthority tests setting authority pseudo-header
func TestRequestWithAuthority(t *testing.T) {
req := NewRequest("GET", "/api/users")
authority := "api.example.com"
req = req.WithAuthority(authority)
if req.Authority != authority {
t.Errorf("Authority = %s, want %s", req.Authority, authority)
}
}
// TestRequestWithScheme tests setting scheme pseudo-header
func TestRequestWithScheme(t *testing.T) {
req := NewRequest("GET", "/secure")
scheme := "https"
req = req.WithScheme(scheme)
if req.Scheme != scheme {
t.Errorf("Scheme = %s, want %s", req.Scheme, scheme)
}
}
// TestRequestMethodChaining tests chaining multiple request methods
func TestRequestMethodChaining(t *testing.T) {
req := NewRequest("POST", "/api/resource").
WithAuthority("api.example.com").
WithScheme("https").
WithHeader("content-type", "application/json").
WithHeader("authorization", "Bearer token123").
WithBody([]byte(`{"data": "test"}`))
if req.Method != "POST" {
t.Errorf("Method = %s, want POST", req.Method)
}
if req.Path != "/api/resource" {
t.Errorf("Path = %s, want /api/resource", req.Path)
}
if req.Authority != "api.example.com" {
t.Errorf("Authority = %s, want api.example.com", req.Authority)
}
if req.Scheme != "https" {
t.Errorf("Scheme = %s, want https", req.Scheme)
}
if len(req.Headers) != 2 {
t.Errorf("Expected 2 headers, got %d", len(req.Headers))
}
if req.Body == nil {
t.Error("Expected body to be set")
}
}
// TestResponse tests response structure
func TestResponse(t *testing.T) {
resp := &Response{
Status: "200 OK",
StatusCode: 200,
Headers: map[string]string{
"content-type": "application/json",
"content-length": "128",
},
Body: []byte(`{"result": "success"}`),
}
if resp.Status != "200 OK" {
t.Errorf("Status = %s, want 200 OK", resp.Status)
}
if resp.StatusCode != 200 {
t.Errorf("StatusCode = %d, want 200", resp.StatusCode)
}
if resp.Headers["content-type"] != "application/json" {
t.Error("Expected content-type header")
}
if resp.Body == nil {
t.Error("Expected body to be set")
}
}
// BenchmarkNewRequest benchmarks request creation
func BenchmarkNewRequest(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = NewRequest("GET", "/api/test")
}
}
// BenchmarkRequestWithHeader benchmarks adding headers
func BenchmarkRequestWithHeader(b *testing.B) {
req := NewRequest("GET", "/test")
b.ResetTimer()
for i := 0; i < b.N; i++ {
req = req.WithHeader("test-header", "test-value")
}
}
// BenchmarkRequestMethodChaining benchmarks chained method calls
func BenchmarkRequestMethodChaining(b *testing.B) {
body := []byte(`{"data": "test"}`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = NewRequest("POST", "/api/resource").
WithAuthority("api.example.com").
WithScheme("https").
WithHeader("content-type", "application/json").
WithBody(body)
}
}