viewdata

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

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

func NewBody

func NewBody(kind string, b []byte, parsed any) *Body

func (*Body) Base64

func (b *Body) Base64() string

func (*Body) Bytes

func (b *Body) Bytes() []byte

func (*Body) Content

func (b *Body) Content() any

func (*Body) String

func (b *Body) String() string

type Data

type Data struct {
	// Request is the request being served.
	Request *Request

	// Provided holds the data of the server's template data providers, by key: {{ .Provided.user }}.
	Provided map[string]any

	// KV reads the server's key-value store: {{ .KV.Get "key" }} and {{ .KV.Has "key" }}.
	KV *KVView

	// Server describes the mock server.
	Server *Server

	// Data is the data of the server, set with its SetData: {{ .Data.target }}.
	Data map[string]any

	// Extra is the template data of the reply being rendered: a mock file's template.data. Other templates, such as
	// a webhook's, don't have it.
	Extra any

	// Scenarios reads the state of the server's scenarios: {{ .Scenarios.State "checkout" }}, and of the instance of
	// a key, {{ .Scenarios.StateOf "checkout" (.Request.Header.Get "X-Test-Id") }}. A scenario the mock that matched
	// moved is in the state it moved it to, even once another request moved it on.
	Scenarios matchscenario.View

	// Explanation holds what the matchers of the mock that matched recorded, such as why the matcher under a not
	// didn't match: {{ range .Explanation.Messages }}.
	Explanation *vlatte.Explanation
}

Data is what templates render with: the dot of a Go template, and the context of any other engine.

func (*Data) WithExtra

func (d *Data) WithExtra(extra any) *Data

WithExtra returns a copy of d with the given template data.

type KVView

type KVView = kvview.View

KVView gives templates the server's key-value store as text: {{ .KV.Get "key" }}.

func NewKVView

func NewKVView(ctx context.Context, kv vlatte.KV) *KVView

NewKVView returns the KVView of kv, read with ctx.

type M

type M interface {
	ID() string
	Name() string
}

type Request

type Request struct {
	// ID identifies the request, as the ID of its mockhttp.RequestValues.
	ID     string
	Method string

	// URL is the request URL: the one a request in absolute form, as a forward proxy gets, or an intercepted one names,
	// and otherwise on the server's base URL.
	URL *url.URL

	// URLPathSegments are the segments of the URL path, each decoded: /users/1 is [users 1], and /files/a%2Fb is
	// [files a/b].
	URLPathSegments []string

	// PathParams holds the parameters of the path template of the mock that matched, decoded, by name: /users/{id}
	// gives {{ .Request.PathParams.id }}. A mock without one has none.
	PathParams map[string]string

	Header http.Header

	// Cookies holds the cookies of the request, by name.
	Cookies map[string]*http.Cookie

	// Body is the body of the request, or nil when it has none: see HasBody.
	Body *Body

	// PostForm holds the text fields of a form body, urlencoded or multipart: {{ .Request.PostForm.Get "name" }}.
	PostForm url.Values

	// MultipartForm is the multipart form of the body, as http.Request.ParseMultipartForm left it, or nil.
	MultipartForm *multipart.Form
}

Request is the request being served.

func (*Request) Cookie

func (r *Request) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie, like http.Request.Cookie.

func (*Request) FormFile

func (r *Request) FormFile(key string) *multipart.FileHeader

FormFile returns the first file of the field key of a multipart form, as http.Request.FormFile picks it, or nil: {{ with .Request.FormFile "avatar" }}{{ .Filename }}{{ end }}.

func (*Request) HasBody

func (r *Request) HasBody() bool

type Server

type Server struct {
	// URL is the base URL of the server, such as http://127.0.0.1:8080: {{ .Server.URL }}/callback.
	URL string
}

Server describes the mock server to templates.

Jump to

Keyboard shortcuts

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