Documentation
¶
Overview ¶
Package testing provides tools to tests the HTTP routes, the message bus, email sending, etc.
Index ¶
- func RunRequestSequence(t *testing.T, c *Client, user string, tests ...RequestTest)
- func Store() *superbus.MemStore
- type Client
- func (c *Client) Cookies() []*http.Cookie
- func (c *Client) Get(target string) *Response
- func (c *Client) Login(username, password string)
- func (c *Client) Logout()
- func (c *Client) NewFormRequest(method, target string, data url.Values) *http.Request
- func (c *Client) NewJSONRequest(method, target string, data interface{}) *http.Request
- func (c *Client) NewRequest(method, target string, body io.Reader) *http.Request
- func (c *Client) PostForm(target string, data url.Values) *Response
- func (c *Client) RenderTemplate(src string, extra map[string]interface{}) (string, error)
- func (c *Client) Request(req *http.Request) *Response
- func (c *Client) RequestJSON(method string, target string, data interface{}) *Response
- type EventManager
- type RequestTest
- type Response
- type TestApp
- type TestUser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunRequestSequence ¶
func RunRequestSequence(t *testing.T, c *Client, user string, tests ...RequestTest)
RunRequestSequence performs a serie of requests using RequestTest instances.
Types ¶
type Client ¶
type Client struct { *testing.T URL *url.URL Jar http.CookieJar CsrfToken string // contains filtered or unexported fields }
Client is a thin HTTP client over the main server router.
func (*Client) NewFormRequest ¶
NewFormRequest returns a new http.Request instance to be used for sending form data.
func (*Client) NewJSONRequest ¶
NewJSONRequest returns a new http.Request instance to be used for sending and receiving JSON data.
func (*Client) NewRequest ¶
NewRequest returns a new http.Request instance ready for tests.
func (*Client) PostForm ¶
PostForm performs a POST request on the given path with some data. If available, the CSRF token is automaticaly sent with the data.
func (*Client) RenderTemplate ¶
RenderTemplate executes a template string using some properties of the client. (Users, URL). Extra data can be sent using the extra map.
type EventManager ¶
type EventManager struct {
// contains filtered or unexported fields
}
EventManager is a in memory event manager that stores every event in a list.
func NewEventManager ¶
func NewEventManager() *EventManager
NewEventManager returns a new EventManager instance.
func (*EventManager) On ¶
func (m *EventManager) On(_ string, _ superbus.EventHandler)
On registers an event handler. In this case, it does nothing.
func (*EventManager) Push ¶
func (m *EventManager) Push(name string, value []byte) error
Push adds an event to the queue.
func (*EventManager) Records ¶
func (m *EventManager) Records(name string) [][]byte
Records returns the recorded events for a given event name.
func (*EventManager) Stop ¶
func (m *EventManager) Stop()
Stop stops the Event manager. In this case it empties the queue of recorded events.
type RequestTest ¶
type RequestTest struct { Method string Target string Form url.Values JSON interface{} ExpectStatus int ExpectRedirect string ExpectJSON string ExpectJQ []any ExpectContains string Assert func(*testing.T, *Response) }
RequestTest contains data that are used to perform requests and assert some results.
type Response ¶
type Response struct { *http.Response URL *url.URL Redirect string Body []byte HTML *html.Node JSON any CsrfToken string }
Response is a wrapper around http.Response where the body is stored and the HTML (when applicable) is parsed in advance.
func NewResponse ¶
NewResponse returns a Response instance based on the ResponseRecorder given in input.
func (*Response) AssertJQ ¶
AssertJQ performs the JQ query and check every expected result argument. The test fails when the returned results count differs from the expected arguments.
func (*Response) AssertJSON ¶
AssertJSON checks that the response's JSON matches what we expect.
func (*Response) AssertRedirect ¶
AssertRedirect checks that the expected target is present in a Location header.
func (*Response) AssertStatus ¶
AssertStatus checks the response's expected status.
type TestApp ¶
type TestApp struct { TmpDir string Srv *server.Server Users map[string]*TestUser Bookmarks map[string]*bookmarks.Bookmark LastEmail string }
TestApp holds information of the application for testing.
func NewTestApp ¶
NewTestApp initializes TestApp with a default configuration, some users, and an http muxer ready to accept requests.
type TestUser ¶
type TestUser struct { User *users.User Token *tokens.Token Bookmarks []*bookmarks.Bookmark // contains filtered or unexported fields }
TestUser contains the user data that we can use during tests.
func NewTestUser ¶
NewTestUser creates a new user for testing.