Skip to content

Commit

Permalink
Merge pull request #36 from appleboy/cookie
Browse files Browse the repository at this point in the history
Support cookie
  • Loading branch information
appleboy authored Sep 10, 2016
2 parents a65d12a + 2507e7c commit 3efeef2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ func TestQueryString(t *testing.T) {
}
```

### Set Cookie String

Using `SetCookie` to generate raw data.

```go
func TestQueryString(t *testing.T) {
r := gofight.New()

r.GET("/hello").
SetQuery(gofight.H{
"foo": "bar",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
})
}
```

## Example

* Basic HTTP Router: [basic.go](example/basic.go), [basic_test.go](example/basic_test.go)
Expand Down
17 changes: 17 additions & 0 deletions gofight.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type RequestConfig struct {
Path string
Body string
Headers H
Cookies H
Debug bool
}

Expand Down Expand Up @@ -245,6 +246,15 @@ func (rc *RequestConfig) SetBody(body string) *RequestConfig {
return rc
}

// SetCookie supply cookies what you defined.
func (rc *RequestConfig) SetCookie(cookies H) *RequestConfig {
if len(cookies) > 0 {
rc.Cookies = cookies
}

return rc
}

func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder) {
qs := ""
if strings.Contains(rc.Path, "?") {
Expand Down Expand Up @@ -278,11 +288,18 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
}
}

if len(rc.Cookies) > 0 {
for k, v := range rc.Cookies {
req.AddCookie(&http.Cookie{Name: k, Value: v})
}
}

if rc.Debug {
log.Printf("Request Method: %s", rc.Method)
log.Printf("Request Path: %s", rc.Path)
log.Printf("Request Body: %s", rc.Body)
log.Printf("Request Headers: %s", rc.Headers)
log.Printf("Request Cookies: %s", rc.Cookies)
log.Printf("Request Header: %s", req.Header)
}

Expand Down
14 changes: 14 additions & 0 deletions gofight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ func TestGinHeader(t *testing.T) {
})
}

func TestGinCookie(t *testing.T) {
r := New()

r.GET("/text").
SetCookie(H{
"foo": "bar",
}).
Run(framework.GinEngine(), func(r HTTPResponse, rq HTTPRequest) {

assert.Equal(t, http.StatusOK, r.Code)
assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
})
}

func TestGinQuery(t *testing.T) {
r := New()

Expand Down

0 comments on commit 3efeef2

Please sign in to comment.