To test an HTTP handler without starting a server, create a request and pass an `httptest.NewRecorder()` to the handler as its response writer. The recorder lets the test inspect the status code, headers, and body the handler produced. section 8.2.1
Check a game-creation response
Create a POST request for `/games`, call `Handle(recorder, req)`, then check `recorder.Code` for `http.StatusCreated`, `recorder.Header().Get("Content-Type")` for `application/json`, and `recorder.Body.String()` for the expected JSON. Use `require.NoError` before continuing if request creation fails; use `assert` for response checks that can report multiple failures. section 8.2.1
A recorder also works when a unit test supplies route data directly. For a `/tick/` handler, set `habitID` with `req.SetPathValue`, call the handler, and inspect `rr.Result().StatusCode` and `rr.Body.String()`. This tests the handler with prepared data, not the router’s path-value extraction; testing the full request flow calls for an integration test. section 11.3.1
Listing 8.12 newgame/handler_test.go: Testing Handle
func TestHandle(t *testing.T) {
req, err := http.NewRequest(
http.MethodPost, "/games", nil) #1
require.NoError(t, err)
recorder := httptest.NewRecorder() #2
Handle(recorder, req) #3
assert.Equal(t, http.StatusCreated, recorder.Code) #4
assert.Equal(t, "application/json",
recorder.Header().Get("Content-Type")) #4
assert.JSONEq(t, `{"id":"",...}`,
recorder.Body.String()) #4
}
The complete game-creation test shows the request, recorder, handler call, and status, header, and JSON assertions together. section 8.2.1

