Skip to content

Instantly share code, notes, and snippets.

@antklim
Last active November 24, 2020 04:01
Show Gist options
  • Save antklim/984cceb1f33bce54c4697057e3a138f4 to your computer and use it in GitHub Desktop.
Save antklim/984cceb1f33bce54c4697057e3a138f4 to your computer and use it in GitHub Desktop.
func TestRemoteHandlerWithClientMock(t *testing.T) {
mockRes := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{"foo": "bar"}`)),
}
clientMock := mocks.HTTPClient{}
clientMock.On("Do", mock.AnythingOfType("*http.Request")).Return(mockRes, nil)
req := httptest.NewRequest("GET", "/remote", nil)
rr := httptest.NewRecorder()
remoteHandler(&clientMock)(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.JSONEq(t, `{"foo": "bar"}`, string(resBody))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment