Testing
Rue includes a built-in Agent for integration testing without starting a server.
Basic Usage
func TestAPI(t *testing.T) {
r := rue.New()
r.GET("/users/:id", getUser)
agent := rue.NewAgent(r)
// Simple GET
resp := agent.Get("/users/123")
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200, got %d", resp.StatusCode)
}
// Parse JSON response
var result map[string]any
resp.JSON(&result)
}
Request Methods
// GET request
resp := agent.Get("/path")
// POST with JSON
resp := agent.PostJSON("/users", map[string]string{"name": "John"})
// POST with form data
resp := agent.PostForm("/login", url.Values{"user": {"john"}, "pass": {"secret"}})
// With headers
agent.SetHeader("Authorization", "Bearer token")
resp := agent.Get("/protected")
Request Builder
resp := agent.NewRequest("POST", "/search").
Query("q", "test").
Query("page", "1").
Header("X-Custom", "value").
JSON(map[string]string{"filter": "active"}).
Send()
Response Methods
| Method | Description |
|---|---|
resp.StatusCode | HTTP status code |
resp.Body | Raw response body |
resp.JSON(&v) | Parse JSON into struct |
resp.Header.Get(key) | Get response header |