httpexpect is a concise and chainable HTTP/REST API testing library for Go. It provides expressive assertions for testing HTTP handlers and APIs with minimal boilerplate.
go get github.com/gavv/httpexpect/v2- Chainable API: Fluent interface for readable tests
- Rich Assertions: JSON, headers, status, cookies, etc.
- Built-in Matchers: Equal, Contains, Match (regex), etc.
- WebSocket Support: Test WebSocket connections
- FormData & Multipart: File uploads and forms
- Integration: Works with net/http handlers or live servers
e := httpexpect.New(t, "http://localhost:8080")
e.GET("/users/1").
Expect().
Status(200).
JSON().Object().
ValueEqual("name", "John")handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(`{"message": "Hello"}`))
})
e := httpexpect.WithConfig(httpexpect.Config{
Client: &http.Client{},
BaseURL: "http://example.com",
Reporter: httpexpect.NewAssertReporter(t),
Printers: []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
},
})
e.GET("/").WithHandler(handler).
Expect().
Status(200).
JSON().Object().
ValueEqual("message", "Hello")go test
go test -v # See HTTP requests/responses- ✅ Extremely readable test code
- ✅ Comprehensive HTTP assertions
- ✅ Works with handlers or live servers
- ✅ Excellent for API testing
- ✅ JSON path navigation
- ✅ Schema validation support
- ❌ HTTP-only (not for general testing)
- ❌ Learning curve for chainable API
- ❌ Can be verbose for simple tests
- ❌ Focused on REST/JSON APIs
- Chain assertions: Make tests readable
- Test handlers directly: Faster than live servers
- Use WithHandler: For unit testing handlers
- Validate JSON schema: Ensure API contracts
- Check all response aspects: Status, headers, body