Skip to content

Commit

Permalink
feat(message): cleanup handler interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Apr 4, 2018
1 parent 6b2e2c5 commit ad7454e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
13 changes: 13 additions & 0 deletions dsl/message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package dsl

// type MessageHandler map[string]func(...interface{})

// MessageProvider is a provider function that generates a
// message for a Consumer given a Message context (state, description etc.)
type MessageProvider func(Message) (interface{}, error)

// MessageProviders is a list of handlers ordered by description
type MessageProviders map[string]MessageProvider

// MessageConsumer receives a message and must be able to parse
// the content
type MessageConsumer func(Message) error

// Message is a representation of a single, unidirectional message
// e.g. MQ, pub/sub, Websocket, Lambda
// Message is the main implementation of the Pact Message interface.
Expand Down
6 changes: 3 additions & 3 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ var checkCliCompatibility = func() {
// A Message Producer is analagous to Consumer in the HTTP Interaction model.
// It is the initiator of an interaction, and expects something on the other end
// of the interaction to respond - just in this case, not immediately.
func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyMessageRequest, handlers map[string]func(...interface{}) (map[string]interface{}, error)) (types.ProviderVerifierResponse, error) {
func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyMessageRequest, handlers MessageProviders) (types.ProviderVerifierResponse, error) {
response := types.ProviderVerifierResponse{}

// Starts the message wrapper API with hooks back to the message handlers
Expand Down Expand Up @@ -372,7 +372,7 @@ func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyMessageRe
}

// Execute function handler
res, handlerErr := f()
res, handlerErr := f(message)

if handlerErr != nil {
w.WriteHeader(http.StatusServiceUnavailable)
Expand Down Expand Up @@ -428,7 +428,7 @@ func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyMessageRe
// A Message Consumer is analagous to a Provider in the HTTP Interaction model.
// It is the receiver of an interaction, and needs to be able to handle whatever
// request was provided.
func (p *Pact) VerifyMessageConsumer(message *Message, handler func(Message) error) error {
func (p *Pact) VerifyMessageConsumer(message *Message, handler MessageConsumer) error {
log.Printf("[DEBUG] verify message")
p.Setup(false)

Expand Down
4 changes: 2 additions & 2 deletions examples/messages/provider/message_pact_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestMessageProvider_Success(t *testing.T) {

// Map test descriptions to message producer (handlers)
// TODO: convert these all to types to ease readability
functionMappings := map[string]func(...interface{}) (map[string]interface{}, error){
"a test message": func(...interface{}) (map[string]interface{}, error) {
functionMappings := dsl.MessageProviders{
"a test message": func(m dsl.Message) (interface{}, error) {
fmt.Println("Calling 'text' function that would produce a message")
res := map[string]interface{}{
"content": map[string]string{
Expand Down

0 comments on commit ad7454e

Please sign in to comment.