-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontext_test.go
277 lines (216 loc) · 7.03 KB
/
context_test.go
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
package pulse
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestContext_Write(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)
message := "Hello, world!"
n, err := ctx.Write([]byte(message))
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if n != len(message) {
t.Errorf("Expected %d bytes written, got %d", len(message), n)
}
if w.Body.String() != message {
t.Errorf("Response body does not match expected value. Expected: %s, got: %s", message, w.Body.String())
}
}
func TestContext_WithParams(t *testing.T) {
ctx := NewContext(nil, nil)
ctx.WithParams(map[string]string{"id": "1"})
if ctx.Params["id"] != "1" {
t.Errorf("Expected id to be 1, got %s", ctx.Params["id"])
}
}
func TestContext_Param(t *testing.T) {
ctx := NewContext(nil, nil)
ctx.WithParams(map[string]string{"id": "1"})
if ctx.Param("id") != "1" {
t.Errorf("Expected id to be 1, got %s", ctx.Param("id"))
}
}
func TestContext_Query(t *testing.T) {
ctx := NewContext(nil, nil)
ctx.Request = &http.Request{
URL: &url.URL{
RawQuery: "id=1",
},
}
if ctx.Query("id") != "1" {
t.Errorf("Expected id to be 1, got %s", ctx.Query("id"))
}
}
func TestContext_Abort(t *testing.T) {
ctx := NewContext(nil, nil)
ctx.Abort()
}
func TestContext_String(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)
message := "Hello, world!"
ctx.String(message)
if w.Body.String() != message {
t.Errorf("Response body does not match expected value. Expected: %s, got: %s", message, w.Body.String())
}
}
func TestContext_Cookie(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/", nil)
ctx := NewContext(w, r)
cookieName := "test-cookie"
cookieValue := "test-value"
// Set a cookie using the SetCookie method.
ctx.SetCookie(&Cookie{
Name: cookieName,
Value: cookieValue,
})
// Verify that the cookie was set correctly by checking the response header.
cookies := w.Header().Get("Set-Cookie")
if !strings.Contains(cookies, cookieName) {
t.Errorf("Expected response header to contain cookie name '%s'", cookieName)
}
if !strings.Contains(cookies, cookieValue) {
t.Errorf("Expected response header to contain cookie value '%s'", cookieValue)
}
// Get the value of the cookie using the GetCookie method.
retrievedValue := ctx.GetCookie(cookieName)
if retrievedValue != cookieValue {
t.Errorf("Expected retrieved cookie value to be '%s', but got '%s'", cookieValue, retrievedValue)
}
// Clear the cookie using the ClearCookie method.
ctx.ClearCookie(cookieName)
}
func TestContext_GetCookie(t *testing.T) {
router := NewRouter()
app.Router = router
router.Get("/", func(ctx *Context) error {
cookie := ctx.GetCookie("test")
ctx.String(cookie)
return nil
})
}
func TestContext_ClearCookie(t *testing.T) {
router := NewRouter()
app.Router = router
router.Get("/", func(ctx *Context) error {
ctx.ClearCookie("test")
return nil
})
}
func TestContext_Header(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/", nil)
ctx := NewContext(w, r)
headerKey := "test-header"
headerValue := "test-value"
ctx.SetResponseHeader(headerKey, headerValue)
retrievedHeaderValue := ctx.GetResponseHeader(headerKey)
if retrievedHeaderValue != headerValue {
t.Errorf("Expected response header value to be '%s', but got '%s'", headerValue, retrievedHeaderValue)
}
reqHeaderKey := "test-request-header"
reqHeaderValue := "test-request-value"
ctx.SetRequestHeader(reqHeaderKey, reqHeaderValue)
retrievedReqHeaderValue := ctx.GetRequestHeader(reqHeaderKey)
if retrievedReqHeaderValue != reqHeaderValue {
t.Errorf("Expected request header value to be '%s', but got '%s'", reqHeaderValue, retrievedReqHeaderValue)
}
anotherHeaderKey := "another-test-header"
anotherHeaderValue := "another-test-value"
ctx.SetResponseHeader(anotherHeaderKey, anotherHeaderValue)
retrievedAnotherHeaderValue := w.Header().Get(anotherHeaderKey)
if retrievedAnotherHeaderValue != anotherHeaderValue {
t.Errorf("Expected response header value to be '%s', but got '%s'", anotherHeaderValue, retrievedAnotherHeaderValue)
}
}
func TestContext_Next(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)
err := ctx.Next()
if err != nil {
return
}
}
func TestContext_Reset(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/", nil)
ctx := NewContext(w, r)
ctx.handlerIdx = 1
ctx.Reset()
if ctx.handlerIdx != -1 {
t.Errorf("Expected handler index to be -1 after calling Reset(), but got: %d", ctx.handlerIdx)
}
}
func TestContext_Status(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)
ctx.Status(200)
}
func TestContext_JSON(t *testing.T) {
w := httptest.NewRecorder()
ctx := NewContext(w, nil)
_, err := ctx.JSON(200, map[string]string{"test": "test"})
if err != nil {
return
}
}
func TestContext_SetContentType(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
ctx := NewContext(w, r)
ctx.SetContentType("application/json")
}
func TestContext_Accepts(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/", nil)
ctx := NewContext(w, r)
// Set the Accept header to a single value of type "text/html".
ctx.SetRequestHeader("Accept", "text/html")
// Verify that the Accept header was set correctly.
retrievedHeaderValue := ctx.GetRequestHeader("Accept")
if retrievedHeaderValue != "text/html" {
t.Errorf("Expected request header value to be 'text/html', but got '%s'", retrievedHeaderValue)
}
// Test with a single acceptable media type.
acceptedType := ctx.Accepts("text/html")
if acceptedType != "text/html" {
t.Errorf("Expected acceptable media type to be 'text/html', but got '%s'", acceptedType)
}
// Test with multiple acceptable media types, including one that is not present in the Accept header.
acceptedType = ctx.Accepts("text/plain", "text/html")
if acceptedType != "text/html" {
t.Errorf("Expected acceptable media type to be 'text/html', but got '%s'", acceptedType)
}
// Test with multiple acceptable media types, none of which are present in the Accept header.
acceptedType = ctx.Accepts("application/json", "text/plain")
if acceptedType != "" {
t.Errorf("Expected no acceptable media type, but got '%s'", acceptedType)
}
}
func TestContext_BodyParser(t *testing.T) {
w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodPost, "/", strings.NewReader(`{"name": "John", "age": 30}`))
ctx := NewContext(w, r)
// Define a struct to decode the request body into.
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Parse the request body using the BodyParser method.
var person Person
err := ctx.BodyParser(&person)
if err != nil {
t.Errorf("Expected no error, but got '%s'", err.Error())
} else {
// Verify that the request body was parsed correctly.
if person.Name != "John" || person.Age != 30 {
t.Errorf("Expected person object to have name 'John' and age 30, but got %+v", person)
}
}
}