Skip to content

Commit

Permalink
Support JSON marshalling any type when setting the mock req/res body
Browse files Browse the repository at this point in the history
This aligns the mock API with the apitest API for creating a request
  • Loading branch information
Stein Fletcher committed Mar 1, 2021
1 parent 3bb39ff commit a809d7c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,20 @@ func (r *MockRequest) BodyFromFile(f string) *MockRequest {
return r
}

// JSON is a convenience method for setting the mock request body
func (r *MockRequest) JSON(v interface{}) *MockRequest {
switch x := v.(type) {
case string:
r.body = x
case []byte:
r.body = string(x)
default:
asJSON, _ := json.Marshal(x)
r.body = string(asJSON)
}
return r
}

// Header configures the mock request to match the given header
func (r *MockRequest) Header(key, value string) *MockRequest {
normalizedKey := textproto.CanonicalMIMEHeaderKey(key)
Expand Down Expand Up @@ -660,6 +674,20 @@ func (r *MockResponse) BodyFromFile(f string) *MockResponse {
return r
}

// JSON is a convenience method for setting the mock response body
func (r *MockResponse) JSON(v interface{}) *MockResponse {
switch x := v.(type) {
case string:
r.body = x
case []byte:
r.body = string(x)
default:
asJSON, _ := json.Marshal(x)
r.body = string(asJSON)
}
return r
}

// Status respond with the given status
func (r *MockResponse) Status(statusCode int) *MockResponse {
r.statusCode = statusCode
Expand Down
30 changes: 30 additions & 0 deletions mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,23 @@ func TestMocks_BodyMatcher(t *testing.T) {
}
}

func TestMocks_RequestBody(t *testing.T) {
tests := map[string]struct {
requestBody interface{}
}{
"supports string input": {`{"a":1}`},
"supports maps": {map[string]int{"a": 1}},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/path", strings.NewReader(`{"a":1}`))
err := bodyMatcher(req, NewMock().Get("/").JSON(test.requestBody))
assert.NoError(t, err)
})
}
}

func TestMocks_PathMatcher(t *testing.T) {
tests := []struct {
requestUrl string
Expand Down Expand Up @@ -952,6 +969,19 @@ func TestMocks_Response_SetsTheBodyAsJSON(t *testing.T) {
assert.Equal(t, "application/json", response.Header.Get("Content-Type"))
}

func TestMocks_ResponseJSON(t *testing.T) {
mockResponse := NewMock().
Get("assert").
RespondWith().
JSON(map[string]int{"a": 123})

response := buildResponseFromMock(mockResponse)

bytes, _ := ioutil.ReadAll(response.Body)
assert.Equal(t, string(bytes), `{"a":123}`)
assert.Equal(t, "application/json", response.Header.Get("Content-Type"))
}

func TestMocks_Response_SetsTheBodyAsOther(t *testing.T) {
mockResponse := NewMock().
Get("assert").
Expand Down

0 comments on commit a809d7c

Please sign in to comment.