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

Commit 4451d99

Browse files
authored
Merge pull request #10 from sandvikcode/develop
Allow mock server to accept 0 when verifying number of calls
2 parents 976e06f + bff0032 commit 4451d99

5 files changed

Lines changed: 18 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Expectation Defaults:
4040
* unlimited calls will respond to a match
4141
* calls are not delayed
4242
* status of matched calls is 200 OK
43+
* body of matched calls is empty
4344

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

cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ steps:
44
- name: 'gcr.io/cloud-builders/docker'
55
args: ['build', '-t', '${_LIBRARY_NAME}', '.', '--cache-from', 'eu.gcr.io/$PROJECT_ID/${_LIBRARY_NAME}']
66
substitutions:
7-
_LIBRARY_NAME: flames-library-mockserver-client
7+
_LIBRARY_NAME: mockserver-client-go

pkg/mockclient/expectations.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type ResponseBody struct {
3737
// Times defines how many times the MockServer will serve a given request in expectation mode whilst
3838
// in verification mode defines the expected number of calls
3939
type Times struct {
40-
AtLeast int `json:"atLeast,omitempty"` // valid for verifications only
41-
AtMost int `json:"atMost,omitempty"` // valid for verifications only
40+
AtLeast *int `json:"atLeast,omitempty"` // valid for verifications only
41+
AtMost *int `json:"atMost,omitempty"` // valid for verifications only
4242
RemainingTimes int `json:"remainingTimes,omitempty"` // valid for expectations only
4343
Unlimited *bool `json:"unlimited,omitempty"` // valid for expectations only
4444
}
@@ -134,7 +134,7 @@ func WhenTimes(times int) ExpectationOption {
134134
return func(e *Expectation) *Expectation {
135135
e.Times = &Times{
136136
RemainingTimes: times,
137-
Unlimited: newBool(false),
137+
Unlimited: boolPointer(false),
138138
}
139139
return e
140140
}
@@ -196,7 +196,7 @@ func ThenResponseDelay(delay time.Duration) ExpectationOption {
196196
}
197197
}
198198

199-
func newBool(value bool) *bool {
200-
b := value
201-
return &b
199+
func boolPointer(value bool) *bool {
200+
b := value
201+
return &b
202202
}

pkg/mockclient/verifications.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
1313
Path: "/(.*)",
1414
},
1515
Times: &Times{
16-
AtLeast: 1,
17-
AtMost: 1,
16+
AtLeast: integerPointer(1),
17+
AtMost: integerPointer(1),
1818
},
1919
}
2020
// Append all options that are set (discard defaults)
@@ -28,15 +28,15 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
2828
// ThenAtLeastCalls creates a verification that a matching call was received at least x times by MockServer
2929
func ThenAtLeastCalls(times int) ExpectationOption {
3030
return func(v *Expectation) *Expectation {
31-
v.Times.AtLeast = times
31+
v.Times.AtLeast = integerPointer(times)
3232
return v
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 {
3838
return func(v *Expectation) *Expectation {
39-
v.Times.AtMost = times
39+
v.Times.AtMost = integerPointer(times)
4040
return v
4141
}
4242
}
@@ -63,3 +63,7 @@ func VerifyPath(path string) VerificationOption {
6363
}
6464
}
6565
*/
66+
67+
func integerPointer(i int) *int {
68+
return &i
69+
}

pkg/mockclient/verifications_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ func TestVerifications(t *testing.T) {
2828
"atMost": 1
2929
}
3030
}`},
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(1)), `
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)), `
3232
{
3333
"httpRequest": {
3434
"path": "/path"
3535
},
3636
"times": {
37-
"atLeast": 1,
37+
"atLeast": 0,
3838
"atMost": 1
3939
}
4040
}`},

0 commit comments

Comments
 (0)