-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathclient_pact_test.go
156 lines (127 loc) · 4.39 KB
/
client_pact_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//go:build integration
package client
import (
"fmt"
"os"
"strconv"
"testing"
"net/url"
"github.com/pact-foundation/pact-go/v2/consumer"
"github.com/pact-foundation/pact-go/v2/log"
"github.com/pact-foundation/pact-go/v2/matchers"
"github.com/pact-foundation/pact-workshop-go/model"
"github.com/stretchr/testify/assert"
)
var Like = matchers.Like
var EachLike = matchers.EachLike
var Term = matchers.Term
var Regex = matchers.Regex
var HexValue = matchers.HexValue
var Identifier = matchers.Identifier
var IPAddress = matchers.IPAddress
var IPv6Address = matchers.IPv6Address
var Timestamp = matchers.Timestamp
var Date = matchers.Date
var Time = matchers.Time
var UUID = matchers.UUID
var ArrayMinLike = matchers.ArrayMinLike
type S = matchers.S
type Map = matchers.MapMatcher
var u *url.URL
var client *Client
func TestClientPact_GetUser(t *testing.T) {
log.SetLogLevel("INFO")
mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{
Consumer: os.Getenv("CONSUMER_NAME"),
Provider: os.Getenv("PROVIDER_NAME"),
LogDir: os.Getenv("LOG_DIR"),
PactDir: os.Getenv("PACT_DIR"),
})
assert.NoError(t, err)
t.Run("the user exists", func(t *testing.T) {
id := 10
err = mockProvider.
AddInteraction().
Given("User sally exists").
UponReceiving("A request to login with user 'sally'").
WithRequestPathMatcher("GET", Regex("/user/"+strconv.Itoa(id), "/user/[0-9]+"), func(b *consumer.V2RequestBuilder) {
b.Header("Authorization", Like("Bearer 2019-01-01"))
}).
WillRespondWith(200, func(b *consumer.V2ResponseBuilder) {
b.BodyMatch(model.User{}).
Header("Content-Type", Term("application/json", `application\/json`)).
Header("X-Api-Correlation-Id", Like("100"))
}).
ExecuteTest(t, func(config consumer.MockServerConfig) error {
// Act: test our API client behaves correctly
// Get the Pact mock server URL
u, _ = url.Parse("http://" + config.Host + ":" + strconv.Itoa(config.Port))
// Initialise the API client and point it at the Pact mock server
client = &Client{
BaseURL: u,
}
// // Execute the API client
user, err := client.WithToken("2019-01-01").GetUser(id)
// // Assert basic fact
if user.ID != id {
return fmt.Errorf("wanted user with ID %d but got %d", id, user.ID)
}
return err
})
assert.NoError(t, err)
})
t.Run("the user does not exist", func(t *testing.T) {
id := 10
err = mockProvider.
AddInteraction().
Given("User sally does not exist").
UponReceiving("A request to login with user 'sally'").
WithRequestPathMatcher("GET", Regex("/user/"+strconv.Itoa(id), "/user/[0-9]+"), func(b *consumer.V2RequestBuilder) {
b.Header("Authorization", Like("Bearer 2019-01-01"))
}).
WillRespondWith(404, func(b *consumer.V2ResponseBuilder) {
b.Header("Content-Type", Term("application/json", `application\/json`)).
Header("X-Api-Correlation-Id", Like("100"))
}).
ExecuteTest(t, func(config consumer.MockServerConfig) error {
// Act: test our API client behaves correctly
// Get the Pact mock server URL
u, _ = url.Parse("http://" + config.Host + ":" + strconv.Itoa(config.Port))
// Initialise the API client and point it at the Pact mock server
client = &Client{
BaseURL: u,
}
// // Execute the API client
_, err := client.WithToken("2019-01-01").GetUser(id)
assert.Equal(t, ErrNotFound, err)
return nil
})
assert.NoError(t, err)
})
t.Run("not authenticated", func(t *testing.T) {
id := 10
err = mockProvider.
AddInteraction().
Given("User is not authenticated").
UponReceiving("A request to login with user 'sally'").
WithRequestPathMatcher("GET", Regex("/user/"+strconv.Itoa(id), "/user/[0-9]+")).
WillRespondWith(401, func(b *consumer.V2ResponseBuilder) {
b.Header("Content-Type", Term("application/json", `application\/json`)).
Header("X-Api-Correlation-Id", Like("100"))
}).
ExecuteTest(t, func(config consumer.MockServerConfig) error {
// Act: test our API client behaves correctly
// Get the Pact mock server URL
u, _ = url.Parse("http://" + config.Host + ":" + strconv.Itoa(config.Port))
// Initialise the API client and point it at the Pact mock server
client = &Client{
BaseURL: u,
}
// // Execute the API client
_, err := client.WithToken("").GetUser(id)
assert.Equal(t, ErrUnauthorized, err)
return nil
})
assert.NoError(t, err)
})
}