From ae91fc3395f4561849770277cb036b378bfe4dad Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Mon, 7 Jun 2021 17:57:42 +1000 Subject: [PATCH] feat: allow WithHeaders to simplify bulk header addition --- consumer/interaction.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/consumer/interaction.go b/consumer/interaction.go index e16540ba4..e6fbf3367 100644 --- a/consumer/interaction.go +++ b/consumer/interaction.go @@ -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 { @@ -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 { @@ -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 +}