Last active
November 28, 2016 22:23
-
-
Save jlindsey/2338963c59111c31e3d0171910b09fa9 to your computer and use it in GitHub Desktop.
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 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