Skip to content

proposal: net/http/httptest: synctest support #76608

@rogpeppe

Description

@rogpeppe

Proposal Details

I use the httptest package a lot, and in particular NewServer rather than ResponseRecorder, because
it picks up subtle real-world issues that don't appear when not using the entire connection machinery,
and can be used for long-lived connections such as websockets too.

It would be really nice to be able to use httptest.NewServer in a manner compatible with testing/synctest;
that is, with a fake network connection rather than using the OS's network stack.

It would also be nice to have some fake network connection code in the standard library
in addition to net.Pipe, but that requires more API surface area and raises tricky
questions about global variables.

For the record, here's how I'm doing it currently; it took me a little time to work it out.

https://go.dev/play/p/AVXzqqwiJPn

Concrete proposal:

// NewSynctestServer returns and starts a new [Server] suitable
// for using with [testing/synctest]. It does not use the underlying
// system's network stack, and thus it's not possible to use
// [net.Dial] etc to access the server. Use methods on the [net/http.Client]
// value returned by [Server.Client]
// instead of invoking top-level HTTP functions such as [net/http.Get].
func NewSynctestServer(handler http.Handler) *Server

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions