liveBook · Manning

Httptest

As explained in Go in Action, Second Edition

httptest is a Go standard-library package that provides utilities for testing HTTP servers and handlers.

httptest provides Go testing utilities for isolating HTTP handlers, capturing responses, and creating temporary test servers. It makes request methods, bodies, status codes, and response data directly testable. section 8.6

Testing GET and POST handlers

A GET handler returns "hello" with status 200. A POST handler reads the request body, returns status 201, and prefixes it with "hello ". A table test checks both cases and can also cover invalid methods through a route.

How Go in Action, Second Edition covers it

How deep it goes
Demonstrates handler tests, table-driven cases, routing, and temporary servers without covering broader HTTP server internals.
Examples
Uses GET and POST hello handlers, response recording, request bodies, status assertions, and an invalid-method route case.
What it assumes
Assumes familiarity with Go handlers, HTTP methods, request bodies, response status codes, and the testing package.
What you can do after
Apply it via Go's httptest package to verify handler status codes, response bodies, methods, request bodies, and routes.
How it connects
Connects HTTP handlers, HTTP requests, HTTP responses, ServeMux routing, and table-driven tests.

Also explained in

Compare how 4 books explain httptest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.