Skip to content

Commit

Permalink
add header matchers for present/not present
Browse files Browse the repository at this point in the history
  • Loading branch information
steinfletcher committed May 5, 2019
1 parent 091ea2a commit 54d33ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
42 changes: 37 additions & 5 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ type Response struct {
status int
body string
headers map[string][]string
headersPresent []string
headersNotPresent []string
cookies []*Cookie
cookiesPresent []string
cookiesNotPresent []string
Expand Down Expand Up @@ -368,16 +370,30 @@ func (r *Response) CookieNotPresent(cookieName string) *Response {

// Header is a builder method to set the request headers
func (r *Response) Header(key, value string) *Response {
normalizedKey := textproto.CanonicalMIMEHeaderKey(key)
r.headers[normalizedKey] = append(r.headers[normalizedKey], value)
normalizedName := textproto.CanonicalMIMEHeaderKey(key)
r.headers[normalizedName] = append(r.headers[normalizedName], value)
return r
}

// HeaderPresent is a builder method to set the request headers that should be present in the response
func (r *Response) HeaderPresent(name string) *Response {
normalizedName := textproto.CanonicalMIMEHeaderKey(name)
r.headersPresent = append(r.headersPresent, normalizedName)
return r
}

// HeaderNotPresent is a builder method to set the request headers that should not be present in the response
func (r *Response) HeaderNotPresent(name string) *Response {
normalizedName := textproto.CanonicalMIMEHeaderKey(name)
r.headersNotPresent = append(r.headersNotPresent, normalizedName)
return r
}

// Headers is a builder method to set the request headers
func (r *Response) Headers(headers map[string]string) *Response {
for k, v := range headers {
normalizedKey := textproto.CanonicalMIMEHeaderKey(k)
r.headers[normalizedKey] = append(r.headers[textproto.CanonicalMIMEHeaderKey(normalizedKey)], v)
for name, value := range headers {
normalizedName := textproto.CanonicalMIMEHeaderKey(name)
r.headers[normalizedName] = append(r.headers[textproto.CanonicalMIMEHeaderKey(normalizedName)], value)
}
return r
}
Expand Down Expand Up @@ -755,6 +771,22 @@ func (a *APITest) assertHeaders(res *httptest.ResponseRecorder) {
}
}
}

if len(a.response.headersPresent) > 0 {
for _, expectedName := range a.response.headersPresent {
if res.Header().Get(expectedName) == "" {
a.t.Fatalf("expected header '%s' not present in response", expectedName)
}
}
}

if len(a.response.headersNotPresent) > 0 {
for _, name := range a.response.headersNotPresent {
if res.Header().Get(name) != "" {
a.t.Fatalf("did not expect header '%s' in response", name)
}
}
}
}

func debugLog(prefix, header, msg string) {
Expand Down
3 changes: 3 additions & 0 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ func TestApiTest_MatchesResponseHeaders_WithMixedKeyCase(t *testing.T) {
Header("Authorization", "12345").
Header("Authorization", "00000").
Header("authorization", "98765").
HeaderPresent("Def").
HeaderPresent("Authorization").
HeaderNotPresent("XYZ").
End()
}

Expand Down

0 comments on commit 54d33ef

Please sign in to comment.