servertest

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2021 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package servertest provides methods and types to test server.Handler implementations.

Index

Constants

This section is empty.

Variables

View Source
var CheckAnyErrorStatus = CheckerFun(func(t *testing.T, response *http.Response, request *server.Request) {
	t.Helper()
	if response.StatusCode < 400 {
		t.Errorf("Wrong status code. Got %d. Expect at least 400.", response.StatusCode)
	}
})
View Source
var (
	Unimplemented = errors.New("Unimplemented")
)

Functions

func Run

func Run(t *testing.T, tests []Test, handler server.Handler)

Run executes all the given tests on the given Handler.

The same handler is used for all tests. The tests are executed in the given order. The requests received by the handler are as if the handler had been registered for the pattern "/a/test".

Each test is executed inside testing.T.Run, hence calling t.Fatal in the checker abort only the current test.

func RunFunc

func RunFunc(t *testing.T, tests []Test, handler server.HandleFunction)

RunFunc is a convenient wrapper around Run for HandleFunction.

Types

type CheckError added in v0.0.4

type CheckError struct {
	Code int
	Body string
}

CheckError checks the statusCode and error message of a response.

func (CheckError) Before added in v0.0.5

func (self CheckError) Before(t *testing.T)

func (CheckError) Check added in v0.0.4

func (self CheckError) Check(t *testing.T, response *http.Response, request *server.Request)

Check implements Checker.

type CheckJSON added in v0.0.4

type CheckJSON struct {
	Code    int // If zero, http.StatusOK is used instead.
	Body    interface{}
	Partial bool // If true, Body may lack some field of the response.
}

CheckJSON checks that the response body is similar to the JSON marshaling of a given value.

func (CheckJSON) Before added in v0.0.5

func (self CheckJSON) Before(t *testing.T)

func (CheckJSON) Check added in v0.0.4

func (self CheckJSON) Check(t *testing.T, response *http.Response, request *server.Request)

Check implements Checker.

type CheckStatus added in v0.0.4

type CheckStatus struct {
	Code int
}

CheckStatus checks only the statusCode of a response.

func (CheckStatus) Before added in v0.0.5

func (self CheckStatus) Before(t *testing.T)

func (CheckStatus) Check added in v0.0.4

func (self CheckStatus) Check(t *testing.T, response *http.Response, request *server.Request)

Check implements Checker.

type Checker

type Checker interface {
	// Before is called at the beginning of each test, before the handler is called, but after the
	// Update function.
	Before(t *testing.T)

	// Check is called at the end of each test, after the handler has been called. This method must
	// check that the handler has done its job correctly.
	Check(t *testing.T, response *http.Response, request *server.Request)
}

Checker checks that a given response is as expected.

type CheckerFun added in v0.0.4

type CheckerFun func(t *testing.T, response *http.Response, request *server.Request)

func (CheckerFun) Before added in v0.0.5

func (self CheckerFun) Before(t *testing.T)

func (CheckerFun) Check added in v0.0.4

func (self CheckerFun) Check(t *testing.T, response *http.Response, request *server.Request)

type ClientStore

type ClientStore struct {
	Codecs []securecookie.Codec
}

ClientStore is a sessions.Store that store sessions in client requests.

func NewClientStore

func NewClientStore(keys ...[]byte) *ClientStore

func (*ClientStore) Get

func (self *ClientStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get is currently not implemented.

func (*ClientStore) New

func (self *ClientStore) New(r *http.Request, name string) (*sessions.Session, error)

func (*ClientStore) Save

func (self *ClientStore) Save(r *http.Request, w http.ResponseWriter,
	session *sessions.Session) error

type MockResponse added in v0.0.5

type MockResponse struct {
	T        *testing.T
	JsonFct  func(*testing.T, context.Context, interface{})
	ErrorFct func(*testing.T, context.Context, error)
	LoginFct func(*testing.T, context.Context, server.User, *server.Request)
}

func (MockResponse) SendError added in v0.0.5

func (self MockResponse) SendError(ctx context.Context, err error)

func (MockResponse) SendJSON added in v0.0.5

func (self MockResponse) SendJSON(ctx context.Context, data interface{})

func (MockResponse) SendLoginAccepted added in v0.0.5

func (self MockResponse) SendLoginAccepted(ctx context.Context, user server.User,
	request *server.Request)

type Request

type Request struct {
	Method string
	Target *string
	Body   string
	UserId *uint32
}

Request provides information to create an http.Request.

Its zero value is valid and produces the request "GET /a/test". See Make() for details.

func (*Request) Make

func (self *Request) Make() (req *http.Request, err error)

Make generates an http.Request.

Default value for Method is "GET". Default value for Target is "/a/test". If UserId is not nil then a valid session for that user is added to the request.

type Test

type Test struct {
	Name    string
	Update  func(t *testing.T)
	Request Request
	Checker Checker
}

Test represents a test to be executed by Run().

Update is called before the test, if not nil.

Jump to

Keyboard shortcuts

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