https://github.com/karlpokus/routest
Simpler http api tests in go
https://github.com/karlpokus/routest
api go http testing
Last synced: 3 months ago
JSON representation
Simpler http api tests in go
- Host: GitHub
- URL: https://github.com/karlpokus/routest
- Owner: karlpokus
- License: mit
- Created: 2019-10-13T20:30:17.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-05T13:11:43.000Z (almost 6 years ago)
- Last Synced: 2025-12-17T12:22:00.436Z (4 months ago)
- Topics: api, go, http, testing
- Language: Go
- Homepage: https://godoc.org/github.com/karlpokus/routest
- Size: 10.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# routest
Fast and easy way of testing your http api. Works with the stdlibs `testing` pkg.
[](https://pkg.go.dev/github.com/karlpokus/routest/v2)
# install
```bash
$ go get github.com/karlpokus/routest/v2
```
# usage
Test a route
```go
import (
// ...
"github.com/karlpokus/routest/v2"
)
func hi(s string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Etag", "abc")
fmt.Fprintf(w, "hi %s", s)
}
}
func TestRoute(t *testing.T) {
routest.Test(t, nil, []routest.Data{
{
Name: "hi from route",
Path: "/",
Handler: hi("bob"),
Status: 200,
ResponseBody: []byte("hi bob"),
ResponseHeader: http.Header{
"Etag": []string{"abc"},
},
},
})
}
```
Test registered routes
```go
import (
// ...
"github.com/karlpokus/routest/v2"
"github.com/julienschmidt/httprouter"
)
func Greet(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
fmt.Fprintf(w, "hello %s", params.ByName("user"))
}
func TestRouter(t *testing.T) {
routest.Test(t, func() http.Handler {
router := httprouter.New()
router.HandlerFunc("GET", "/greet/:user", Greet)
return router
}, []routest.Data{
{
Name: "Greet from router",
Method: "GET",
Path: "/greet/bob",
Status: 200,
ResponseBody: []byte("hello bob"),
},
})
}
```
# todos
- [x] allow for custom server/router to register as handler
- [x] fix v2 module path
- [x] Add http.Header to Data
- [x] Make some Data fields optional
- [ ] v3 no need for path for testing routes, no need for handler if testing a router
- [ ] v3 remove httprouter dependency
# license
MIT