-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
consumer_v3_test.go
139 lines (124 loc) · 3.85 KB
/
consumer_v3_test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//go:build consumer
// +build consumer
// Package main contains a runnable Consumer Pact test example.
package main
import (
"errors"
"testing"
"github.com/pact-foundation/pact-go/v2/consumer"
"github.com/pact-foundation/pact-go/v2/log"
"github.com/pact-foundation/pact-go/v2/matchers"
message "github.com/pact-foundation/pact-go/v2/message/v3"
"github.com/pact-foundation/pact-go/v2/models"
"github.com/stretchr/testify/assert"
)
var Decimal = matchers.Decimal
var Integer = matchers.Integer
var Equality = matchers.Equality
var Includes = matchers.Includes
var FromProviderState = matchers.FromProviderState
var EachKeyLike = matchers.EachKeyLike
var ArrayContaining = matchers.ArrayContaining
var ArrayMinMaxLike = matchers.ArrayMinMaxLike
var ArrayMaxLike = matchers.ArrayMaxLike
var DateGenerated = matchers.DateGenerated
var TimeGenerated = matchers.TimeGenerated
var DateTimeGenerated = matchers.DateTimeGenerated
func TestConsumerV3(t *testing.T) {
log.SetLogLevel("INFO")
mockProvider, err := consumer.NewV3Pact(consumer.MockHTTPProviderConfig{
Consumer: "PactGoV3Consumer",
Provider: "V3Provider",
Host: "127.0.0.1",
TLS: true,
})
assert.NoError(t, err)
// Set up our expected interactions.
err = mockProvider.
AddInteraction().
Given("state 1").
GivenWithParameter(models.ProviderState{
Name: "User foo exists",
Parameters: map[string]interface{}{
"id": "foo",
},
}).
UponReceiving("A request to do a foo").
WithRequest("POST", "/foobar", func(b *consumer.V3RequestBuilder) {
b.
Header("Content-Type", S("application/json")).
Header("Authorization", Like("Bearer 1234")).
Query("baz", Regex("bar", "[a-z]+"), Regex("bat", "[a-z]+"), Regex("baz", "[a-z]+")).
JSONBody(Map{
"id": Like(27),
"name": FromProviderState("${name}", "billy"),
"lastName": Like("billy"),
"datetime": DateTimeGenerated("2020-01-01T08:00:45", "yyyy-MM-dd'T'HH:mm:ss"),
})
}).
WillRespondWith(200, func(b *consumer.V3ResponseBuilder) {
b.
Header("Content-Type", S("application/json")).
JSONBody(Map{
"datetime": Regex("2020-01-01", "[0-9\\-]+"),
"name": S("Billy"),
"lastName": S("Sampson"),
"superstring": Includes("foo"),
"id": Integer(12),
"accountBalance": Decimal(123.76),
"itemsMinMax": ArrayMinMaxLike(27, 3, 5),
"itemsMin": ArrayMinLike("thereshouldbe3ofthese", 3),
"equality": Equality("a thing"),
"arrayContaining": ArrayContaining([]interface{}{
Like("string"),
Integer(1),
Map{
"foo": Like("bar"),
},
}),
})
}).
ExecuteTest(t, test)
assert.NoError(t, err)
}
func TestMessagePact(t *testing.T) {
log.SetLogLevel("INFO")
provider, err := message.NewMessagePact(message.Config{
Consumer: "PactGoV3MessageConsumer",
Provider: "V3MessageProvider", // must be different to the HTTP one, can't mix both interaction styles
})
assert.NoError(t, err)
err = provider.AddMessage().
GivenWithParameter(models.ProviderState{
Name: "User with id 127 exists",
Parameters: map[string]interface{}{
"id": 127,
},
}).
ExpectsToReceive("a user event").
WithMetadata(map[string]string{
"Content-Type": "application/json",
}).
WithJSONContent(Map{
"datetime": Regex("2020-01-01", "[0-9\\-]+"),
"name": S("Billy"),
"lastName": S("Sampson"),
"id": Integer(12),
}).
AsType(&User{}).
ConsumedBy(userHandlerWrapper).
Verify(t)
assert.NoError(t, err)
}
// Message Pact - wrapped handler extracts the message
var userHandlerWrapper = func(m message.MessageContents) error {
return userHandler(*m.Content.(*User))
}
// Message Pact - actual handler
var userHandler = func(u User) error {
if u.ID == 0 {
return errors.New("invalid object supplied, missing fields (id)")
}
// ... actually consume the message
return nil
}