Simple Go HTTP Test
package main | |
import ( | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
func TestIndexHandler(t *testing.T) { | |
w := httptest.NewRecorder() | |
r := httptest.NewRequest(http.MethodGet, "/", nil) | |
IndexHandler(w, r) | |
resp := w.Result() | |
body, _ := ioutil.ReadAll(resp.Body) | |
//fmt.Println(resp.StatusCode) | |
//fmt.Println(resp.Header.Get("Content-Type")) | |
//fmt.Println(string(body)) | |
got := string(body) | |
want := "Hello World" | |
if got != want { | |
t.Errorf("got %q, wanted %q", got, want) | |
} | |
if resp.StatusCode != 200 { | |
t.Errorf("Status was not 200, got %q instead", resp.StatusCode) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment