Skip to content

Instantly share code, notes, and snippets.

@sudaraka94
Created June 20, 2020 10:49
Show Gist options
  • Save sudaraka94/fedc57b08f60f8793357074dde9868fc to your computer and use it in GitHub Desktop.
Save sudaraka94/fedc57b08f60f8793357074dde9868fc to your computer and use it in GitHub Desktop.
How to test a serverless function in go
func TestF(t *testing.T) {
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler := http.HandlerFunc(F)
handler.ServeHTTP(w, r)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Errorf("wrong status code: got %v want %v", resp.StatusCode, http.StatusOK)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "Hello, World!\n" {
t.Errorf("wrong response body: got %v want %v", body, "Hello, World!\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment