Skip to content

Commit 8e8ac1d

Browse files
authored
Adding WillReturnBinary, WillReturnFileContent, WillReturnJSON for response (#10)
1 parent a9890bf commit 8e8ac1d

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ func TestSome(t *testing.T) {
3030
WithQueryParam("lastName", wiremock.NotMatching("Black")).
3131
WithBodyPattern(wiremock.EqualToJson(`{"meta": "information"}`)).
3232
WithHeader("x-session", wiremock.Matching("^\\S+fingerprint\\S+$")).
33-
WillReturn(
34-
`{"code": 400, "detail": "detail"}`,
33+
WillReturnJSON(
34+
map[string]interface{}{
35+
"code": 400,
36+
"detail": "detail",
37+
},
3538
map[string]string{"Content-Type": "application/json"},
3639
400,
3740
).
@@ -40,8 +43,10 @@ func TestSome(t *testing.T) {
4043
// scenario
4144
defer wiremockClient.ResetAllScenarios()
4245
wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/status")).
43-
WillReturn(
44-
`{"status": null}`,
46+
WillReturnJSON(
47+
map[string]interface{}{
48+
"status": nil,
49+
},
4550
map[string]string{"Content-Type": "application/json"},
4651
200,
4752
).
@@ -54,8 +59,10 @@ func TestSome(t *testing.T) {
5459
WillSetStateTo("Status started"))
5560

5661
statusStub := wiremock.Get(wiremock.URLPathEqualTo("/status")).
57-
WillReturn(
58-
`{"status": "started"}`,
62+
WillReturnJSON(
63+
map[string]interface{}{
64+
"status": "started",
65+
},
5966
map[string]string{"Content-Type": "application/json"},
6067
200,
6168
).

stub_rule.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package wiremock
22

33
import (
4+
"encoding/base64"
45
"encoding/json"
56
"net/http"
67
"time"
@@ -23,7 +24,10 @@ type URLMatcherInterface interface {
2324
}
2425

2526
type response struct {
26-
body string
27+
body *string
28+
base64Body []byte
29+
bodyFileName *string
30+
jsonBody interface{}
2731
headers map[string]string
2832
status int64
2933
fixedDelayMilliseconds time.Duration
@@ -89,7 +93,31 @@ func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
8993

9094
// WillReturn sets response and returns *StubRule
9195
func (s *StubRule) WillReturn(body string, headers map[string]string, status int64) *StubRule {
92-
s.response.body = body
96+
s.response.body = &body
97+
s.response.headers = headers
98+
s.response.status = status
99+
return s
100+
}
101+
102+
// WillReturnBinary sets response with binary body and returns *StubRule
103+
func (s *StubRule) WillReturnBinary(body []byte, headers map[string]string, status int64) *StubRule {
104+
s.response.base64Body = body
105+
s.response.headers = headers
106+
s.response.status = status
107+
return s
108+
}
109+
110+
// WillReturnFileContent sets response with some file content and returns *StubRule
111+
func (s *StubRule) WillReturnFileContent(bodyFileName string, headers map[string]string, status int64) *StubRule {
112+
s.response.bodyFileName = &bodyFileName
113+
s.response.headers = headers
114+
s.response.status = status
115+
return s
116+
}
117+
118+
// WillReturnJSON sets response with json body and returns *StubRule
119+
func (s *StubRule) WillReturnJSON(json interface{}, headers map[string]string, status int64) *StubRule {
120+
s.response.jsonBody = json
93121
s.response.headers = headers
94122
s.response.status = status
95123
return s
@@ -173,6 +201,9 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
173201
Request *Request `json:"request"`
174202
Response struct {
175203
Body string `json:"body,omitempty"`
204+
Base64Body string `json:"base64Body,omitempty"`
205+
BodyFileName string `json:"bodyFileName,omitempty"`
206+
JSONBody interface{} `json:"jsonBody,omitempty"`
176207
Headers map[string]string `json:"headers,omitempty"`
177208
Status int64 `json:"status,omitempty"`
178209
FixedDelayMilliseconds int `json:"fixedDelayMilliseconds,omitempty"`
@@ -182,7 +213,17 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
182213
jsonStubRule.ScenarioName = s.scenarioName
183214
jsonStubRule.RequiredScenarioScenarioState = s.requiredScenarioState
184215
jsonStubRule.NewScenarioState = s.newScenarioState
185-
jsonStubRule.Response.Body = s.response.body
216+
217+
if s.response.body != nil {
218+
jsonStubRule.Response.Body = *s.response.body
219+
} else if len(s.response.base64Body) > 0 {
220+
jsonStubRule.Response.Base64Body = base64.StdEncoding.EncodeToString(s.response.base64Body)
221+
} else if s.response.bodyFileName != nil {
222+
jsonStubRule.Response.BodyFileName = *s.response.bodyFileName
223+
} else if s.response.jsonBody != nil {
224+
jsonStubRule.Response.JSONBody = s.response.jsonBody
225+
}
226+
186227
jsonStubRule.Response.Headers = s.response.headers
187228
jsonStubRule.Response.Status = s.response.status
188229
jsonStubRule.Response.FixedDelayMilliseconds = int(s.response.fixedDelayMilliseconds.Milliseconds())

0 commit comments

Comments
 (0)