Skip to content

Commit 3efeef2

Browse files
authored
Merge pull request #36 from appleboy/cookie
Support cookie
2 parents a65d12a + 2507e7c commit 3efeef2

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ func TestQueryString(t *testing.T) {
213213
}
214214
```
215215

216+
### Set Cookie String
217+
218+
Using `SetCookie` to generate raw data.
219+
220+
```go
221+
func TestQueryString(t *testing.T) {
222+
r := gofight.New()
223+
224+
r.GET("/hello").
225+
SetQuery(gofight.H{
226+
"foo": "bar",
227+
}).
228+
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
229+
assert.Equal(t, http.StatusOK, r.Code)
230+
assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
231+
})
232+
}
233+
```
234+
216235
## Example
217236

218237
* Basic HTTP Router: [basic.go](example/basic.go), [basic_test.go](example/basic_test.go)

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ 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+
}).
86+
Run(framework.GinEngine(), func(r HTTPResponse, rq HTTPRequest) {
87+
88+
assert.Equal(t, http.StatusOK, r.Code)
89+
assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
90+
})
91+
}
92+
7993
func TestGinQuery(t *testing.T) {
8094
r := New()
8195

0 commit comments

Comments
 (0)