Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Commit 1091fe6

Browse files
author
ronanbarrett
authored
Added mock sequence
1 parent 7396a70 commit 1091fe6

File tree

5 files changed

+112
-91
lines changed

5 files changed

+112
-91
lines changed

Gopkg.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Usage:
88
Create an expectation example:
99
```
1010
mockServer := mockclient.Client{
11-
T: t,
11+
T: t,
1212
BaseURL: os.Getenv("MOCKSERVER_HOST"),
1313
}
1414
@@ -24,7 +24,7 @@ defer mockServer.Clear("/(.*)")
2424
Create a verification example:
2525
```
2626
mockServer := mockclient.Client{
27-
T: t,
27+
T: t,
2828
BaseURL: os.Getenv("MOCKSERVER_HOST"),
2929
}
3030
@@ -36,14 +36,40 @@ mockServer.AddVerification(
3636
))
3737
```
3838

39-
Expectation Defaults:
39+
Create a verification sequence example:
40+
```
41+
mockServer := mockclient.Client{
42+
T: t,
43+
BaseURL: os.Getenv("MOCKSERVER_HOST"),
44+
}
45+
46+
mockServer.AddVerificationSequence(
47+
mockclient.CreateVerification(
48+
mockclient.WhenRequestPath("/a"),
49+
),
50+
mockclient.CreateVerification(
51+
mockclient.WhenRequestPath("/b(.*)"),
52+
),
53+
mockclient.CreateVerification(
54+
mockclient.WhenRequestPath("/c"),
55+
mockclient.WhenRequestMethod("POST"),
56+
),
57+
)
58+
59+
```
60+
61+
Expectation defaults:
4062
* unlimited calls will respond to a match
4163
* calls are not delayed
4264
* status of matched calls is 200 OK
4365
* body of matched calls is empty
4466

45-
Verification Defaults:
46-
* matched request occurs once i.e. at 1 least call and at most 1 call
67+
Verification defaults:
68+
* matched request occurs once i.e. at 1 least call and at most 1 call
69+
70+
Verification sequence notes:
71+
* the order of the requests matters as the requests form a sequence to be verified
72+
* only the request part is used for matching the sequence i.e. request count is not applicable
4773

4874
Links:
4975
* Expectations - http://www.mock-server.com/mock_server/creating_expectations.html

pkg/mockclient/client.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ func (c *Client) AddVerification(exp *Expectation) {
4545
c.callMock("verify", string(msg))
4646
}
4747

48-
/*
4948
// AddVerificationSequence adds a verification of a specific sequence of requests to MockServer
50-
func (c *Client) AddVerificationSequence(v []*VerificationSequence) {
51-
msg, err := json.Marshal(v)
49+
func (c *Client) AddVerificationSequence(exps ...*Expectation) {
50+
vs := &VerificationSequence{}
51+
for _, exp := range exps {
52+
// Only request part of the expectation will be used for verification sequences
53+
vs.Requests = append(vs.Requests, exp.Request)
54+
}
55+
msg, err := json.Marshal(vs)
5256
if err != nil {
5357
require.NoError(c.T, err,
5458
"Failed to serialize mock server verification sequence.")
5559
}
5660

5761
c.callMock("verifySequence", string(msg))
5862
}
59-
*/
6063

6164
// Clear everything that matches a given path in MockServer
6265
func (c *Client) Clear(path string) {

pkg/mockclient/verifications.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package mockclient
22

3-
// VerificationSequence defines a specific sequence of calls to MockServer
3+
// VerificationSequence defines a specific ordered sequence of requests to MockServer
44
type VerificationSequence struct {
5-
Path string `json:"path,omitempty"`
5+
Requests []*RequestMatcher `json:"httpRequests"`
66
}
77

88
// CreateVerification converts a number of expectation parts (options) into a single Expectation
@@ -27,43 +27,20 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
2727

2828
// ThenAtLeastCalls creates a verification that a matching call was received at least x times by MockServer
2929
func ThenAtLeastCalls(times int) ExpectationOption {
30-
return func(v *Expectation) *Expectation {
31-
v.Times.AtLeast = integerPointer(times)
32-
return v
30+
return func(e *Expectation) *Expectation {
31+
e.Times.AtLeast = integerPointer(times)
32+
return e
3333
}
3434
}
3535

3636
// ThenAtMostCalls creates a verification that a matching call was received at most x times by MockServer
3737
func ThenAtMostCalls(times int) ExpectationOption {
38-
return func(v *Expectation) *Expectation {
39-
v.Times.AtMost = integerPointer(times)
40-
return v
38+
return func(e *Expectation) *Expectation {
39+
e.Times.AtMost = integerPointer(times)
40+
return e
4141
}
4242
}
4343

44-
/*
45-
// VerificationOption enables building verifications in many parts
46-
type VerificationOption func(e *VerificationSequence) *VerificationSequence
47-
48-
// CreateVerificationSequence creates a verification for a given expectation sequence
49-
func CreateVerificationSequence(opts ...VerificationOption) []*VerificationSequence {
50-
vsArray := make([]*VerificationSequence, 0)
51-
for _, opt := range opts {
52-
v := &VerificationSequence{}
53-
vsArray = append(vsArray, opt(v))
54-
}
55-
56-
return vsArray
57-
}
58-
59-
func VerifyPath(path string) VerificationOption {
60-
return func(vs *VerificationSequence) *VerificationSequence {
61-
vs.Path = path
62-
return vs
63-
}
64-
}
65-
*/
66-
6744
func integerPointer(i int) *int {
6845
return &i
6946
}

pkg/mockclient/verifications_test.go

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,47 @@ func TestVerifications(t *testing.T) {
1818
expectation *Expectation
1919
expectedJSON string
2020
}{
21-
{"Verify the MockServer was called at least 1 times, and at most 1 times, for a given path, by using the defaults.", CreateVerification(WhenRequestPath("/path")), `
22-
{
23-
"httpRequest": {
24-
"path": "/path"
25-
},
26-
"times": {
27-
"atLeast": 1,
28-
"atMost": 1
29-
}
30-
}`},
31-
{"Verify the MockServer was called at least 0 times, and at most 1 times, for a given path, by using the default atMost.", CreateVerification(WhenRequestPath("/path"), ThenAtLeastCalls(0)), `
32-
{
33-
"httpRequest": {
34-
"path": "/path"
35-
},
36-
"times": {
37-
"atLeast": 0,
38-
"atMost": 1
39-
}
40-
}`},
41-
{"Verify the MockServer was called at least 5 times, and at most 10 times, for a given path.", CreateVerification(WhenRequestPath("/path"), ThenAtLeastCalls(5), ThenAtMostCalls(10)), `
42-
{
43-
"httpRequest": {
44-
"path": "/path"
45-
},
46-
"times": {
47-
"atLeast": 5,
48-
"atMost": 10
49-
}
50-
}`},
21+
{"Verify the MockServer was called at least 1 times, and at most 1 times, for a given path, by using the defaults.", CreateVerification(WhenRequestPath("/path")), `
22+
{
23+
"httpRequest": {
24+
"path": "/path"
25+
},
26+
"times": {
27+
"atLeast": 1,
28+
"atMost": 1
29+
}
30+
}`},
31+
{"Verify the MockServer was called at least 1 times, and at most 1 times, for a given path and given HTTP method, by using the defaults.", CreateVerification(WhenRequestPath("/path"), WhenRequestMethod("GET")), `
32+
{
33+
"httpRequest": {
34+
"path": "/path",
35+
"method": "GET"
36+
},
37+
"times": {
38+
"atLeast": 1,
39+
"atMost": 1
40+
}
41+
}`},
42+
{"Verify the MockServer was called at least 0 times, and at most 1 times, for a given path, by using the default atMost.", CreateVerification(WhenRequestPath("/path"), ThenAtLeastCalls(0)), `
43+
{
44+
"httpRequest": {
45+
"path": "/path"
46+
},
47+
"times": {
48+
"atLeast": 0,
49+
"atMost": 1
50+
}
51+
}`},
52+
{"Verify the MockServer was called at least 5 times, and at most 10 times, for a given path.", CreateVerification(WhenRequestPath("/path"), ThenAtLeastCalls(5), ThenAtMostCalls(10)), `
53+
{
54+
"httpRequest": {
55+
"path": "/path"
56+
},
57+
"times": {
58+
"atLeast": 5,
59+
"atMost": 10
60+
}
61+
}`},
5162
}
5263

5364
for _, tc := range testCases {
@@ -79,27 +90,29 @@ func TestVerifications(t *testing.T) {
7990
}
8091
}
8192

82-
/*
8393
func TestVerificationSequence(t *testing.T) {
8494

8595
// Define test table
8696
testCases := []struct {
87-
description string
88-
verificationSequence []*VerificationSequence
89-
expectedJSON string
97+
description string
98+
expectations []*Expectation
99+
expectedJSON string
90100
}{
91-
{"Verify the MockServer was called with these specific calls in this specific order.", CreateVerificationSequence(VerifyPath("/some/path/one"), VerifyPath("/some/path/two"), VerifyPath("/some/path/three")), `
92-
[
93-
{
94-
"path": "/some/path/one"
95-
},
96-
{
97-
"path": "/some/path/two"
98-
},
99-
{
100-
"path": "/some/path/three"
101-
}
102-
]`},
101+
{"Verify the MockServer was called with these specific calls in this specific order.", []*Expectation{CreateVerification(WhenRequestPath("/some/path/one")), CreateVerification(WhenRequestPath("/some/path/two")), CreateVerification(WhenRequestPath("/some/path/three"), WhenRequestMethod("POST"))}, `
102+
{
103+
"httpRequests": [
104+
{
105+
"path": "/some/path/one"
106+
},
107+
{
108+
"path": "/some/path/two"
109+
},
110+
{
111+
"path": "/some/path/three",
112+
"method": "POST"
113+
}
114+
]
115+
}`},
103116
}
104117

105118
for _, tc := range testCases {
@@ -109,11 +122,11 @@ func TestVerificationSequence(t *testing.T) {
109122
body, err := ioutil.ReadAll(r.Body)
110123
require.NoError(t, err, "Body reader must not return an error.")
111124

112-
var bodyMap []map[string]interface{}
125+
bodyMap := make(map[string]interface{})
113126
err = json.Unmarshal(body, &bodyMap)
114127
require.NoError(t, err, "Body un-marshall must not return an error.")
115128

116-
var expectedMap []map[string]interface{}
129+
expectedMap := make(map[string]interface{})
117130
err = json.Unmarshal([]byte(tc.expectedJSON), &expectedMap)
118131
require.NoError(t, err, "Body un-marshall must not return an error.")
119132

@@ -126,8 +139,7 @@ func TestVerificationSequence(t *testing.T) {
126139
BaseURL: ts.URL,
127140
T: t,
128141
}
129-
mockClient.AddVerificationSequence(tc.verificationSequence)
142+
mockClient.AddVerificationSequence(tc.expectations...)
130143
})
131144
}
132145
}
133-
*/

0 commit comments

Comments
 (0)