Skip to content

Commit 02ca550

Browse files
committed
add comments/documentation, change SingleHeaderMatcher -> HeaderMatcher
1 parent af99b58 commit 02ca550

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ defer s.Close()
5454
downstream.AssertExpectations(t)
5555
```
5656

57+
If instead you wish to match against headers as well, a slightly different httpmock object can be used (please note the change in function name to be matched against):
58+
59+
```go
60+
downstream := &httpmock.MockHandlerWithHeaders{}
61+
62+
// A simple GET that returns some pre-canned content and a specific header
63+
downstream.On("HandleWithHeaders", "GET", "/object/12345", MatchHeader("MOCK", "this"), mock.Anything).Return(httpmock.Response{
64+
Body: []byte(`{"status": "ok"}`),
65+
})
66+
67+
// ... same as above
68+
69+
```
70+
5771
The httpmock package also provides helpers for checking calls using json objects, like so:
5872

5973
```go

httpmock.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ This example uses MockHandler, a Handler that is a github.com/stretchr/testify/m
3030
3131
downstream.AssertExpectations(t)
3232
33+
If instead you wish to match against headers as well, a slightly different httpmock object can be used
34+
(please note the change in function name to be matched against):
35+
36+
downstream := &httpmock.MockHandlerWithHeaders{}
37+
38+
// A simple GET that returns some pre-canned content
39+
downstream.On("HandleWithHeaders", "GET", "/object/12345", MatchHeader("MOCK", "this"), mock.Anything).Return(httpmock.Response{
40+
Body: []byte(`{"status": "ok"}`),
41+
})
42+
43+
// ... same as above
44+
45+
3346
Httpmock also provides helpers for checking calls using json objects, like so:
3447
3548
// This tests a hypothetical "echo" endpoint, which returns the body we pass to it.

httpmock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestBasicRequestResponseWithHeaders(t *testing.T) {
4141
"HandleWithHeaders",
4242
"GET",
4343
"/object/12345",
44-
SingleHeaderMatcher(headerKey, headerVal),
44+
HeaderMatcher(headerKey, headerVal),
4545
mock.Anything,
4646
).
4747
Return(Response{

mockhandler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ func ToJSON(obj interface{}) []byte {
6464
return data
6565
}
6666

67-
func SingleHeaderMatcher(key, value string) interface{} {
67+
// HeaderMatcher matches the presence of a header named key that has a given value. Other headers
68+
// are allowed to exist and are not checked.
69+
func HeaderMatcher(key, value string) interface{} {
6870
headers := make(http.Header)
6971
headers.Set(key, value)
7072
return MultiHeaderMatcher(headers)
7173
}
7274

75+
// MultiHeaderMatcher matches the presence and content of multiple headers. Other headers besides those
76+
// within desiredHeaders are allowed to exist and are not checked.
7377
func MultiHeaderMatcher(desiredHeaders http.Header) interface{} {
7478
return mock.MatchedBy(func(headers http.Header) bool {
7579
for key, val := range desiredHeaders {

0 commit comments

Comments
 (0)