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
|
package growel
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"net/url"
"bytes"
)
func TestContextPlain(t *testing.T) {
w := httptest.NewRecorder()
c := &Context{W: w}
c.Plain(201, "ok")
if w.Code != 201 {
t.Fatalf("expected 201, got %d", w.Code)
}
if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "text/plain") {
t.Fatalf("wrong content-type: %s", ct)
}
if w.Body.String() != "ok" {
t.Fatalf("wrong body: %s", w.Body.String())
}
}
func TestContextJSON(t *testing.T) {
w := httptest.NewRecorder()
c := &Context{W: w}
c.JSON(200, map[string]string{"x": "y"})
if w.Code != 200 {
t.Fatalf("expected 200, got %d", w.Code)
}
if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") {
t.Fatalf("unexpected content-type: %s", ct)
}
if !strings.Contains(w.Body.String(), `"x":"y"`) {
t.Fatalf("wrong json: %s", w.Body.String())
}
}
func TestContextError(t *testing.T) {
w := httptest.NewRecorder()
c := &Context{W: w}
c.Error(http.StatusBadRequest, "bad input")
if w.Code != 400 {
t.Fatalf("expected 400, got %d", w.Code)
}
body := w.Body.String()
if !strings.Contains(body, `"error":"Bad Request"`) {
t.Fatalf("missing human readable status: %s", body)
}
if !strings.Contains(body, `"message":"bad input"`) {
t.Fatalf("missing message: %s", body)
}
if !strings.Contains(body, `"status":"400"`) {
t.Fatalf("missing status field: %s", body)
}
}
func TestContextCookies(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.AddCookie(&http.Cookie{Name: "a", Value: "1"})
r.AddCookie(&http.Cookie{Name: "b", Value: "2"})
c := &Context{R: r}
cookies := c.Cookies()
if len(cookies) != 2 {
t.Fatalf("expected 2 cookies, got %d", len(cookies))
}
}
func TestContextCookieFound(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.AddCookie(&http.Cookie{Name: "token", Value: "xyz"})
c := &Context{R: r}
ck := c.Cookie("token")
if ck == nil {
t.Fatalf("expected cookie token, got nil")
}
if ck.Value != "xyz" {
t.Fatalf("expected value xyz, got %s", ck.Value)
}
}
func TestContextCookieNotFound(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
r.AddCookie(&http.Cookie{Name: "session", Value: "abc"})
c := &Context{R: r}
ck := c.Cookie("missing")
if ck != nil {
t.Fatalf("expected nil, got %#v", ck)
}
}
func TestContextSetCompleteCookie(t *testing.T) {
w := httptest.NewRecorder()
c := &Context{W: w}
cookie := &http.Cookie{
Name: "x",
Value: "y",
Path: "/",
}
c.SetCompleteCookie(cookie)
res := w.Result()
cs := res.Cookies()
if len(cs) != 1 {
t.Fatalf("expected 1 cookie, got %d", len(cs))
}
ck := cs[0]
if ck.Name != "x" || ck.Value != "y" {
t.Fatalf("unexpected cookie: %#v", ck)
}
}
func TestContextSetCookie(t *testing.T) {
w := httptest.NewRecorder()
c := &Context{W: w}
c.SetCookie("uid", "100")
res := w.Result()
cs := res.Cookies()
if len(cs) != 1 {
t.Fatalf("expected 1 cookie, got %d", len(cs))
}
ck := cs[0]
if ck.Name != "uid" || ck.Value != "100" {
t.Fatalf("unexpected cookie: %#v", ck)
}
if ck.Path != "/" {
t.Fatalf("wrong Path: %s", ck.Path)
}
if ck.MaxAge != 3600 {
t.Fatalf("wrong MaxAge: %d", ck.MaxAge)
}
if !ck.HttpOnly {
t.Fatalf("HttpOnly should be true")
}
if !ck.Secure {
t.Fatalf("Secure should be true")
}
if ck.SameSite != http.SameSiteLaxMode {
t.Fatalf("wrong SameSite: %v", ck.SameSite)
}
}
func TestContextFormValue(t *testing.T) {
// simulate query
query, _ := url.ParseQuery("q=hello&page=2")
// simulate POST form
post := url.Values{}
post.Set("username", "eltrac")
post.Set("password", "123")
req := httptest.NewRequest("POST", "/test?q=hello&page=2", nil)
req.Form = make(url.Values)
for k, v := range query {
req.Form[k] = v
}
for k, v := range post {
req.Form[k] = append(req.Form[k], v...)
}
c := &Context{
R: req,
Form: req.Form,
Querys: req.URL.Query(),
}
if got := c.FormValue("q"); got != "hello" {
t.Errorf("FormValue(q) = %s, want hello", got)
}
if got := c.FormValue("username"); got != "eltrac" {
t.Errorf("FormValue(username) = %s, want eltrac", got)
}
if got := c.FormValue("password"); got != "123" {
t.Errorf("FormValue(password) = %s, want 123", got)
}
}
func TestContextPostFormValue(t *testing.T) {
body := bytes.NewBufferString("x=1&y=2")
req := httptest.NewRequest("POST", "/test?z=999", body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.ParseForm()
c := &Context{
R: req,
Querys: req.URL.Query(),
Form: req.Form,
}
// PostFormValue should NOT include query
if got := c.PostFormValue("z"); got != "" {
t.Errorf("PostFormValue(z) should be empty, got %s", got)
}
if got := c.PostFormValue("x"); got != "1" {
t.Errorf("PostFormValue(x) = %s, want 1", got)
}
if got := c.PostFormValue("y"); got != "2" {
t.Errorf("PostFormValue(y) = %s, want 2", got)
}
}
func TestContextQuery(t *testing.T) {
req := httptest.NewRequest("GET", "/search?kw=hello&sort=asc", nil)
c := &Context{
R: req,
Querys: req.URL.Query(),
}
if got := c.Query("kw"); got != "hello" {
t.Errorf("Query(kw) = %s, want hello", got)
}
if got := c.Query("sort"); got != "asc" {
t.Errorf("Query(sort) = %s, want asc", got)
}
if got := c.Query("page"); got != "" {
t.Errorf("Query(page) should be empty, got %s", got)
}
}
func TestContextBindJSON(t *testing.T) {
type User struct {
Name string `json:"name"`
Level int `json:"level"`
}
data := `{"name":"Eltrac","level":42}`
req := httptest.NewRequest("POST", "/json", bytes.NewBufferString(data))
w := httptest.NewRecorder()
c := &Context{
W: w,
R: req,
}
var u User
err := c.BindJSON(&u)
if err != nil {
t.Fatalf("BindJSON returned error: %v", err)
}
if u.Name != "Eltrac" {
t.Errorf("BindJSON Name = %s, want Eltrac", u.Name)
}
if u.Level != 42 {
t.Errorf("BindJSON Level = %d, want 42", u.Level)
}
}
func TestBindJSONUnknownField(t *testing.T) {
type User struct {
Name string `json:"name"`
}
data := `{"name":"Eltrac", "extra":"BAD"}`
req := httptest.NewRequest("POST", "/json", bytes.NewBufferString(data))
w := httptest.NewRecorder()
c := &Context{W: w, R: req}
var u User
err := c.BindJSON(&u)
if err == nil {
t.Fatalf("expected error for unknown field, got nil")
}
}
|