httptest
Go's standard library package of helpers for testing HTTP code, such as a recorder that captures what a handler writes.
In more detail
httptest.NewRequest builds a request and httptest.NewRecorder captures the status, headers and body, so a handler can be tested in memory with no port or network. httptest.NewServer starts a real local server when a client needs one.
Example
recorder := httptest.NewRecorder()
health(recorder, httptest.NewRequest(http.MethodGet, "/health", nil))Related terms
- http.Handler — In Go, a function or type that receives an HTTP request and writes the response, with the signature func(w http.ResponseWriter, r *http.Request).
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- deterministic — Always producing the same result from the same inputs, with nothing left to chance or timing.
Taught in
- Serving HTTP with net/http Server — Concurrent Systems Programming with Go