Documentation
¶
Index ¶
- func NewRequest(method, target string, body io.Reader) *http.Request
- func SetPathValue(req *http.Request, key, value string)
- func TemplateFromString(templates map[string]string) *template.Template
- func Test(r *rex.Router, req *http.Request) (*http.Response, error)
- func TestHandler(h rex.HandlerFunc, req *http.Request, options ...rex.RouterOption) (*http.Response, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewRequest ¶
NewRequest creates a new HTTP request. It's a wrapper around httptest.NewRequest but handy for consistency.
func SetPathValue ¶
SetPathValue sets a path value on the request. This is useful for testing handlers that rely on path parameters (c.Param). Note: This requires Go 1.22+.
func TemplateFromString ¶
TemplateFromString creates a *template.Template from a string map. Keys are template names (e.g. "index.html") and values are the content. Useful for quick tests without creating files.
func Test ¶
Test executes the request against the router and returns the response. It mimics the behavior of the real server by using the router's ServeHTTP method.
func TestHandler ¶
func TestHandler(h rex.HandlerFunc, req *http.Request, options ...rex.RouterOption) (*http.Response, error)
TestHandler executes a specific handler in isolation with a temporary router context. It allows testing handlers that require router features like templates without registering a route.
If the handler returns an error, it is returned directly. The response in the recorder contains whatever was written to the ResponseWriter up to that point.
Example:
func TestMyHandler(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
resp, err := test.TestHandler(MyHandler, req, rex.WithTemplates(tmpl))
// ...
}
Types ¶
This section is empty.