1
1
package wiremock
2
2
3
3
import (
4
+ "encoding/base64"
4
5
"encoding/json"
5
6
"net/http"
6
7
"time"
@@ -23,7 +24,10 @@ type URLMatcherInterface interface {
23
24
}
24
25
25
26
type response struct {
26
- body string
27
+ body * string
28
+ base64Body []byte
29
+ bodyFileName * string
30
+ jsonBody interface {}
27
31
headers map [string ]string
28
32
status int64
29
33
fixedDelayMilliseconds time.Duration
@@ -89,7 +93,31 @@ func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
89
93
90
94
// WillReturn sets response and returns *StubRule
91
95
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
93
121
s .response .headers = headers
94
122
s .response .status = status
95
123
return s
@@ -173,6 +201,9 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
173
201
Request * Request `json:"request"`
174
202
Response struct {
175
203
Body string `json:"body,omitempty"`
204
+ Base64Body string `json:"base64Body,omitempty"`
205
+ BodyFileName string `json:"bodyFileName,omitempty"`
206
+ JSONBody interface {} `json:"jsonBody,omitempty"`
176
207
Headers map [string ]string `json:"headers,omitempty"`
177
208
Status int64 `json:"status,omitempty"`
178
209
FixedDelayMilliseconds int `json:"fixedDelayMilliseconds,omitempty"`
@@ -182,7 +213,17 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
182
213
jsonStubRule .ScenarioName = s .scenarioName
183
214
jsonStubRule .RequiredScenarioScenarioState = s .requiredScenarioState
184
215
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
+
186
227
jsonStubRule .Response .Headers = s .response .headers
187
228
jsonStubRule .Response .Status = s .response .status
188
229
jsonStubRule .Response .FixedDelayMilliseconds = int (s .response .fixedDelayMilliseconds .Milliseconds ())
0 commit comments