Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created July 10, 2017 15:02
Show Gist options
  • Save mindscratch/61ed738c91e9b6ecc089c0dd91263c49 to your computer and use it in GitHub Desktop.
Save mindscratch/61ed738c91e9b6ecc089c0dd91263c49 to your computer and use it in GitHub Desktop.
[golang] http handler test
package main
import (
“net/http”
“net/http/httptest”
“testing”
)
fun TestHandler(t *testing.T) {
req, err := http.NewRequest(
http.MethodGet,
“http://localhost/foobar”,
nil,
)
if err != nil {
t.Fatalf(“could not create request: %v”, err)
}
// recorder satisfies the http response interface
rec := httptest.NewRecorder()
// handler is some http handler function we wrote that we want to test
handler(rec, req)
if rec.Code != http.StatusOK {
t.Errorf(“expected status 200; got %d”, rec.Code)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment