memhttp
provides a full net/http
server and client that communicate over
in-memory pipes rather than the network. This is often useful in tests, where
you want to avoid localhost networking but don't want to stub out all the
complexity of HTTP.
Occasionally, it's also useful in production code: if you're planning to split a
monolithic application into microservices, you can first use memhttp
to
simulate the split. This allows you to rewrite local function calls as HTTP
calls (complete with serialization, compression, and middleware) while
retaining the ability to quickly change service boundaries.
In particular, memhttp
pairs well with connect-go
RPC
servers.
go get go.akshayshah.org/memhttp
In-memory HTTP is most common in tests, so most users will be best served by
the memhttptest
subpackage:
func TestServer(t *testing.T) {
hello := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!")
})
// The server starts automatically, and it shuts down gracefully when the
// test ends. Startup and shutdown errors fail the test.
//
// By default, servers and clients use TLS and support HTTP/2.
srv := memhttptest.New(t, hello)
res, err := srv.Client().Get(srv.URL())
if err != nil {
t.Fatal(err)
}
if res.StatusCode != http.StatusOK {
t.Error(res.Status)
}
}
This module is unstable, with a stable release expected before the end of 2023. It supports the two most recent major releases of Go.
Within those parameters, memhttp
follows semantic versioning.
Offered under the MIT license.