-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontext_test.go
267 lines (204 loc) · 5.92 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
package rux
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/testutil/assert"
)
func mockContext(method, uri string, body io.Reader, header m) *Context {
w := httptest.NewRecorder()
// create fake request
r, _ := http.NewRequest(method, uri, body)
r.RequestURI = r.URL.String()
// add headers
if len(header) > 0 {
// req.Header.Set("Content-Type", "text/plain")
for k, v := range header {
r.Header.Set(k, v)
}
}
c := &Context{}
c.Init(w, r)
return c
}
func TestContext_WithReqCtxValue(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/", nil, nil)
c.WithReqCtxValue("name", "inhere")
is.Eq("inhere", c.ReqCtxValue("name"))
is.False(c.IsTLS())
is.False(c.IsAborted())
}
func TestContext_Query(t *testing.T) {
is := assert.New(t)
// c := mockContext("GET", "/test?a=12&b=tom&arr[]=4&arr[]=9", nil, nil)
c := mockContext("GET", "/test?page=12&name=tom&arr=4&arr=9", nil, nil)
is.Eq("GET", c.Req.Method)
is.Eq("12", c.Query("page"))
is.Eq("val0", c.Query("no-key", "val0"))
ss, has := c.QueryParams("arr")
is.True(has)
is.Len(ss, 2)
vs := c.QueryValues()
is.Len(vs, 3)
// fmt.Println(vs)
is.Eq("", c.Post("page"))
is.Eq("1", c.Post("page", "1"))
val, has := c.PostParam("page")
is.Eq("", val)
is.False(has)
}
func TestContext_Post(t *testing.T) {
is := assert.New(t)
body := bytes.NewBufferString("foo=bar&page=11&both=v0&foo=second")
c := mockContext("POST", "/?both=v1", body, m{
"Accept": "application/json",
httpctype.Key: "application/x-www-form-urlencoded",
})
val, has := c.PostParam("page")
is.True(has)
is.Eq("11", val)
is.Eq("11", c.Post("page"))
is.Eq("11", c.Post("page", "1"))
is.Eq([]string{"application/json"}, c.AcceptedTypes())
is.Eq("application/x-www-form-urlencoded", c.ContentType())
// test parse multipart/form-data
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
err := mw.WriteField("kay0", "val0")
is.NoErr(err)
is.NoErr(mw.Close()) // must call Close()
c3 := mockContext("POST", "/", buf, m{
"Content-Type": mw.FormDataContentType(),
})
err = c3.ParseMultipartForm(defaultMaxMemory)
is.NoErr(err)
f0 := c3.Req.Form
is.Eq("kay0=val0", f0.Encode())
}
func TestContext_FormParams(t *testing.T) {
is := assert.New(t)
c1 := mockContext("GET", "/test1?a=1&b=2&c=3", nil, nil)
c2 := mockContext("GET", "/test2?a=1&b=2&c=3", nil, nil)
var err error
form1, err := c1.FormParams()
is.NoErr(err)
form2, err := c2.FormParams([]string{"b"})
is.NoErr(err)
is.Eq(form1.Encode(), "a=1&b=2&c=3")
is.Eq(form2.Encode(), "a=1&c=3")
// test parse multipart/form-data
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
err = mw.WriteField("kay0", "val0")
is.NoErr(err)
err = mw.Close()
is.NoErr(err)
c3 := mockContext("POST", "/", buf, m{
"Content-Type": mw.FormDataContentType(),
})
f3, err := c3.FormParams()
is.NoErr(err)
is.Eq("kay0=val0", f3.Encode())
}
func TestContext_SetCookie(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/?both=v1", nil, m{
"Accept": "application/json",
ContentType: "application/x-www-form-urlencoded",
})
c.SetCookie("ck-name", "val", 30, "/", "abc.com", true, true)
c.SetCookie("ck-name1", "val1", 40, "", "abc.com", true, true)
// Header().Get() will only return first
s := c.Resp.Header().Get("Set-Cookie")
is.NotEmpty(s)
is.Contains(s, "ck-name=val")
hs := c.Resp.Header()
is.Contains(hs, "Set-Cookie")
is.Len(hs["Set-Cookie"], 2)
is.Contains(hs["Set-Cookie"][0], "ck-name=val")
is.Contains(hs["Set-Cookie"][1], "ck-name1=val1")
}
func TestContext_FormFile(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
w, err := mw.CreateFormFile("file", "test.txt")
if assert.NoErr(t, err) {
_, _ = w.Write([]byte("test"))
}
_ = mw.Close()
c := mockContext("POST", "/", buf, m{
"Content-Type": mw.FormDataContentType(),
})
f, err := c.FormFile("file")
if assert.NoErr(t, err) {
assert.Eq(t, "test.txt", f.Filename)
}
assert.NoErr(t, c.SaveFile(f, "testdata/test.txt"))
assert.NoErr(t, c.UploadFile("file", "testdata/test.txt"))
assert.Err(t, c.UploadFile("no-exist", "testdata/test.txt"))
}
func TestContext_SaveFile(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
_ = mw.Close()
c := mockContext("POST", "/", buf, m{
"Content-Type": mw.FormDataContentType(),
})
f := &multipart.FileHeader{
Filename: "file",
}
assert.Err(t, c.SaveFile(f, "testdata/test.txt"))
}
func TestContext_RouteName(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/", nil, nil)
c.Set(CTXCurrentRouteName, "test_name")
name, ok := c.Get(CTXCurrentRouteName)
is.True(ok)
is.Eq("test_name", name)
}
func TestContext_RoutePath(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/test/{name}", nil, nil)
c.Set(CTXCurrentRoutePath, "/test/{name}")
name, ok := c.Get(CTXCurrentRoutePath)
is.True(ok)
is.Eq("/test/{name}", name)
}
func TestContext_Cookie(t *testing.T) {
is := assert.New(t)
r := New()
r.GET("/test", func(c *Context) {
val := c.Cookie("req-cke")
is.Eq("req-val", val)
val = c.Cookie("not-exist")
is.Eq("", val)
c.FastSetCookie("res-cke", "val1", 300)
})
r.GET("/delcookie", func(c *Context) {
c.DelCookie("req-cke")
})
w := mockRequest(r, GET, "/test", nil, func(req *http.Request) {
req.AddCookie(&http.Cookie{Name: "req-cke", Value: "req-val"})
})
is.Eq(200, w.Code)
resCke := w.Header().Get("Set-Cookie")
is.Eq("res-cke=val1; Path=/; Max-Age=300; HttpOnly", resCke)
w = mockRequest(r, GET, "/delcookie", nil, func(req *http.Request) {
req.AddCookie(&http.Cookie{Name: "req-cke", Value: "req-val"})
})
is.Eq(200, w.Code)
resCke = w.Header().Get("Set-Cookie")
is.Eq("req-cke=; Path=/; Max-Age=0; HttpOnly", resCke)
}
func TestContext_Length(t *testing.T) {
ris := assert.New(t)
c := mockContext("GET", "/", nil, nil)
c.WriteString("#length#")
ris.Eq(8, c.Length())
}