Skip to content

Instantly share code, notes, and snippets.

@alekc
Created October 5, 2019 08:41
Show Gist options
  • Save alekc/5172866d02d65d909f1879a0272f88cf to your computer and use it in GitHub Desktop.
Save alekc/5172866d02d65d909f1879a0272f88cf to your computer and use it in GitHub Desktop.
func TestGetEntryByID(t *testing.T) {
req, err := http.NewRequest("GET", "/entry", nil)
if err != nil {
t.Fatal(err)
}
q := req.URL.Query()
q.Add("id", "1")
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetEntryByID)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `{"id":1,"first_name":"Krish","last_name":"Bhanushali","email_address":"krishsb2405@gmail.com","phone_number":"0987654321"}`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment