Created
June 20, 2020 10:49
-
-
Save sudaraka94/fedc57b08f60f8793357074dde9868fc to your computer and use it in GitHub Desktop.
How to test a serverless function in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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