Skip to content

Instantly share code, notes, and snippets.

@antklim
Last active November 20, 2020 11:47
Show Gist options
  • Select an option

  • Save antklim/59abd3b3d3526a0044f547a0b93546b7 to your computer and use it in GitHub Desktop.

Select an option

Save antklim/59abd3b3d3526a0044f547a0b93546b7 to your computer and use it in GitHub Desktop.
func TestNotFoundHandler(t *testing.T) {
req := httptest.NewRequest("GET", "/not-found", nil)
rr := httptest.NewRecorder()
notFoundHandler(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.JSONEq(t, `{"message": "not found"}`, string(resBody))
}
func TestDoHandler(t *testing.T) {
req := httptest.NewRequest(
"POST",
"/do",
strings.NewReader(`{"operation": "add", "arguments": [1,2]}`))
rr := httptest.NewRecorder()
doHandler()(rr, req)
res := rr.Result()
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.JSONEq(
t,
`{"operation": "add", "arguments": [1,2], "result": 3}`,
string(resBody))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment