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

Commit 976e06f

Browse files
author
ronanbarrett
authored
Merge pull request #6 from sandvikcode/develop
Updated names and improved error handling
2 parents 03490b7 + 7250a5d commit 976e06f

6 files changed

Lines changed: 52 additions & 19 deletions

File tree

README.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1-
# flames-library-mockserver-client
1+
# mockserver-client-go
22

33
The mockserver client is a golang client for use with the fantastic http://www.mock-server.com/ HTTP mock server.
44

55
Usage:
6-
* Import the library `import "github.com/sandvikcode/flames-library-mockserver-client/pkg/mock"`
6+
* Import the library `import "github.com/sandvikcode/mockserver-client-go/pkg/mockclient"`
77

8-
Example:
8+
Create an expectation example:
99
```
10-
mockServer := mock.Client{
10+
mockServer := mockclient.Client{
1111
T: t,
12-
BaseURL: os.Getenv("MOCKSERVER_HOST")
12+
BaseURL: os.Getenv("MOCKSERVER_HOST"),
1313
}
1414
1515
mockServer.AddExpectation(
16-
mock.CreateExpectation(
17-
mock.WhenRequestPath("/(.*)"),
18-
mock.ThenResponseStatus(http.StatusOK),
16+
mockclient.CreateExpectation(
17+
mockclient.WhenRequestPath("/(.*)"),
18+
mockclient.ThenResponseStatus(http.StatusOK),
1919
))
2020
2121
defer mockServer.Clear("/(.*)")
2222
```
2323

24+
Create a verification example:
25+
```
26+
mockServer := mockclient.Client{
27+
T: t,
28+
BaseURL: os.Getenv("MOCKSERVER_HOST"),
29+
}
30+
31+
mockServer.AddVerification(
32+
mockclient.CreateVerification(
33+
mockclient.WhenRequestPath("/v1/jobs/(.*)"),
34+
mockclient.ThenAtLeastCalls(2),
35+
mockclient.ThenAtMostCalls(4),
36+
))
37+
```
38+
39+
Expectation Defaults:
40+
* unlimited calls will respond to a match
41+
* calls are not delayed
42+
* status of matched calls is 200 OK
43+
44+
Verification Defaults:
45+
* matched request occurs once i.e. at 1 least call and at most 1 call
46+
2447
Links:
2548
* Expectations - http://www.mock-server.com/mock_server/creating_expectations.html
2649
* Verifications - http://www.mock-server.com/mock_server/verification.html
2750
* Clearing & Resetting http://www.mock-server.com/mock_server/clearing_and_resetting.html
51+
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
package mock
1+
package mockclient
22

33
import (
44
"encoding/json"
55
"fmt"
6+
"io/ioutil"
67
"testing"
78
"time"
89

910
"net/http"
1011
"net/url"
1112
"strings"
1213

14+
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
1416
)
1517

@@ -91,11 +93,18 @@ func (c *Client) callMock(mockAPI, mockReqBody string) {
9193
}
9294
mockRes, err := hc.Do(mockReq)
9395
if err != nil {
94-
require.NoError(c.T, err, "Failed to send request to mock server.")
96+
require.NoError(c.T, err, "Failed to send request to MockServer.")
9597
}
96-
// MockServer verification returns 202 on success and 406 on failure
97-
if mockRes.StatusCode != http.StatusAccepted {
98-
require.NoError(c.T, err,
99-
fmt.Sprintf("Mock server verification did not meet expectations and failed with status: %s", mockRes.Status))
98+
99+
// all went well so return (clears & /reset return 200 whilst /verify & /expectation return 201)
100+
if mockRes.StatusCode >= 200 && mockRes.StatusCode <= 299 {
101+
return
102+
}
103+
104+
// something went wrong so return the error message (Note: MockServer /verify returns 406 on failure)
105+
b, err := ioutil.ReadAll(mockRes.Body)
106+
if err != nil {
107+
assert.Fail(c.T, fmt.Sprintf("MockServer call failed with status: %s", mockRes.Status))
100108
}
109+
assert.Fail(c.T, fmt.Sprintf("MockServer call to /%s failed with status: %s. Error message from MockServer is: %s", mockAPI, mockRes.Status, string(b)))
101110
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mock
1+
package mockclient
22

33
import (
44
"fmt"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mock
1+
package mockclient
22

33
import (
44
"encoding/json"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mock
1+
package mockclient
22

33
// VerificationSequence defines a specific sequence of calls to MockServer
44
type VerificationSequence struct {
@@ -13,7 +13,7 @@ func CreateVerification(opts ...ExpectationOption) *Expectation {
1313
Path: "/(.*)",
1414
},
1515
Times: &Times{
16-
AtLeast: 1,
16+
AtLeast: 1,
1717
AtMost: 1,
1818
},
1919
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mock
1+
package mockclient
22

33
import (
44
"encoding/json"

0 commit comments

Comments
 (0)