Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock request headers use the CanonicalHeaderKey format #minor #9

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mock/requestbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"encoding/base64"
"net/http"
)

type RequestBuilder struct {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions mock/requestbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
Loading