-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils_test.go
More file actions
331 lines (275 loc) · 7.99 KB
/
utils_test.go
File metadata and controls
331 lines (275 loc) · 7.99 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
package ming
import (
"testing"
"github.com/valyala/fasthttp"
)
func TestGetMethod(t *testing.T) {
tests := []struct {
name string
method string
expected string
}{
{"GET", fasthttp.MethodGet, fasthttp.MethodGet},
{"POST", fasthttp.MethodPost, fasthttp.MethodPost},
{"PUT", fasthttp.MethodPut, fasthttp.MethodPut},
{"DELETE", fasthttp.MethodDelete, fasthttp.MethodDelete},
{"PATCH", fasthttp.MethodPatch, fasthttp.MethodPatch},
{"HEAD", fasthttp.MethodHead, fasthttp.MethodHead},
{"OPTIONS", fasthttp.MethodOptions, fasthttp.MethodOptions},
{"CONNECT", fasthttp.MethodConnect, fasthttp.MethodConnect},
{"TRACE", fasthttp.MethodTrace, fasthttp.MethodTrace},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(tc.method)
result := GetMethod(ctx)
if result != tc.expected {
t.Errorf("GetMethod() for %s = %s, want %s", tc.method, result, tc.expected)
}
})
}
}
func TestGetMethodUnknown(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
// Set a custom method that's not in our switch statement
ctx.Request.Header.SetMethod("CUSTOM")
result := GetMethod(ctx)
if result != "" {
t.Errorf("GetMethod() for unknown method = %s, want empty string", result)
}
}
func TestGetMethodWithCustomMethod(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("CUSTOM")
result := GetMethod(ctx)
if result != "" {
t.Errorf("GetMethod() for custom method = %s, want empty string", result)
}
}
func TestQuery(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("http://example.com/path?name=john&age=30&city=")
tests := []struct {
key string
expected string
}{
{"name", "john"},
{"age", "30"},
{"city", ""},
{"nonexistent", ""},
}
for _, tc := range tests {
t.Run(tc.key, func(t *testing.T) {
result := string(Query(ctx, tc.key))
if result != tc.expected {
t.Errorf("Query(%s) = %s, want %s", tc.key, result, tc.expected)
}
})
}
}
func TestQueryWithMultipleValues(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("http://example.com/path?tags=go&tags=web&tags=router")
// Query should return the first value when multiple values exist
result := string(Query(ctx, "tags"))
if result != "go" {
t.Errorf("Query(tags) with multiple values = %s, want go", result)
}
}
func TestSetHeader(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
tests := []struct {
key string
value string
}{
{"Content-Type", "application/json"},
{"X-Custom-Header", "custom-value"},
{"Authorization", "Bearer token123"},
}
for _, tc := range tests {
t.Run(tc.key, func(t *testing.T) {
SetHeader(ctx, tc.key, tc.value)
result := string(ctx.Response.Header.Peek(tc.key))
if result != tc.value {
t.Errorf("SetHeader(%s, %s) result = %s, want %s", tc.key, tc.value, result, tc.value)
}
})
}
}
func TestSetHeaderOverwrite(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
// Set initial value
SetHeader(ctx, "Content-Type", "text/plain")
result1 := string(ctx.Response.Header.Peek("Content-Type"))
if result1 != "text/plain" {
t.Errorf("First SetHeader(Content-Type) = %s, want text/plain", result1)
}
// Overwrite with new value
SetHeader(ctx, "Content-Type", "application/json")
result2 := string(ctx.Response.Header.Peek("Content-Type"))
if result2 != "application/json" {
t.Errorf("Second SetHeader(Content-Type) = %s, want application/json", result2)
}
}
func TestBody(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
tests := []struct {
name string
body string
}{
{"empty body", ""},
{"json body", `{"name":"john","age":30}`},
{"plain text", "Hello, World!"},
{"binary data", "\x00\x01\x02\x03"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx.Request.SetBody([]byte(tc.body))
result := Body(ctx)
if string(result) != tc.body {
t.Errorf("Body() = %s, want %s", string(result), tc.body)
}
})
}
}
func TestBodyWithLargePayload(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
// Create a large body (1MB)
largeBody := make([]byte, 1024*1024)
for i := range largeBody {
largeBody[i] = byte(i % 256)
}
ctx.Request.SetBody(largeBody)
result := Body(ctx)
if len(result) != len(largeBody) {
t.Errorf("Body() length = %d, want %d", len(result), len(largeBody))
}
// Verify content
for i := 0; i < len(result); i++ {
if result[i] != largeBody[i] {
t.Errorf("Body() content mismatch at index %d: got %d, want %d", i, result[i], largeBody[i])
break
}
}
}
func TestUserValue(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
tests := []struct {
key string
value interface{}
}{
{"string_param", "test_value"},
{"int_param", 42},
{"bool_param", true},
{"nil_param", nil},
{"struct_param", struct{ Name string }{"test"}},
}
for _, tc := range tests {
t.Run(tc.key, func(t *testing.T) {
// Set the user value
ctx.SetUserValue(tc.key, tc.value)
// Get it back using UserValue
result := UserValue(ctx, tc.key)
if result != tc.value {
t.Errorf("UserValue(%s) = %v, want %v", tc.key, result, tc.value)
}
})
}
}
func TestUserValueNonexistent(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
result := UserValue(ctx, "nonexistent")
if result != nil {
t.Errorf("UserValue(nonexistent) = %v, want nil", result)
}
}
func TestParam(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
tests := []struct {
key string
value string
}{
{"id", "123"},
{"name", "john"},
{"category", "electronics"},
{"empty", ""},
}
for _, tc := range tests {
t.Run(tc.key, func(t *testing.T) {
// Set the parameter as user value
ctx.SetUserValue(tc.key, tc.value)
// Get it back using Param
result := Param(ctx, tc.key)
if result != tc.value {
t.Errorf("Param(%s) = %s, want %s", tc.key, result, tc.value)
}
})
}
}
func TestParamNonexistent(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
result := Param(ctx, "nonexistent")
if result != "" {
t.Errorf("Param(nonexistent) = %s, want empty string", result)
}
}
func TestParamWithNonStringValue(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
// Set a non-string value
ctx.SetUserValue("number", 42)
// This should cause a panic due to type assertion
defer func() {
if r := recover(); r == nil {
t.Error("Param() should panic when value is not a string")
}
}()
Param(ctx, "number")
}
func TestUtilityFunctionsIntegration(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
// Setup request
ctx.Request.SetRequestURI("http://example.com/users/123?format=json&detailed=true")
ctx.Request.Header.SetMethod("POST")
ctx.Request.SetBody([]byte(`{"name":"john","email":"john@example.com"}`))
// Set some parameters
ctx.SetUserValue("id", "123")
ctx.SetUserValue("action", "update")
// Test all utility functions together
method := GetMethod(ctx)
if method != fasthttp.MethodPost {
t.Errorf("GetMethod() = %s, want %s", method, fasthttp.MethodPost)
}
format := string(Query(ctx, "format"))
if format != "json" {
t.Errorf("Query(format) = %s, want json", format)
}
detailed := string(Query(ctx, "detailed"))
if detailed != "true" {
t.Errorf("Query(detailed) = %s, want true", detailed)
}
id := Param(ctx, "id")
if id != "123" {
t.Errorf("Param(id) = %s, want 123", id)
}
action := UserValue(ctx, "action")
if action != "update" {
t.Errorf("UserValue(action) = %v, want update", action)
}
body := Body(ctx)
expectedBody := `{"name":"john","email":"john@example.com"}`
if string(body) != expectedBody {
t.Errorf("Body() = %s, want %s", string(body), expectedBody)
}
// Test setting headers
SetHeader(ctx, "Content-Type", "application/json")
SetHeader(ctx, "X-Request-ID", "req-123")
contentType := string(ctx.Response.Header.Peek("Content-Type"))
if contentType != "application/json" {
t.Errorf("Response Content-Type = %s, want application/json", contentType)
}
requestId := string(ctx.Response.Header.Peek("X-Request-ID"))
if requestId != "req-123" {
t.Errorf("Response X-Request-ID = %s, want req-123", requestId)
}
}