webtest

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package webtest provides lightweight test helpers for web handlers and middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context is a lightweight web.Context implementation for handler tests built on httptest.

func NewContext

func NewContext(request *http.Request, recorder *httptest.ResponseRecorder, path string, pathParams PathParams) *Context

NewContext creates a new test context around the provided request/recorder pair. @group Testing Example: req := httptest.NewRequest(http.MethodGet, "/users/42?expand=roles", nil) ctx := webtest.NewContext(req, nil, "/users/:id", webtest.PathParams{"id": "42"}) fmt.Println(ctx.Param("id"), ctx.Query("expand"))

// 42 roles

func (*Context) AddHeader

func (c *Context) AddHeader(name string, value string)

func (*Context) AppSourceName

func (c *Context) AppSourceName() string

func (*Context) Bind

func (c *Context) Bind(target any) error

func (*Context) Blob

func (c *Context) Blob(code int, contentType string, body []byte) error

func (*Context) Context

func (c *Context) Context() context.Context

func (*Context) Cookie

func (c *Context) Cookie(name string) (*http.Cookie, error)

func (*Context) File

func (c *Context) File(path string) error

func (*Context) Get

func (c *Context) Get(key string) any

func (*Context) HTML

func (c *Context) HTML(code int, body string) error

func (*Context) Header

func (c *Context) Header(name string) string

func (*Context) Host

func (c *Context) Host() string

func (*Context) JSON

func (c *Context) JSON(code int, payload any) error

func (*Context) Method

func (c *Context) Method() string

func (*Context) Native

func (c *Context) Native() any

func (*Context) NoContent

func (c *Context) NoContent(code int) error

func (*Context) Param

func (c *Context) Param(name string) string

func (*Context) Path

func (c *Context) Path() string

func (*Context) Query

func (c *Context) Query(name string) string

func (*Context) RawRequest

func (c *Context) RawRequest() *http.Request

func (*Context) RealIP

func (c *Context) RealIP() string

func (*Context) Redirect

func (c *Context) Redirect(code int, url string) error

func (*Context) Request

func (c *Context) Request() *http.Request

func (*Context) Response

func (c *Context) Response() web.Response

func (*Context) ResponseWriter

func (c *Context) ResponseWriter() http.ResponseWriter

func (*Context) Scheme

func (c *Context) Scheme() string

func (*Context) Set

func (c *Context) Set(key string, value any)

func (*Context) SetAppSourceName

func (c *Context) SetAppSourceName(source string)

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

func (*Context) SetHeader

func (c *Context) SetHeader(name string, value string)

func (*Context) SetRequest

func (c *Context) SetRequest(request *http.Request)

func (*Context) SetResponseWriter

func (c *Context) SetResponseWriter(writer http.ResponseWriter)

func (*Context) StatusCode

func (c *Context) StatusCode() int

func (*Context) Text

func (c *Context) Text(code int, body string) error

func (*Context) URI

func (c *Context) URI() string

type PathParams

type PathParams map[string]string

PathParams captures named path parameter values for test contexts.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response exposes the recorded response state for a test Context.

func (*Response) Committed

func (r *Response) Committed() bool

func (*Response) Header

func (r *Response) Header() http.Header

func (*Response) Native

func (r *Response) Native() any

func (*Response) SetWriter

func (r *Response) SetWriter(writer http.ResponseWriter)

func (*Response) Size

func (r *Response) Size() int64

func (*Response) StatusCode

func (r *Response) StatusCode() int

func (*Response) Writer

func (r *Response) Writer() http.ResponseWriter

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL