golazy.dev
–
golazy.dev
/
lazytest
Index
|
Files
package lazytest ¶
import "golazy.dev/lazytest"
Types ¶
type App ¶
type App struct {
Handler http.Handler
Router *lazyroutes.Scope
// contains filtered or unexported fields
}
func FromHandler ¶
func FromHandler(t testing.TB, handler http.Handler, opts ...Option) *App
func New ¶
func New(t testing.TB, app *lazyapp.App, opts ...Option) *App
func (app *App) Check ¶
func (app *App) Check(cases ...Case)
func (app *App) Client ¶
func (app *App) Client() *Client
func (app *App) Do ¶
func (app *App) Do(method, target string, body io.Reader, opts ...RequestOption) *Response
func (app *App) Get ¶
func (app *App) Get(target string, opts ...RequestOption) *Response
func (app *App) GetPath ¶
func (app *App) GetPath(name string, values ...any) *Response
func (app *App) PathFor ¶
func (app *App) PathFor(name string, values ...any) string
func (app *App) Post ¶
func (app *App) Post(target string, body io.Reader, opts ...RequestOption) *Response
func (app *App) PostForm ¶
func (app *App) PostForm(target string, values url.Values, opts ...RequestOption) *Response
func (app *App) Routes ¶
func (app *App) Routes() lazyroutes.RouteTable
type Case ¶
type Case struct {
Name string
Method string
Path string
Headers http.Header
Status int
Contains []string
NotContains []string
ContentType string
Allow []string
}
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (client *Client) Cookies ¶
func (client *Client) Cookies() []*http.Cookie
func (client *Client) Do ¶
func (client *Client) Do(method, target string, body io.Reader, opts ...RequestOption) *Response
func (client *Client) Get ¶
func (client *Client) Get(target string, opts ...RequestOption) *Response
func (client *Client) Post ¶
func (client *Client) Post(target string, body io.Reader, opts ...RequestOption) *Response
func (client *Client) PostForm ¶
func (client *Client) PostForm(target string, values url.Values, opts ...RequestOption) *Response
func (client *Client) SetCookie ¶
func (client *Client) SetCookie(cookie *http.Cookie)
type Option ¶
type Option func(*App)
func WithRouter ¶
func WithRouter(router *lazyroutes.Scope) Option
type RequestOption ¶
type RequestOption func(*http.Request)
func Accept ¶
func Accept(value string) RequestOption
func BasicAuth ¶
func BasicAuth(username, password string) RequestOption
func Cookie ¶
func Cookie(cookie *http.Cookie) RequestOption
func Cookies ¶
func Cookies(cookies ...*http.Cookie) RequestOption
func Header ¶
func Header(name, value string) RequestOption
type Response ¶
type Response struct {
Recorder *httptest.ResponseRecorder
Result *http.Response
Request *http.Request
// contains filtered or unexported fields
}
func (response *Response) BodyBytes ¶
func (response *Response) BodyBytes() []byte
func (response *Response) BodyString ¶
func (response *Response) BodyString() string
func (response *Response) Contains ¶
func (response *Response) Contains(value string) *Response
func (response *Response) ContentType ¶
func (response *Response) ContentType(value string) *Response
func (response *Response) Cookies ¶
func (response *Response) Cookies() []*http.Cookie
func (response *Response) Header ¶
func (response *Response) Header(name string) string
func (response *Response) HeaderContains ¶
func (response *Response) HeaderContains(name, value string) *Response
func (response *Response) HeaderEquals ¶
func (response *Response) HeaderEquals(name, value string) *Response
func (response *Response) JSON ¶
func (response *Response) JSON(target any) *Response
func (response *Response) Match ¶
func (response *Response) Match(pattern string) []string
func (response *Response) MethodNotAllowed ¶
func (response *Response) MethodNotAllowed() *Response
func (response *Response) NotContains ¶
func (response *Response) NotContains(value string) *Response
func (response *Response) NotFound ¶
func (response *Response) NotFound() *Response
func (response *Response) OK ¶
func (response *Response) OK() *Response
func (response *Response) Status ¶
func (response *Response) Status(code int) *Response
Package lazytest provides HTTP-level test helpers for GoLazy applications and plain net/http handlers.
The package exercises the same handler that serves the application at runtime, but it does so through httptest instead of opening a port. That makes it a good boundary for behavior that depends on lazyapp composition: routing, public files, lazycontroller construction, rendering through lazyview, session cookies, response headers, redirects, method errors, and asset URLs.
Use New with a *lazyapp.App when the test should see the application's lazyroutes.Scope. New stores app.Router on the test wrapper, so PathFor, GetPath, and Routes use the same named routes lazycontroller.PathFor and the view route helpers use in a running app. Use FromHandler for a plain http.Handler, or pass WithRouter when that handler still has a route scope that should be available to assertions.
App issues one request at a time and returns a Response with fluent assertions for status codes, body text, headers, content type, JSON payloads, cookies, and regular-expression matches. Check runs a compact table of HTTP-level cases and creates subtests when the supplied testing.TB supports Run. Client keeps cookies between requests, which is the normal choice for login, flash, lazysession, and other multi-request flows.
Most controller behavior should be tested at this level instead of calling action methods directly. Direct controller tests are still useful for narrow constructor or dependency checks, but lazytest verifies the wiring between lazyapp, lazyroutes, lazycontroller, lazydispatch, lazyview, and public-file handling together.
Typical application tests stay at this level instead of calling controller actions directly:
func TestHomePage(t *testing.T) { app := lazytest.New(t, appinit.App()) app.Get("/").OK(). ContentType("text/html"). Contains("Welcome") }Standalone handler tests use the same assertions without requiring a GoLazy app:
func TestJSONHandler(t *testing.T) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"ok":true}`)) }) app := lazytest.FromHandler(t, handler) var payload struct { OK bool `json:"ok"` } app.Get("/", lazytest.Accept("application/json")). OK(). ContentType("application/json"). JSON(&payload) if !payload.OK { t.Fatal("expected ok payload") } }