Skip to content

Commit bbdd677

Browse files
committed
support set cookie.
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent a65d12a commit bbdd677

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

gofight.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type RequestConfig struct {
9999
Path string
100100
Body string
101101
Headers H
102+
Cookies H
102103
Debug bool
103104
}
104105

@@ -245,6 +246,15 @@ func (rc *RequestConfig) SetBody(body string) *RequestConfig {
245246
return rc
246247
}
247248

249+
// SetCookie supply cookies what you defined.
250+
func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig {
251+
if len(cookies) > 0 {
252+
rc.Cookies = cookies
253+
}
254+
255+
return rc
256+
}
257+
248258
func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder) {
249259
qs := ""
250260
if strings.Contains(rc.Path, "?") {
@@ -278,11 +288,18 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
278288
}
279289
}
280290

291+
if len(rc.Cookies) > 0 {
292+
for k, v := range rc.Cookies {
293+
req.AddCookie(&http.Cookie{Name: k, Value: v})
294+
}
295+
}
296+
281297
if rc.Debug {
282298
log.Printf("Request Method: %s", rc.Method)
283299
log.Printf("Request Path: %s", rc.Path)
284300
log.Printf("Request Body: %s", rc.Body)
285301
log.Printf("Request Headers: %s", rc.Headers)
302+
log.Printf("Request Cookies: %s", rc.Cookies)
286303
log.Printf("Request Header: %s", req.Header)
287304
}
288305

gofight_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ func TestGinHeader(t *testing.T) {
7676
})
7777
}
7878

79+
func TestGinCookie(t *testing.T) {
80+
r := New()
81+
82+
r.GET("/text").
83+
SetCookie(H{
84+
"foo": "bar",
85+
"hello": "world",
86+
}).
87+
Run(framework.GinEngine(), func(r HTTPResponse, rq HTTPRequest) {
88+
89+
assert.Equal(t, http.StatusOK, r.Code)
90+
assert.Equal(t, "foo=bar; hello=world", rq.Header.Get("cookie"))
91+
})
92+
}
93+
7994
func TestGinQuery(t *testing.T) {
8095
r := New()
8196

0 commit comments

Comments
 (0)