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 #10 from sandvikcode/develop
Browse files Browse the repository at this point in the history
Allow mock server to accept 0 when verifying number of calls
  • Loading branch information
sfro authored Apr 10, 2019
2 parents 976e06f + bff0032 commit 4451d99
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', '${_LIBRARY_NAME}', '.', '--cache-from', 'eu.gcr.io/$PROJECT_ID/${_LIBRARY_NAME}']
substitutions:
_LIBRARY_NAME: flames-library-mockserver-client
_LIBRARY_NAME: mockserver-client-go
12 changes: 6 additions & 6 deletions pkg/mockclient/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type ResponseBody struct {
// Times defines how many times the MockServer will serve a given request in expectation mode whilst
// in verification mode defines the expected number of calls
type Times struct {
AtLeast int `json:"atLeast,omitempty"` // valid for verifications only
AtMost int `json:"atMost,omitempty"` // valid for verifications only
AtLeast *int `json:"atLeast,omitempty"` // valid for verifications only
AtMost *int `json:"atMost,omitempty"` // valid for verifications only
RemainingTimes int `json:"remainingTimes,omitempty"` // valid for expectations only
Unlimited *bool `json:"unlimited,omitempty"` // valid for expectations only
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func WhenTimes(times int) ExpectationOption {
return func(e *Expectation) *Expectation {
e.Times = &Times{
RemainingTimes: times,
Unlimited: newBool(false),
Unlimited: boolPointer(false),
}
return e
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func ThenResponseDelay(delay time.Duration) ExpectationOption {
}
}

func newBool(value bool) *bool {
b := value
return &b
func boolPointer(value bool) *bool {
b := value
return &b
}
12 changes: 8 additions & 4 deletions pkg/mockclient/verifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
Path: "/(.*)",
},
Times: &Times{
AtLeast: 1,
AtMost: 1,
AtLeast: integerPointer(1),
AtMost: integerPointer(1),
},
}
// Append all options that are set (discard defaults)
Expand All @@ -28,15 +28,15 @@ 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 = times
v.Times.AtLeast = integerPointer(times)
return v
}
}

// 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 = times
v.Times.AtMost = integerPointer(times)
return v
}
}
Expand All @@ -63,3 +63,7 @@ func VerifyPath(path string) VerificationOption {
}
}
*/

func integerPointer(i int) *int {
return &i
}
4 changes: 2 additions & 2 deletions pkg/mockclient/verifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestVerifications(t *testing.T) {
"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(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": 1,
"atLeast": 0,
"atMost": 1
}
}`},
Expand Down

0 comments on commit 4451d99

Please sign in to comment.