Skip to content

Commit

Permalink
Merge pull request #28 from steinfletcher/feature/add_cookie_response…
Browse files Browse the repository at this point in the history
…_matcher

Add cookie name/value response matcher
  • Loading branch information
steinfletcher authored Mar 8, 2019
2 parents e24e986 + 9d483c0 commit 7d10450
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,13 @@ func (r *Response) Body(b string) *Response {

// Cookies is the expected response cookies
func (r *Response) Cookies(cookies ...*Cookie) *Response {
r.cookies = cookies
r.cookies = append(r.cookies, cookies...)
return r
}

// Cookie is used to match on an individual cookie name/value pair in the expected response cookies
func (r *Response) Cookie(name, value string) *Response {
r.cookies = append(r.cookies, NewCookie(name).Value(value))
return r
}

Expand Down Expand Up @@ -672,7 +678,7 @@ func (a *APITest) assertCookies(response *httptest.ResponseRecorder) {
}
}
assert.Equal(a.t, true, foundCookie, "ExpectedCookie not found - "+*expectedCookie.name)
assert.Equal(a.t, 0, len(mismatchedFields), mismatchedFields)
assert.Equal(a.t, 0, len(mismatchedFields), strings.Join(mismatchedFields, ","))
}
}

Expand Down
5 changes: 5 additions & 0 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ func TestApiTest_MatchesResponseCookies(t *testing.T) {
Name: "VVV",
Value: "9ig32g34g",
})
http.SetCookie(w, &http.Cookie{
Name: "YYY",
Value: "kfiufhtne",
})

w.WriteHeader(http.StatusOK)
})
Expand All @@ -306,6 +310,7 @@ func TestApiTest_MatchesResponseCookies(t *testing.T) {
Cookies(
NewCookie("ABC").Value("12345"),
NewCookie("DEF").Value("67890")).
Cookie("YYY", "kfiufhtne").
CookiePresent("XXX").
CookiePresent("VVV").
CookieNotPresent("ZZZ").
Expand Down

0 comments on commit 7d10450

Please sign in to comment.