servertest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0, MIT Imports: 21 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package servertest has the parts of a server for tests and examples: a clock you set, an authenticator that trusts a header, and a notifier, flows, agents, books, files and an audit log that remember what they were given; and, for the tests of a project's pages, a browser with no script (Browser) and the form of a page as a browser holds it (Form). None of them is for production.

Index

Constants

This section is empty.

Variables

View Source
var ErrFailed = errors.New("failed, as the test said")

ErrFailed is the failure of the parts that are told to fail.

Functions

This section is empty.

Types

type Agents

type Agents struct {
	Answer func(server.Ask)
	// contains filtered or unexported fields
}

Agents remembers what was asked. Answer, when set, is called for each new ask after it is remembered (in its own goroutine), to answer it as an agent would.

func (*Agents) Ask

func (a *Agents) Ask(_ context.Context, x server.Ask) error

Ask remembers an ask; a key given again is remembered once.

func (*Agents) Asks

func (a *Agents) Asks() []server.Ask

Asks answers what was asked.

type AuditLog

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

AuditLog remembers its entries.

func (*AuditLog) Entries

func (a *AuditLog) Entries() []server.AuditEntry

Entries answers the entries.

func (*AuditLog) Record

func (a *AuditLog) Record(_ context.Context, e server.AuditEntry) error

Record remembers an entry.

type Books

type Books struct {
	Refuse func(server.BookCall) string
	Fail   func(server.BookCall) error
	// contains filtered or unexported fields
}

Books are books in memory that answer done to every call, and done_before to a call they made before: the same call (hold, post or release) of the same effect with the same arguments, as a book that keys its holds by the ticket and the try answers a call given again. Refuse, when set, refuses a call with its reason; Fail, when set, answers a call with an error, as books that do not answer do, and the call is not made. With a memstore, a call counts only once its move is saved.

func (*Books) Call

func (b *Books) Call(_ context.Context, tx any, c server.BookCall) (server.BookAnswer, error)

Call makes a call.

func (*Books) Calls

func (b *Books) Calls() []server.BookCall

Calls answers the calls made.

type Browser

type Browser struct {
	Handler http.Handler
	// Base is the scheme and the host requests go to; http://localhost:8080 when "" (a loopback
	// host, which the development sign-in answers).
	Base string
	// Header is sent with each request.
	Header http.Header
	// contains filtered or unexported fields
}

Browser is a browser with no script on the pages of a handler, for tests: it sends what a browser sends from the server's own pages (Sec-Fetch-Site same-origin, Sec-Fetch-Mode navigate, Accept text/html) to Base, with Header and the cookies the answers set (a cookie set with a negative MaxAge is forgotten), and follows no redirect.

func (*Browser) Get

func (b *Browser) Get(target string) *httptest.ResponseRecorder

Get sends GET target (a path of the server, with its query).

func (*Browser) Post

func (b *Browser) Post(target string, form url.Values) *httptest.ResponseRecorder

Post posts a form, url-encoded, to target.

func (*Browser) Press

func (b *Browser) Press(f *Form, button string) (*httptest.ResponseRecorder, error)

Press posts f with its button, as the page posts it (url-encoded, or multipart when the form is).

type Clock

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

Clock is a clock that tells the time it is set to.

func NewClock

func NewClock(t time.Time) *Clock

NewClock makes a clock set to t.

func (*Clock) Add

func (c *Clock) Add(d time.Duration)

Add moves the time on.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now tells the time.

func (*Clock) Set

func (c *Clock) Set(t time.Time)

Set sets the time.

type Files

type Files = memstore.Files

Files keeps files in memory: it is memstore.Files, which a server that keeps its tickets in memory keeps its attachments in. With a Base, URL answers Base/<id>; without one, "".

type Flows

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

Flows remembers the flows launched.

func (*Flows) Launch

func (f *Flows) Launch(_ context.Context, l server.Launch) error

Launch remembers a launch; a key given again is remembered once.

func (*Flows) Launches

func (f *Flows) Launches() []server.Launch

Launches answers the flows launched.

type Form

type Form struct {
	// Action is where it posts.
	Action string
	// Values are the values it posts, but the files and the button.
	Values url.Values
	// contains filtered or unexported fields
}

Form is the form of a page as a browser holds it: what it posts, as the person leaves it.

func ReadForm

func ReadForm(page []byte) (*Form, error)

ReadForm reads the form of a page (the one of class form; the bar's is another) as a browser does when the page is shown: its controls in their order, but those in a <template>. A page with no such form is an error. The page is read as the framework writes its pages, well-formed XML with the named characters of HTML; a page that is not is an error too.

func (*Form) Attach

func (f *Form) Attach(name, file string, data []byte) error

Attach chooses a file for a file control: its name and what it holds. The post gives it the type a browser gives a file of that name (application/pdf for a name that ends in .pdf, …; application/octet-stream for an ending it does not know).

func (*Form) Body

func (f *Form) Body(button string) ([]byte, string, error)

Body is what a browser posts with a button: the body and its Content-Type (the button is sent as the page's buttons send it, _do=<value>). A button the form does not have is an error.

func (*Form) Buttons

func (f *Form) Buttons() []string

Buttons are the values of the form's buttons, in their order.

func (*Form) Set

func (f *Form) Set(name, value string) error

Set types value in a control a person types in, or chooses one of a select's options. A hidden control, a file, a box, a name the form does not have and a value no option has are errors.

func (*Form) Tick

func (f *Form) Tick(name, value string, on bool) error

Tick ticks a box (on) or clears it.

type Header struct{}

Header is an authenticator that trusts a header to say who sends the request: X-Person (the person's ID) and X-Roles (their roles, apart with commas; "staff" when it is not there). For tests and examples only: anyone who can send a request can say they are anyone.

func (Header) Authenticate

func (Header) Authenticate(r *http.Request) (server.Person, error)

Authenticate reads the headers.

type Notifier

type Notifier struct {
	Fail func(server.Notice) error
	// contains filtered or unexported fields
}

Notifier remembers the notices it is given. Fail, when set, makes Notify fail for a notice.

func (*Notifier) Calls

func (n *Notifier) Calls() int

Calls answers how many times Notify was called, a key given again included.

func (*Notifier) Notices

func (n *Notifier) Notices() []server.Notice

Notices answers the notices given, the first first.

func (*Notifier) Notify

func (n *Notifier) Notify(_ context.Context, x server.Notice) error

Notify remembers a notice; a key given again is remembered once.

Jump to

Keyboard shortcuts

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