https://github.com/celrenheit/htest
A lightweight high-level abstraction for testing HTTP inspired by supertest built around httptest.ResponseRecorder
https://github.com/celrenheit/htest
Last synced: 9 months ago
JSON representation
A lightweight high-level abstraction for testing HTTP inspired by supertest built around httptest.ResponseRecorder
- Host: GitHub
- URL: https://github.com/celrenheit/htest
- Owner: celrenheit
- License: mit
- Created: 2016-04-18T14:22:51.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-06-21T16:08:07.000Z (about 9 years ago)
- Last Synced: 2025-08-13T17:43:04.096Z (11 months ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTest [](https://travis-ci.org/celrenheit/htest) [](https://godoc.org/github.com/celrenheit/htest) [](LICENSE)
A lightweight high-level abstractions for testing HTTP inspired by [supertest](https://github.com/visionmedia/supertest) built around [http.ResponseRecorder](https://golang.org/pkg/net/http/httptest/#ResponseRecorder)

# Install/Update
```
$ go get -u github.com/celrenheit/htest
```
# Usage
Let's say that when we hit `/admin` we get a status code of `401 Unauthorized`, a response body of `You are not authorized` and a header of `foo=bar`
```go
mux := http.NewServeMux()
mux.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", "bar")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "You are not authorized")
})
```
We can write a Test to test if our program behaves correctly
```go
func TestUnauthorized(t *testing.T) {
// We create a new instance for HTTPTester
// It requires a testing.T and an http.Handler as argument
h := htest.New(t, mux)
// We make assertions to the repsonse received
h.Get("/admin").Do().
ExpectHeader("foo", "bar").
ExpectStatus(http.StatusUnauthorized).
ExpectBody("You are not authorized")
}
```
h.Get returns a [Requester](https://godoc.org/github.com/celrenheit/htest#Requester) to be able to easily build your request.
We call the Do() to execute the request and get a [ResponseAsserter](https://godoc.org/github.com/celrenheit/htest#ResponseAsserter).
There are methods for each http methods in the [HTTPTester interface](https://godoc.org/github.com/celrenheit/htest#HTTPTester)
# Building the request
We send some arbitrary data and set a header to the request. The response should have the same body and the same header value.
```go
func TestBuildingRequest(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/path", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", r.Header.Get("foo"))
io.Copy(w, r.Body)
})
test := htest.New(t, mux)
test.Get("/path").
// Set a header to the request
AddHeader("foo", "barbar").
// Send a string to the request's body
SendString("my data").
// Executes the request
Do().
// The body sent should stay the same
ExpectBody("my data").
// The header sent should stay the same
ExpectHeader("foo", "barbar")
}
```
# Credits
* https://github.com/go-errors/errors
* https://github.com/stretchr/testify