net/http/httptest
import "net/http/httptest"
Package httptest provides utilities for HTTP testing.
Go 1.26.1
Constants
const DefaultRemoteAddr = "1.2.3.4"
DefaultRemoteAddr is the default remote address to return in RemoteAddr if an explicit DefaultRemoteAddr isn't set on [ResponseRecorder].
net/http/httptest/recorder.go:61
Functions
func NewRecorder() *ResponseRecorder
NewRecorder returns an initialized [ResponseRecorder].
net/http/httptest/recorder.go:51
func NewRequest(method string, target string, body io.Reader) *http.Request
NewRequest wraps NewRequestWithContext using context.Background.
net/http/httptest/httptest.go:19
func NewRequestWithContext(ctx context.Context, method string, target string, body io.Reader) *http.Request
NewRequestWithContext returns a new incoming server Request, suitable for passing to an [http.Handler] for testing. The target is the RFC 7230 "request-target": it may be either a path or an absolute URL. If target is an absolute URL, the host name from the URL is used. Otherwise, "example.com" is used. The TLS field is set to a non-nil dummy value if target has scheme "https". The Request.Proto is always HTTP/1.1. An empty method means "GET". The provided body may be nil. If the body is...
net/http/httptest/httptest.go:46
func NewServer(handler http.Handler) *Server
NewServer starts and returns a new [Server]. The caller should call Close when finished, to shut it down.
net/http/httptest/server.go:106
func NewTLSServer(handler http.Handler) *Server
NewTLSServer starts and returns a new [Server] using TLS. The caller should call Close when finished, to shut it down.
net/http/httptest/server.go:218
func NewUnstartedServer(handler http.Handler) *Server
NewUnstartedServer returns a new [Server] but doesn't start it. After changing its configuration, the caller should call Start or StartTLS. The caller should call Close when finished, to shut it down.
net/http/httptest/server.go:118
Types
type ResponseRecorder struct {
// Code is the HTTP response code set by WriteHeader.
//
// Note that if a Handler never calls WriteHeader or Write,
// this might end up being 0, rather than the...ResponseRecorder is an implementation of [http.ResponseWriter] that records its mutations for later inspection in tests.
net/http/httptest/recorder.go:21
func (rw *ResponseRecorder) Flush()
Flush implements [http.Flusher]. To test whether Flush was called, see rw.Flushed.
net/http/httptest/recorder.go:181
func (rw *ResponseRecorder) Header() http.Header
Header implements [http.ResponseWriter]. It returns the response headers to mutate within a handler. To test the headers that were written after a handler completes, use the [ResponseRecorder.Result] method and see the returned Response value's Header.
net/http/httptest/recorder.go:67
func (rw *ResponseRecorder) Result() *http.Response
Result returns the response generated by the handler. The returned Response will have at least its StatusCode, Header, Body, and optionally Trailer populated. More fields may be populated in the future, so callers should not DeepEqual the result in tests. The Response.Header is a snapshot of the headers at the time of the first write call, or at the time of this call, if the handler never did a write. The Response.Body is guaranteed to be non-nil and Body.Read call is guaranteed to not ret...
net/http/httptest/recorder.go:203
func (rw *ResponseRecorder) Write(buf []byte) (int, error)
Write implements http.ResponseWriter. The data in buf is written to rw.Body, if not nil.
net/http/httptest/recorder.go:107
func (rw *ResponseRecorder) WriteHeader(code int)
WriteHeader implements [http.ResponseWriter].
net/http/httptest/recorder.go:165
func (rw *ResponseRecorder) WriteString(str string) (int, error)
WriteString implements [io.StringWriter]. The data in str is written to rw.Body, if not nil.
net/http/httptest/recorder.go:121
type Server struct {
URL string // base URL of form http://ipaddr:port with no trailing slash
Listener net.Listener
// EnableHTTP2 controls whether HTTP/2 is enabled
// on the server. It ...A Server is an HTTP server listening on a system-chosen port on the local loopback interface, for use in end-to-end HTTP tests.
net/http/httptest/server.go:27
func (s *Server) Certificate() *x509.Certificate
Certificate returns the certificate used by the server, or nil if the server doesn't use TLS.
net/http/httptest/server.go:323
func (s *Server) Client() *http.Client
Client returns an HTTP client configured for making requests to the server. It is configured to trust the server's TLS test certificate and will close its idle connections on [Server.Close]. Use Server.URL as the base URL to send requests to the server. The returned client will also redirect any requests to "example.com" or its subdomains to the server.
net/http/httptest/server.go:333
func (s *Server) Close()
Close shuts down the server and blocks until all outstanding requests on this server have completed.
net/http/httptest/server.go:230
func (s *Server) CloseClientConnections()
CloseClientConnections closes any open HTTP connections to the test Server.
net/http/httptest/server.go:294
func (s *Server) Start()
Start starts a server from NewUnstartedServer.
net/http/httptest/server.go:126
func (s *Server) StartTLS()
StartTLS starts TLS on a server from NewUnstartedServer.
net/http/httptest/server.go:159