diff --git a/mock/requestbuilder.go b/mock/requestbuilder.go index 3a7836b..3e675a0 100644 --- a/mock/requestbuilder.go +++ b/mock/requestbuilder.go @@ -2,6 +2,7 @@ package mock import ( "encoding/base64" + "net/http" ) type RequestBuilder struct { @@ -57,9 +58,14 @@ func createBasicToken(username string, password string) string { return basicToken } +// AddHeader Sets a request header key and values to match against on the mock definition +// +// Note: the header key will be formatted to [http.CanonicalHeaderKey] as smocker is case-sensitive regarding header +// keys, this will help prevent mock expectations not matching as Go's standard http library uses the Canonical format. func (rb RequestBuilder) AddHeader(key string, values ...string) RequestBuilder { rb.initialiseHeaders() - rb.request.Headers[key] = values + canonicalHeaderKey := http.CanonicalHeaderKey(key) + rb.request.Headers[canonicalHeaderKey] = values return rb } diff --git a/mock/requestbuilder_test.go b/mock/requestbuilder_test.go index e95ced7..3b25e54 100644 --- a/mock/requestbuilder_test.go +++ b/mock/requestbuilder_test.go @@ -75,8 +75,9 @@ func TestNewRequestBuilder_AddQueryParam_Build(t *testing.T) { func TestNewRequestBuilder_AddHeader_Build(t *testing.T) { expectedHeaders := map[string][]string{ - "Content-Type": {"application/json", "application/vnd.api+json"}, - "Authorization": {"Bearer sv2361fr1o8ph3oin"}, + "Content-Type": {"application/json", "application/vnd.api+json"}, + "Authorization": {"Bearer sv2361fr1o8ph3oin"}, + "Canonical-Header-Format": {"some-value"}, } expectedRequest := mock.Request{ Method: http.MethodPut, @@ -87,6 +88,7 @@ func TestNewRequestBuilder_AddHeader_Build(t *testing.T) { requestBuilder := mock.NewRequestBuilder(http.MethodPut, "/foo/bar") requestBuilder.AddHeader("Content-Type", "application/json", "application/vnd.api+json") requestBuilder.AddHeader("Authorization", "Bearer sv2361fr1o8ph3oin") + requestBuilder.AddHeader("canonical-header-FORMAT", "some-value") request := requestBuilder.Build() assert.Equal(t, expectedRequest, request)