Created
August 2, 2016 17:54
-
-
Save gophertron/a337627530f6aaf2bbb2e6930cb3a939 to your computer and use it in GitHub Desktop.
sample httptest usage
This file contains hidden or 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
package main | |
import ( | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
func TestPingHandler(t *testing.T) { | |
ts := httptest.NewServer(http.HandlerFunc(PingHandler)) | |
resp, err := http.Get(ts.URL) | |
if err != nil { | |
t.Fatal(err) | |
} | |
if resp.StatusCode != 200 { | |
t.Fatalf("Received non-200 response: %d\n", resp.StatusCode) | |
} | |
expected := "PING" | |
actual, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
t.Fatal(err) | |
} | |
if expected != string(actual) { | |
t.Errorf("Expected the message '%s' but got '%s'\n", expected, actual) | |
} | |
t.Log("Tested OK:", resp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment