From 250baaa219a25e881204fe3c4f3bb0aa67d72331 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Tue, 22 May 2018 16:33:04 +1000 Subject: [PATCH] feat(state): update dsl.State to support v3 params - State struct will now accept params, and pass in to StateHandler functions See https://github.com/pact-foundation/pact-specification/tree/version-3#allow-multiple-provider-states-with-parameters --- dsl/message.go | 7 ++++--- dsl/pact.go | 2 +- examples/messages/provider/message_pact_provider_test.go | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/dsl/message.go b/dsl/message.go index de4664bb6..ba7cb349f 100644 --- a/dsl/message.go +++ b/dsl/message.go @@ -7,7 +7,7 @@ import ( // StateHandler is a provider function that sets up a given state before // the provider interaction is validated -type StateHandler func(string) error +type StateHandler func(State) error // StateHandlers is a list of StateHandler's type StateHandlers map[string]StateHandler @@ -52,12 +52,13 @@ type Message struct { // State specifies how the system should be configured when // verified. e.g. "user A exists" type State struct { - Name string `json:"name"` + Name string `json:"name"` + Params map[string]interface{} `json:"params,omitempty"` } // Given specifies a provider state. Optional. func (p *Message) Given(state string) *Message { - p.States = []State{State{state}} + p.States = []State{State{Name: state}} return p } diff --git a/dsl/pact.go b/dsl/pact.go index 2c162bb67..19f1b8cd2 100644 --- a/dsl/pact.go +++ b/dsl/pact.go @@ -356,7 +356,7 @@ var messageHandler = func(messageHandlers MessageHandlers, stateHandlers StateHa log.Printf("[WARN] state handler not found for state: %v", state.Name) } else { // Execute state handler - if err = sf(state.Name); err != nil { + if err = sf(state); err != nil { log.Printf("[WARN] state handler for '%v' return error: %v", state.Name, err) w.WriteHeader(http.StatusInternalServerError) return diff --git a/examples/messages/provider/message_pact_provider_test.go b/examples/messages/provider/message_pact_provider_test.go index eac97a4e5..02391877c 100644 --- a/examples/messages/provider/message_pact_provider_test.go +++ b/examples/messages/provider/message_pact_provider_test.go @@ -36,7 +36,7 @@ func TestMessageProvider_Success(t *testing.T) { } stateMappings := dsl.StateHandlers{ - "user with id 127 exists": func(s string) error { + "user with id 127 exists": func(s dsl.State) error { user = &types.User{ ID: 44, Name: "Baz", @@ -48,7 +48,7 @@ func TestMessageProvider_Success(t *testing.T) { return nil }, - "no users": func(s string) error { + "no users": func(s dsl.State) error { user = nil return nil