https://github.com/cateiru/go-http-easy-test
A package that wraps `net/http/httptest` and allows you to easily test HTTP Handlers.
https://github.com/cateiru/go-http-easy-test
go golang httptest
Last synced: 8 months ago
JSON representation
A package that wraps `net/http/httptest` and allows you to easily test HTTP Handlers.
- Host: GitHub
- URL: https://github.com/cateiru/go-http-easy-test
- Owner: cateiru
- License: mit
- Created: 2022-07-12T09:08:14.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-23T14:14:18.000Z (over 3 years ago)
- Last Synced: 2025-07-01T13:05:32.371Z (about 1 year ago)
- Topics: go, golang, httptest
- Language: Go
- Homepage: https://pkg.go.dev/github.com/cateiru/go-http-easy-test/v2
- Size: 442 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go http easy test
[](https://pkg.go.dev/github.com/cateiru/go-http-easy-test) [](https://goreportcard.com/report/github.com/cateiru/go-http-easy-test) [](https://github.com/cateiru/go-http-easy-test/actions/workflows/go.yml) [](https://codecov.io/gh/cateiru/go-http-easy-test)
A package that wraps `net/http/httptest` and allows you to easily test HTTP Handlers.
✅ Easy
✅ Intuitive
✅ Support `application/json`
✅ Support `application/x-www-form-urlencoded`
✅ Support `multipart/form-data`
✅ Support [Echo package](https://echo.labstack.com/)
✅ Support cookie
## Install
```bash
go get -u github.com/cateiru/go-http-easy-test/v2
```
## Mock
The user can choose from the following two options.
- Actually start the server using `httptest.NewServer`
- Mock the Handler arguments (`w http.ResponseWriter, r *http.Request`)
### Actually start the server using `httptest.NewServer`
```go
package main_test
import (
"testing"
"github.com/cateiru/go-http-easy-test/easy"
)
func Handler(w http.ResponseWriter, r *http.Request) {
...do something
}
func TestHandler(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", Handler)
// create server
s := easy.NewMockServer(mux)
// Start the server with TLS using:
// s := server.TestNewMockTLSServer(mux)
defer s.Close()
// Option: You can set cookies.
cookie := &http.Cookie{
Name: "name",
Value: "value",
}
s.Cookie([]*http.Cookie{
cookie,
})
// GET
resp := s.Get(t, "/")
resp := s.GetOK(t, "/")
// POST
resp := s.Post(t, "/", "text/plain", body)
resp := s.PostForm(t, "/", url) // application/x-www-form-urlencoded
resp := s.PostJson(t, "/", obj) // application/json
resp := s.PostString(t, "/", "text/plain", body)
// Easily build multipart/form-data
form := easy.NewMultipart()
form.Insert("key", "value")
resp := s.PostFormData(t, "/", form)
resp := s.FormData(t, "/", "[method]", form)
// Other
resp := s.Do(t, "/", "[method]", body)
// The `resp` of all return values are easy to compare.
// Check status
resp.Ok(t)
resp.Status(t, 200)
// get body
body := resp.Body().String()
// Compare response body
resp.EqBody(t, body)
resp.EqJson(t, obj)
// prase response json
body := new(JsonType)
err := resp.Json(body)
// returns Set-Cookie headers
cookies := resp.SetCookies()
}
```
### Mock the Handler arguments (`w http.ResponseWriter, r *http.Request`)
```go
package main_test
import (
"testing"
"github.com/cateiru/go-http-easy-test/easy"
)
func Handler(w http.ResponseWriter, r *http.Request) {
...do something
}
func EchoHandler(c echo.Context) error {
...do something
}
func TestHandler(t *testing.T) {
// Default
m, err := easy.NewMock(body, http.MethodGet, "/")
m, err := easy.NewMockReader(reader, http.MethodGet, "/")
// GET
m, err := easy.NewGet(body, "/")
// POST or PUT send json
m, err := easy.NewJson("/", data, http.MethodPost)
// POST or PUT send x-www-form-urlencoded
m, err := easy.NewURLEncoded("/", url, http.MethodPost)
// POST or PUT send multipart/form-data
// Easily build multipart/form-data using the `contents` package.
m, err := easy.NewFormData("/", multipart, http.MethodPost)
// Option: set remote addr
m.SetAddr("203.0.113.0")
// Option: You can set cookies.
cookie := &http.Cookie{
Name: "name",
Value: "value",
}
m.Cookie([]*http.Cookie{
cookie,
})
// Set handler and run
m.Handler(Handler)
// Use echo package
echoCtx := m.Echo()
err := EchoHandler(echoCtx)
// check response
m.Ok(t)
m.Status(t, 200)
// Compare response body
m.EqBody(t, body)
m.EqJson(t, obj)
// prase response json
body := new(JsonType)
err := m.Json(body)
// returns Set-Cookie headers
cookies := m.SetCookies()
// Return http.Response
response := m.Response()
// set-cookie
cookie := m.FindCookie("name")
}
```
### multipart
Easily create `multipart/form-data` requests.
This method is used when submitting with `multipart/form-data`.
```go
package main
import (
"os"
"github.com/cateiru/go-http-easy-test/easy"
)
func main() {
m := easy.NewMultipart()
// Add a string format form.
err := m.Insert("key", "value")
// Add a file format form.
file, err := os.Open("path")
err := m.InsertFile("key", file)
// Outputs in the specified format.
body := m.Export()
contentType := m.ContentType()
// Use `handler` package
// Actually start the server using `httptest.NewServer`
s := server.NewMockServer(mux)
defer s.Close()
resp := s.PostFormData(t, "/", m)
// Mock the Handler arguments (`w http.ResponseWriter, r *http.Request`)
m, err := mock.NewFormData("/", m, http.MethodPost)
}
```
## License
[MIT](./LICENSE)