Skip to content

Commit

Permalink
support multiple http headers with the same key
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Jan 17, 2019
1 parent a5cddc2 commit 9a39e62
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
42 changes: 32 additions & 10 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/textproto"
"strings"
"testing"
)
Expand Down Expand Up @@ -41,7 +42,7 @@ func New(name ...string) *APITest {
}
response := &Response{
apiTest: apiTest,
headers: map[string]string{},
headers: map[string][]string{},
}
apiTest.request = request
apiTest.response = response
Expand Down Expand Up @@ -198,14 +199,16 @@ func (r *Request) QueryCollection(q map[string][]string) *Request {

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

// Headers is a builder method to set the request headers
func (r *Request) Headers(headers map[string]string) *Request {
for k, v := range headers {
r.headers[k] = append(r.headers[k], v)
normalizedKey := textproto.CanonicalMIMEHeaderKey(k)
r.headers[normalizedKey] = append(r.headers[normalizedKey], v)
}
return r
}
Expand Down Expand Up @@ -233,7 +236,7 @@ func (r *Request) Expect(t *testing.T) *Response {
type Response struct {
status int
body string
headers map[string]string
headers map[string][]string
cookies []*Cookie
cookiesPresent []string
cookiesNotPresent []string
Expand Down Expand Up @@ -271,9 +274,19 @@ func (r *Response) CookieNotPresent(cookieName string) *Response {
return r
}

// Headers is the expected response headers
// 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)
return r
}

// Headers is a builder method to set the request headers
func (r *Response) Headers(headers map[string]string) *Response {
r.headers = headers
for k, v := range headers {
normalizedKey := textproto.CanonicalMIMEHeaderKey(k)
r.headers[normalizedKey] = append(r.headers[textproto.CanonicalMIMEHeaderKey(normalizedKey)], v)
}
return r
}

Expand Down Expand Up @@ -469,10 +482,19 @@ func responseCookies(response *httptest.ResponseRecorder) []*http.Cookie {
}

func (a *APITest) assertHeaders(res *httptest.ResponseRecorder) {
if a.response.headers != nil {
for k, v := range a.response.headers {
header := res.Header().Get(k)
assertEqual(a.t, v, header, fmt.Sprintf("'%s' header should be equal", k))
for expectedHeader, expectedValues := range a.response.headers {
for _, expectedValue := range expectedValues {
found := false
result := res.Result()
for _, resValue := range result.Header[expectedHeader] {
if expectedValue == resValue {
found = true
break
}
}
if !found {
a.t.Fatalf("could not match header=%s", expectedHeader)
}
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,13 @@ func TestApiTest_MatchesResponseHttpCookies_OnlySuppliedFields(t *testing.T) {
End()
}

func TestApiTest_MatchesResponseHeaders(t *testing.T) {
func TestApiTest_MatchesResponseHeaders_WithMixedKeyCase(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("ABC", "12345")
w.Header().Set("DEF", "67890")
w.Header().Set("Authorization", "12345")
w.Header().Add("Authorization", "98765")
w.WriteHeader(http.StatusOK)
})

Expand All @@ -324,9 +326,11 @@ func TestApiTest_MatchesResponseHeaders(t *testing.T) {
Expect(t).
Status(http.StatusOK).
Headers(map[string]string{
"ABC": "12345",
"DEF": "67890",
"Abc": "12345",
"Def": "67890",
}).
Header("Authorization", "12345").
Header("authorization", "98765").
End()
}

Expand Down

0 comments on commit 9a39e62

Please sign in to comment.