Skip to content

Instantly share code, notes, and snippets.

@jlindsey
Last active November 28, 2016 22:23
Show Gist options
  • Save jlindsey/2338963c59111c31e3d0171910b09fa9 to your computer and use it in GitHub Desktop.
Save jlindsey/2338963c59111c31e3d0171910b09fa9 to your computer and use it in GitHub Desktop.
func withTestServer(handler http.HandlerFunc, f func(*httptest.Server)) {
server := httptest.NewServer(handler)
defer server.Close()
f(server)
}
func TestSomething(t *testing.T) {
hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.URL.Path)
})
withTestServer(hf, func(server *httptest.Server) {
resp := http.Get(server.URL)
// ...
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment