https://github.com/akshayjshah/memhttp
In-memory HTTP for Go applications
https://github.com/akshayjshah/memhttp
connect-go golang http testing
Last synced: 7 months ago
JSON representation
In-memory HTTP for Go applications
- Host: GitHub
- URL: https://github.com/akshayjshah/memhttp
- Owner: akshayjshah
- License: mit
- Created: 2023-06-15T06:15:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-15T06:29:07.000Z (almost 3 years ago)
- Last Synced: 2025-04-10T19:36:41.107Z (12 months ago)
- Topics: connect-go, golang, http, testing
- Language: Go
- Homepage: https://go.akshayshah.org/memhttp
- Size: 11.7 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
memhttp
=======
[](https://github.com/akshayjshah/memhttp/actions/workflows/ci.yaml)
[](https://goreportcard.com/report/go.akshayshah.org/memhttp)
[](https://pkg.go.dev/go.akshayshah.org/memhttp)
`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`][connect-go] RPC
servers.
## Installation
```
go get go.akshayshah.org/memhttp
```
## Usage
In-memory HTTP is most common in tests, so most users will be best served by
the `memhttptest` subpackage:
```go
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)
}
}
```
## Status: Unstable
This module is unstable, with a stable release expected before the end of 2023.
It supports the [two most recent major releases][go-support-policy] of Go.
Within those parameters, `memhttp` follows semantic versioning.
## Legal
Offered under the [MIT license][license].
[go-support-policy]: https://golang.org/doc/devel/release#policy
[license]: https://github.com/akshayjshah/memhttp/blob/main/LICENSE
[connect-go]: https://github.com/bufbuild/connect-go