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

Commit

Permalink
Merge pull request #12 from sandvikcode/develop
Browse files Browse the repository at this point in the history
License & Sequence
  • Loading branch information
ronanbarrett authored May 6, 2019
2 parents 4451d99 + 1091fe6 commit e26f6fd
Show file tree
Hide file tree
Showing 6 changed files with 786 additions and 91 deletions.
5 changes: 4 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Usage:
Create an expectation example:
```
mockServer := mockclient.Client{
T: t,
T: t,
BaseURL: os.Getenv("MOCKSERVER_HOST"),
}
Expand All @@ -24,7 +24,7 @@ defer mockServer.Clear("/(.*)")
Create a verification example:
```
mockServer := mockclient.Client{
T: t,
T: t,
BaseURL: os.Getenv("MOCKSERVER_HOST"),
}
Expand All @@ -36,14 +36,40 @@ mockServer.AddVerification(
))
```

Expectation Defaults:
Create a verification sequence example:
```
mockServer := mockclient.Client{
T: t,
BaseURL: os.Getenv("MOCKSERVER_HOST"),
}
mockServer.AddVerificationSequence(
mockclient.CreateVerification(
mockclient.WhenRequestPath("/a"),
),
mockclient.CreateVerification(
mockclient.WhenRequestPath("/b(.*)"),
),
mockclient.CreateVerification(
mockclient.WhenRequestPath("/c"),
mockclient.WhenRequestMethod("POST"),
),
)
```

Expectation defaults:
* unlimited calls will respond to a match
* calls are not delayed
* status of matched calls is 200 OK
* body of matched calls is empty

Verification Defaults:
* matched request occurs once i.e. at 1 least call and at most 1 call
Verification defaults:
* matched request occurs once i.e. at 1 least call and at most 1 call

Verification sequence notes:
* the order of the requests matters as the requests form a sequence to be verified
* only the request part is used for matching the sequence i.e. request count is not applicable

Links:
* Expectations - http://www.mock-server.com/mock_server/creating_expectations.html
Expand Down
11 changes: 7 additions & 4 deletions pkg/mockclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ func (c *Client) AddVerification(exp *Expectation) {
c.callMock("verify", string(msg))
}

/*
// AddVerificationSequence adds a verification of a specific sequence of requests to MockServer
func (c *Client) AddVerificationSequence(v []*VerificationSequence) {
msg, err := json.Marshal(v)
func (c *Client) AddVerificationSequence(exps ...*Expectation) {
vs := &VerificationSequence{}
for _, exp := range exps {
// Only request part of the expectation will be used for verification sequences
vs.Requests = append(vs.Requests, exp.Request)
}
msg, err := json.Marshal(vs)
if err != nil {
require.NoError(c.T, err,
"Failed to serialize mock server verification sequence.")
}

c.callMock("verifySequence", string(msg))
}
*/

// Clear everything that matches a given path in MockServer
func (c *Client) Clear(path string) {
Expand Down
39 changes: 8 additions & 31 deletions pkg/mockclient/verifications.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mockclient

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

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

// ThenAtLeastCalls creates a verification that a matching call was received at least x times by MockServer
func ThenAtLeastCalls(times int) ExpectationOption {
return func(v *Expectation) *Expectation {
v.Times.AtLeast = integerPointer(times)
return v
return func(e *Expectation) *Expectation {
e.Times.AtLeast = integerPointer(times)
return e
}
}

// ThenAtMostCalls creates a verification that a matching call was received at most x times by MockServer
func ThenAtMostCalls(times int) ExpectationOption {
return func(v *Expectation) *Expectation {
v.Times.AtMost = integerPointer(times)
return v
return func(e *Expectation) *Expectation {
e.Times.AtMost = integerPointer(times)
return e
}
}

/*
// VerificationOption enables building verifications in many parts
type VerificationOption func(e *VerificationSequence) *VerificationSequence
// CreateVerificationSequence creates a verification for a given expectation sequence
func CreateVerificationSequence(opts ...VerificationOption) []*VerificationSequence {
vsArray := make([]*VerificationSequence, 0)
for _, opt := range opts {
v := &VerificationSequence{}
vsArray = append(vsArray, opt(v))
}
return vsArray
}
func VerifyPath(path string) VerificationOption {
return func(vs *VerificationSequence) *VerificationSequence {
vs.Path = path
return vs
}
}
*/

func integerPointer(i int) *int {
return &i
}
112 changes: 62 additions & 50 deletions pkg/mockclient/verifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,47 @@ func TestVerifications(t *testing.T) {
expectation *Expectation
expectedJSON string
}{
{"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")), `
{
"httpRequest": {
"path": "/path"
},
"times": {
"atLeast": 1,
"atMost": 1
}
}`},
{"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)), `
{
"httpRequest": {
"path": "/path"
},
"times": {
"atLeast": 0,
"atMost": 1
}
}`},
{"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)), `
{
"httpRequest": {
"path": "/path"
},
"times": {
"atLeast": 5,
"atMost": 10
}
}`},
{"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")), `
{
"httpRequest": {
"path": "/path"
},
"times": {
"atLeast": 1,
"atMost": 1
}
}`},
{"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")), `
{
"httpRequest": {
"path": "/path",
"method": "GET"
},
"times": {
"atLeast": 1,
"atMost": 1
}
}`},
{"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)), `
{
"httpRequest": {
"path": "/path"
},
"times": {
"atLeast": 0,
"atMost": 1
}
}`},
{"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)), `
{
"httpRequest": {
"path": "/path"
},
"times": {
"atLeast": 5,
"atMost": 10
}
}`},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -79,27 +90,29 @@ func TestVerifications(t *testing.T) {
}
}

/*
func TestVerificationSequence(t *testing.T) {

// Define test table
testCases := []struct {
description string
verificationSequence []*VerificationSequence
expectedJSON string
description string
expectations []*Expectation
expectedJSON string
}{
{"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")), `
[
{
"path": "/some/path/one"
},
{
"path": "/some/path/two"
},
{
"path": "/some/path/three"
}
]`},
{"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"))}, `
{
"httpRequests": [
{
"path": "/some/path/one"
},
{
"path": "/some/path/two"
},
{
"path": "/some/path/three",
"method": "POST"
}
]
}`},
}

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

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

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

Expand All @@ -126,8 +139,7 @@ func TestVerificationSequence(t *testing.T) {
BaseURL: ts.URL,
T: t,
}
mockClient.AddVerificationSequence(tc.verificationSequence)
mockClient.AddVerificationSequence(tc.expectations...)
})
}
}
*/

0 comments on commit e26f6fd

Please sign in to comment.