Skip to content

Commit

Permalink
feat: allow WithHeaders to simplify bulk header addition
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 7, 2021
1 parent 05ac8e3 commit ae91fc3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions consumer/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func (i *InteractionRequest) WithHeader(key string, values ...matchers.Matcher)
return i
}

func (i *InteractionRequest) WithHeaders(headers matchers.HeadersMatcher) *InteractionRequest {
i.interactionHandle.WithRequestHeaders(headersMatcherToNativeHeaders(headers))

return i
}

func (i *InteractionRequest) WithJSONBody(body interface{}) *InteractionRequest {
// TODO: Don't like panic, but not sure if there is a better builder experience?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
Expand Down Expand Up @@ -131,6 +137,12 @@ func (i *InteractionResponse) WithHeader(key string, values ...matchers.Matcher)
return i
}

func (i *InteractionResponse) WithHeaders(headers matchers.HeadersMatcher) *InteractionResponse {
i.interactionHandle.WithRequestHeaders(headersMatcherToNativeHeaders(headers))

return i
}

func (i *InteractionResponse) WithJSONBody(body interface{}) *InteractionResponse {
// TODO: Don't like panic, how to build a better builder here - nil return + log?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
Expand Down Expand Up @@ -246,3 +258,16 @@ func keyValuesToMapStringArrayInterface(key string, values ...matchers.Matcher)

return q
}

func headersMatcherToNativeHeaders(headers matchers.HeadersMatcher) map[string][]interface{} {
h := make(map[string][]interface{})

for k, v := range headers {
h[k] = make([]interface{}, len(v))
for i, vv := range v {
h[k][i] = vv
}
}

return h
}

0 comments on commit ae91fc3

Please sign in to comment.