Testing HTTP handlers

Testing the server without a server

In module 06 you wrote HTTP handlers β€” functions with the signature func(w http.ResponseWriter, r *http.Request). How do you test one without actually starting a server and firing real network requests at a port?

The standard library's net/http/httptest package is the answer. It hands you a fake request and a fake response writer, you call your handler directly with them, and then you inspect what it wrote. No ports, no networking, no flakiness β€” just a function call.