/
message.go
97 lines (75 loc) · 2.76 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package dsl
import (
"log"
"reflect"
)
// StateHandler is a provider function that sets up a given state before
// the provider interaction is validated
type StateHandler func(State) error
// StateHandlers is a list of StateHandler's
type StateHandlers map[string]StateHandler
// MessageHandler is a provider function that generates a
// message for a Consumer given a Message context (state, description etc.)
type MessageHandler func(Message) (interface{}, error)
// MessageHandlers is a list of handlers ordered by description
type MessageHandlers map[string]MessageHandler
// 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.
type Message struct {
// Message Body
Content interface{} `json:"contents,omitempty"`
// Message Body as a Raw JSON string
ContentRaw interface{} `json:"-"`
// Provider state to be written into the Pact file
States []State `json:"providerStates,omitempty"`
// Message metadata
Metadata MapMatcher `json:"metadata,omitempty"`
// Description to be written into the Pact file
Description string `json:"description"`
// Type to Marshall content into when sending back to the consumer
// Defaults to interface{}
Type interface{}
Args []string `json:"-"`
}
// State specifies how the system should be configured when
// verified. e.g. "user A exists"
type State struct {
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{{Name: state}}
return p
}
// ExpectsToReceive specifies the content it is expecting to be
// given from the Provider. The function must be able to handle this
// message for the interaction to succeed.
func (p *Message) ExpectsToReceive(description string) *Message {
p.Description = description
return p
}
// WithMetadata specifies message-implementation specific metadata
// to go with the content
func (p *Message) WithMetadata(metadata MapMatcher) *Message {
p.Metadata = metadata
return p
}
// WithContent specifies the details of the HTTP request that will be used to
// confirm that the Provider provides an API listening on the given interface.
// Mandatory.
func (p *Message) WithContent(content interface{}) *Message {
p.Content = content
return p
}
// AsType specifies that the content sent through to the
// consumer handler should be sent as the given type
func (p *Message) AsType(t interface{}) *Message {
log.Println("[DEBUG] setting Message decoding to type:", reflect.TypeOf(t))
p.Type = t
return p
}