Skip to content

Instantly share code, notes, and snippets.

@gophertron
Created August 2, 2016 17:54
Show Gist options
  • Save gophertron/a337627530f6aaf2bbb2e6930cb3a939 to your computer and use it in GitHub Desktop.
Save gophertron/a337627530f6aaf2bbb2e6930cb3a939 to your computer and use it in GitHub Desktop.
sample httptest usage
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