tester

package
v0.0.0-...-ba7cdbd Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2014 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package tester implements functions for testing and benchmarking Gondola applications.

To set up a test, create a _test.go file, like in Go standard tests, and create a standard Go test function. Then, use New to create a Tester and use it to generate requests and check what they return.

See Tester or this package's tests for a few examples of complete tests. For benchmark, use Request.Bench. See its documentation for details.

Additionaly, tests might be run against a remote server by using the -H command line flag. e.g.

go test -v -H example.com // or https://example.com

Will run your tests against the server at example.com, rather than using the compiled code. This is pretty useful to make sure your tests pass on production after deploying your application.

App Engine apps with a correctly set up app.yaml might also use the -R flag to automatically test against http://<your-app-id>.appspot.com.

goapp test -v -R

The -L flag is also supported on GAE apps, for testing against http://localhost:8080. Note that you must manually start your app with goapp serve.

goapp test -v -L

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Contains

type Contains string

Contains is an alias for string, to indicate Request.Expect to check that a response contains the given value (rather than being equal). Users should use the shorthand methods Response.Contains and Response.ContainsHeader.

type Match

type Match string

Match is an alias for string, to indicate Request.Expect to compile the given value as a regular expression and then match it against the response body. Users should use the shorthand methods Response.Match and Response.MatchHeader.

type Reporter

type Reporter interface {
	Log(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
}

Reporter is the interface used to log and report errors when testing Gondola applications. Both testing.T and testing.B implement this interface.

type Request

type Request struct {
	Reporter Reporter
	App      *app.App
	Header   http.Header
	Method   string
	Path     string
	Body     []byte
	// contains filtered or unexported fields
}

A Request represents a request to be sent to the app. Users won't usually need to construct requests by hand, most of the time the helper type Test and its conveniency methods should be used. Use Request.Expect and its related functions to perform tests on the App response.

func (*Request) AddHeader

func (r *Request) AddHeader(key string, value interface{}) *Request

AddHeader is a conveniency function which adds a new HTTP header and returns the same *Request, to allow chaining. The value is converted to an string using types.ToString.

func (*Request) Bench

func (r *Request) Bench(b *testing.B)

Bench performs a benchmark with this request. Note that Bench does not perform any tests on the response, so you should not use Bench for testing, only strictly for benchmarking. Tipically, Bench is used from a testing benchmark.

 BenchmarkSomething(b *testing.B) {
	te := tester.New(b, MyApp)
	te.Get("/something", nil).Bench(b)
 }

Note that bench does not support benchmarking a remote server, so it ignores the -H flag.

func (*Request) Contains

func (r *Request) Contains(what string) *Request

Contains checks that the response body contains the given string. It's a shorthand for r.Expect(tester.Contains(what)).

func (*Request) ContainsHeader

func (r *Request) ContainsHeader(name string, what string) *Request

ContainsHeader works like Contains, but checks the requested header rather than the response body.

func (*Request) Err

func (r *Request) Err() error

Err returns the first error generated from this Request.

func (*Request) Expect

func (r *Request) Expect(what interface{}) *Request

Expect checks the response body or the status against the given criteria. Expect accepts the following argument types.

int: Check that the response code matches the given value.
nil: Check that the response is empty.
string: Check that the response body is equal to the given value.
Contains: Check that the response body contains the given value.
Match: Compile the given value as a regular expression and match it against the body.

Request send the request to the App lazily, on the first call to Expect. Multiple Expect (and related functions, like Contains or ExpectHeader) can be chained, but processing will stop as soon as an error is generated (either when getting the response from the App or from a failed Expect check). To get the first error, use the Err() method. Note that when using a *testing.T or a *testing.B as the Reporter, Expect will call t.Error or t.Fatal for you. See also the shorthand functions Request.Contains and Request.Match. Additionally, check this package's tests, in tester_test.go, for examples.

func (*Request) ExpectHeader

func (r *Request) ExpectHeader(name string, what interface{}) *Request

ExpectHeader works like Expect, but checks the requested header rather than the response body. See Response.Expect to find the accepted types.

func (*Request) Match

func (r *Request) Match(what string) *Request

Match checks that the response body matches the given regular expression. It's a shorthand for r.Expect(tester.Match(what)). If the regular expression can't be compiled, the current test will be aborted.

func (*Request) MatchHeader

func (r *Request) MatchHeader(name string, what string) *Request

MatchHeader works like Match, but checks the requested header rather than the response body.

type Tester

type Tester struct {
	Reporter Reporter
	App      *app.App
}

Tester encapsulates the app.App to be tested with the Reporter to send the logs and errors to. It's usually used in conjuction with the testing package.

 func TestMyApp(t *testing.T) {
	// App was initialized on init. Note that
	// the app does not need to be listening.
	te := tester.New(t, App)
	te.Get("/foo", nil).Expect(200)
 }

func New

func New(r Reporter, a *app.App) *Tester

New returns prepares the *app.App and then returns a new Tester. It also disables logging in the App, since the Tester does its own logging.

func (*Tester) Form

func (t *Tester) Form(path string, params map[string]interface{}) *Request

Form returns a POST request which sends a form with the given parameters, while also setting the right Content-Type.

func (*Tester) Get

func (t *Tester) Get(path string, params map[string]interface{}) *Request

Get returns a GET request with the given path and parameters, which are appended to the URL as a query string.

func (*Tester) Post

func (t *Tester) Post(path string, body interface{}) *Request

Post returns a POST request with the given body. See Tester.Request for the supported types in the body argument.

func (*Tester) Request

func (t *Tester) Request(method string, path string, body interface{}) *Request

Request returns a new request with the given method, path and body. Body might be one of the following:

string
[]byte
io.Reader

Jump to

Keyboard shortcuts

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