API Reference

httptest

Standard library helpers in Go for exercising HTTP handlers and clients without real network sockets.

Official Documentation ↗
CompatibilityThis documentation covers version net/http/httptest (stdlib). Check the official docs for the latest API surface.

Installation

gogo doc net/http/httptest

Methods

Standard library helpers in Go for exercising HTTP handlers and clients without real network sockets.

Overview

httptest provides the two halves of HTTP testing in the standard library. NewRequest with NewRecorder tests handlers in memory: you build the request, invoke ServeHTTP directly, and assert against captured status, headers, and body.

NewServer and NewTLSServer stand up real loopback listeners when the code under test is an HTTP client, including TLS with a pre-trusted test certificate. The package is the backbone of idiomatic Go web testing at every layer.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • httptest.NewRequest — Builds a request with a server-side context; target may include a query string.
  • httptest.NewRecorder — Fakes a ResponseWriter, capturing status, headers, and body for direct assertions.
  • httptest.NewServer — Runs a real listener on localhost for integration-style tests of actual HTTP clients.
  • NewTLSServer — Serves over TLS with a self-signed cert; ts.Client trusts that certificate automatically.

Usage Notes

  • Assert response headers explicitly, Content-Type and Location, not just status codes.
  • Always defer ts.Close() so parallel tests do not leak listeners.
  • Use ts.Client() with TLS servers; a default client rejects the self-signed certificate.

Common Pitfalls

  • Recorder tests skip real middleware wrappers like timeouts and recovery added by the server framework.
  • Reading the request body twice fails; buffer or re-create it when multiple layers consume it.
  • Panics in handlers under a recorder are not converted to 500s, unlike production servers.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://pkg.go.dev/net/http/httptest · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:19.835Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://pkg.go.dev/net/http/httptest · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:19.835Z.

  • Learn more about best practices Repository cs.opensource.google/go/go Links Report a Vulnerability Documentation ¶ Overview ¶ Package httptest provides utilities for HTTP testing. (https://pkg.go.dev/net/http/httptest)

FAQ

Recorder or NewServer, which one should I use?
Recorder exercises a handler function in memory, fast and isolated. NewServer runs a real listener so you can test full clients, middleware chains, and HTTP/1.1 wire behavior. Start with the recorder and graduate to a server when the client is the subject.
How do I test request body handling?
Pass a strings.NewReader or bytes.NewReader payload to NewRequest, set Content-Type, and read r.Body in the handler. For JSON, decode from r.Body directly; the recorder captures whatever the handler writes back.
Why is httptest an install-free package?
It ships with the Go toolchain as part of net/http. The command shown opens the documentation locally; there is nothing to download, and the import is always available at net/http/httptest.