-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: HaveHTTPHeaderWithValue() matcher (#463)
Co-authored-by: Onsi Fakhouri <onsijoe@gmail.com>
- Loading branch information
Showing
3 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package matchers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/onsi/gomega/format" | ||
"github.com/onsi/gomega/types" | ||
) | ||
|
||
type HaveHTTPHeaderWithValueMatcher struct { | ||
Header string | ||
Value interface{} | ||
} | ||
|
||
func (matcher *HaveHTTPHeaderWithValueMatcher) Match(actual interface{}) (success bool, err error) { | ||
headerValue, err := matcher.extractHeader(actual) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
headerMatcher, err := matcher.getSubMatcher() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return headerMatcher.Match(headerValue) | ||
} | ||
|
||
func (matcher *HaveHTTPHeaderWithValueMatcher) FailureMessage(actual interface{}) string { | ||
headerValue, err := matcher.extractHeader(actual) | ||
if err != nil { | ||
panic(err) // protected by Match() | ||
} | ||
|
||
headerMatcher, err := matcher.getSubMatcher() | ||
if err != nil { | ||
panic(err) // protected by Match() | ||
} | ||
|
||
diff := format.IndentString(headerMatcher.FailureMessage(headerValue), 1) | ||
return fmt.Sprintf("HTTP header %q:\n%s", matcher.Header, diff) | ||
} | ||
|
||
func (matcher *HaveHTTPHeaderWithValueMatcher) NegatedFailureMessage(actual interface{}) (message string) { | ||
headerValue, err := matcher.extractHeader(actual) | ||
if err != nil { | ||
panic(err) // protected by Match() | ||
} | ||
|
||
headerMatcher, err := matcher.getSubMatcher() | ||
if err != nil { | ||
panic(err) // protected by Match() | ||
} | ||
|
||
diff := format.IndentString(headerMatcher.NegatedFailureMessage(headerValue), 1) | ||
return fmt.Sprintf("HTTP header %q:\n%s", matcher.Header, diff) | ||
} | ||
|
||
func (matcher *HaveHTTPHeaderWithValueMatcher) getSubMatcher() (types.GomegaMatcher, error) { | ||
switch m := matcher.Value.(type) { | ||
case string: | ||
return &EqualMatcher{Expected: matcher.Value}, nil | ||
case types.GomegaMatcher: | ||
return m, nil | ||
default: | ||
return nil, fmt.Errorf("HaveHTTPHeaderWithValue matcher must be passed a string or a GomegaMatcher. Got:\n%s", format.Object(matcher.Value, 1)) | ||
} | ||
} | ||
|
||
func (matcher *HaveHTTPHeaderWithValueMatcher) extractHeader(actual interface{}) (string, error) { | ||
switch r := actual.(type) { | ||
case *http.Response: | ||
return r.Header.Get(matcher.Header), nil | ||
case *httptest.ResponseRecorder: | ||
return r.Result().Header.Get(matcher.Header), nil | ||
default: | ||
return "", fmt.Errorf("HaveHTTPHeaderWithValue matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n%s", format.Object(actual, 1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package matchers_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("HaveHTTPHeader", func() { | ||
It("can match an HTTP header", func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "fake value")) | ||
}) | ||
|
||
It("can mismatch an HTTP header", func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("other-header", "fake value")) | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "other value")) | ||
}) | ||
|
||
When("the header is set more than once", func() { | ||
It("matches the first value and not the second", func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value1") | ||
resp.Header.Add("fake-header", "fake value2") | ||
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "fake value1")) | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "fake value2")) | ||
}) | ||
}) | ||
|
||
When("ACTUAL is *httptest.ResponseRecorder", func() { | ||
It("can match an HTTP header", func() { | ||
resp := &httptest.ResponseRecorder{} | ||
resp.Header().Add("fake-header", "fake value") | ||
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "fake value")) | ||
}) | ||
|
||
It("can mismatch an HTTP header", func() { | ||
resp := &httptest.ResponseRecorder{} | ||
resp.Header().Add("fake-header", "fake value") | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("other-header", "fake value")) | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "other value")) | ||
}) | ||
}) | ||
|
||
When("ACTUAL is neither *http.Response nor *httptest.ResponseRecorder", func() { | ||
It("errors", func() { | ||
failures := InterceptGomegaFailures(func() { | ||
Expect("foo").To(HaveHTTPHeaderWithValue("bar", "baz")) | ||
}) | ||
Expect(failures).To(HaveLen(1)) | ||
Expect(failures[0]).To(Equal("HaveHTTPHeaderWithValue matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n <string>: foo")) | ||
}) | ||
}) | ||
|
||
When("EXPECTED VALUE is a matcher", func() { | ||
It("can match an HTTP header", func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("value"))) | ||
}) | ||
|
||
It("can mismatch an HTTP header", func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("foo"))) | ||
}) | ||
}) | ||
|
||
When("EXPECTED VALUE is something else", func() { | ||
It("errors", func() { | ||
failures := InterceptGomegaFailures(func() { | ||
resp := &http.Response{} | ||
Expect(resp).To(HaveHTTPHeaderWithValue("bar", 42)) | ||
}) | ||
Expect(failures).To(HaveLen(1)) | ||
Expect(failures[0]).To(Equal("HaveHTTPHeaderWithValue matcher must be passed a string or a GomegaMatcher. Got:\n <int>: 42")) | ||
}) | ||
}) | ||
|
||
Describe("FailureMessage", func() { | ||
When("matching a string", func() { | ||
It("returns message", func() { | ||
failures := InterceptGomegaFailures(func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "other value")) | ||
}) | ||
Expect(failures).To(HaveLen(1)) | ||
Expect(failures[0]).To(Equal(`HTTP header "fake-header": | ||
Expected | ||
<string>: fake value | ||
to equal | ||
<string>: other value`), failures[0]) | ||
}) | ||
}) | ||
|
||
When("matching a matcher", func() { | ||
It("returns message", func() { | ||
failures := InterceptGomegaFailures(func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("other"))) | ||
}) | ||
Expect(failures).To(HaveLen(1)) | ||
Expect(failures[0]).To(Equal(`HTTP header "fake-header": | ||
Expected | ||
<string>: fake value | ||
to contain substring | ||
<string>: other`), failures[0]) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("NegatedFailureMessage", func() { | ||
When("matching a string", func() { | ||
It("returns message", func() { | ||
failures := InterceptGomegaFailures(func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "fake value")) | ||
}) | ||
Expect(failures).To(HaveLen(1)) | ||
Expect(failures[0]).To(Equal(`HTTP header "fake-header": | ||
Expected | ||
<string>: fake value | ||
not to equal | ||
<string>: fake value`), failures[0]) | ||
}) | ||
}) | ||
|
||
When("matching a matcher", func() { | ||
It("returns message", func() { | ||
failures := InterceptGomegaFailures(func() { | ||
resp := &http.Response{} | ||
resp.Header = make(http.Header) | ||
resp.Header.Add("fake-header", "fake value") | ||
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("value"))) | ||
}) | ||
Expect(failures).To(HaveLen(1)) | ||
Expect(failures[0]).To(Equal(`HTTP header "fake-header": | ||
Expected | ||
<string>: fake value | ||
not to contain substring | ||
<string>: value`), failures[0]) | ||
}) | ||
}) | ||
}) | ||
}) |