Skip to content

Commit

Permalink
Adding WillReturnBinary, WillReturnFileContent, WillReturnJSON for re…
Browse files Browse the repository at this point in the history
…sponse (#10)
  • Loading branch information
walkerus authored Dec 16, 2022
1 parent a9890bf commit 8e8ac1d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ func TestSome(t *testing.T) {
WithQueryParam("lastName", wiremock.NotMatching("Black")).
WithBodyPattern(wiremock.EqualToJson(`{"meta": "information"}`)).
WithHeader("x-session", wiremock.Matching("^\\S+fingerprint\\S+$")).
WillReturn(
`{"code": 400, "detail": "detail"}`,
WillReturnJSON(
map[string]interface{}{
"code": 400,
"detail": "detail",
},
map[string]string{"Content-Type": "application/json"},
400,
).
Expand All @@ -40,8 +43,10 @@ func TestSome(t *testing.T) {
// scenario
defer wiremockClient.ResetAllScenarios()
wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/status")).
WillReturn(
`{"status": null}`,
WillReturnJSON(
map[string]interface{}{
"status": nil,
},
map[string]string{"Content-Type": "application/json"},
200,
).
Expand All @@ -54,8 +59,10 @@ func TestSome(t *testing.T) {
WillSetStateTo("Status started"))

statusStub := wiremock.Get(wiremock.URLPathEqualTo("/status")).
WillReturn(
`{"status": "started"}`,
WillReturnJSON(
map[string]interface{}{
"status": "started",
},
map[string]string{"Content-Type": "application/json"},
200,
).
Expand Down
47 changes: 44 additions & 3 deletions stub_rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wiremock

import (
"encoding/base64"
"encoding/json"
"net/http"
"time"
Expand All @@ -23,7 +24,10 @@ type URLMatcherInterface interface {
}

type response struct {
body string
body *string
base64Body []byte
bodyFileName *string
jsonBody interface{}
headers map[string]string
status int64
fixedDelayMilliseconds time.Duration
Expand Down Expand Up @@ -89,7 +93,31 @@ func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {

// WillReturn sets response and returns *StubRule
func (s *StubRule) WillReturn(body string, headers map[string]string, status int64) *StubRule {
s.response.body = body
s.response.body = &body
s.response.headers = headers
s.response.status = status
return s
}

// WillReturnBinary sets response with binary body and returns *StubRule
func (s *StubRule) WillReturnBinary(body []byte, headers map[string]string, status int64) *StubRule {
s.response.base64Body = body
s.response.headers = headers
s.response.status = status
return s
}

// WillReturnFileContent sets response with some file content and returns *StubRule
func (s *StubRule) WillReturnFileContent(bodyFileName string, headers map[string]string, status int64) *StubRule {
s.response.bodyFileName = &bodyFileName
s.response.headers = headers
s.response.status = status
return s
}

// WillReturnJSON sets response with json body and returns *StubRule
func (s *StubRule) WillReturnJSON(json interface{}, headers map[string]string, status int64) *StubRule {
s.response.jsonBody = json
s.response.headers = headers
s.response.status = status
return s
Expand Down Expand Up @@ -173,6 +201,9 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
Request *Request `json:"request"`
Response struct {
Body string `json:"body,omitempty"`
Base64Body string `json:"base64Body,omitempty"`
BodyFileName string `json:"bodyFileName,omitempty"`
JSONBody interface{} `json:"jsonBody,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Status int64 `json:"status,omitempty"`
FixedDelayMilliseconds int `json:"fixedDelayMilliseconds,omitempty"`
Expand All @@ -182,7 +213,17 @@ func (s *StubRule) MarshalJSON() ([]byte, error) {
jsonStubRule.ScenarioName = s.scenarioName
jsonStubRule.RequiredScenarioScenarioState = s.requiredScenarioState
jsonStubRule.NewScenarioState = s.newScenarioState
jsonStubRule.Response.Body = s.response.body

if s.response.body != nil {
jsonStubRule.Response.Body = *s.response.body
} else if len(s.response.base64Body) > 0 {
jsonStubRule.Response.Base64Body = base64.StdEncoding.EncodeToString(s.response.base64Body)
} else if s.response.bodyFileName != nil {
jsonStubRule.Response.BodyFileName = *s.response.bodyFileName
} else if s.response.jsonBody != nil {
jsonStubRule.Response.JSONBody = s.response.jsonBody
}

jsonStubRule.Response.Headers = s.response.headers
jsonStubRule.Response.Status = s.response.status
jsonStubRule.Response.FixedDelayMilliseconds = int(s.response.fixedDelayMilliseconds.Milliseconds())
Expand Down

0 comments on commit 8e8ac1d

Please sign in to comment.