servertest

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: AGPL-3.0 Imports: 15 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 ChainPrepare added in v0.1.7

func ChainPrepare(t *testing.T, loc *ioc.Locator, chain ...interface {
	Prepare(*testing.T, *ioc.Locator) *ioc.Locator
}) *ioc.Locator

ChainPrepare sequentially call the Prepare method of its arguments. The methods are called in the same order as the arguments. The locator is updated by each call, then returned.

func FindCookie

func FindCookie(response *http.Response, name string) (found *http.Cookie)

FindCookie returns a cookie by name.

func Run

func Run(t *testing.T, tests []Test, handlerFactory interface{})

Run executes all the given tests on the given Handler.

The tests are executed in the given order. For each test, the handler is created by calling handlerFactory using the ioc.Locator returned by Prepare. The handler is registered for the pattern "/a/test".

If a test implement the method

ChangeResponse(*testing.T, server.Response) server.Response

then this method is called with the usual server.Response objet, and the returned object is used by the handler. This method allows the test to spy the calls to server.Response methods.

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 CheckCookieIsSet

type CheckCookieIsSet struct {
	Name string
}

func (CheckCookieIsSet) Check

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

type CheckError

type CheckError struct {
	Code int
	Body string
}

CheckError checks the statusCode and error message of a response.

func (CheckError) Check

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

type CheckJSON

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) Check

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

type CheckStatus

type CheckStatus struct {
	Code int
}

CheckStatus checks only the statusCode of a response.

func (CheckStatus) Check

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

type Checker

type Checker interface {
	// 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

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

CheckerFun wraps a function into a Checker.

func (CheckerFun) Check

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 Request

type Request struct {
	Method     string
	Target     *string
	RemoteAddr *string
	Body       string
	UserId     *uint32
	Hash       *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(t *testing.T) (req *http.Request, err error)

Make generates an http.Request.

Default value for Method is "GET". Default value for Target is "/a/test". If RemoteAddr is not nil, the RemoteAddr field of the returned request is set to its value. If UserId is not nil and Hash is nil then a valid session for that user is added to the request. If UserId and Hash are both non-nil then an "unlogged cookie" is added to the request.

type ResponseSpy

type ResponseSpy struct {
	Backend     server.Response
	T           *testing.T
	JsonFct     func(*testing.T, context.Context, interface{})
	ErrorFct    func(*testing.T, context.Context, error)
	RedirectFct func(*testing.T, context.Context, *server.Request, string)
	LoginFct    func(*testing.T, context.Context, server.User, *server.Request, interface{})
	UnloggedFct func(*testing.T, context.Context, server.User, *server.Request) error
}

func (ResponseSpy) SendError

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

func (ResponseSpy) SendJSON

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

func (ResponseSpy) SendLoginAccepted

func (self ResponseSpy) SendLoginAccepted(ctx context.Context, user server.User,
	request *server.Request, profile interface{})

func (ResponseSpy) SendRedirect added in v0.1.7

func (self ResponseSpy) SendRedirect(ctx context.Context, req *server.Request, url string)

func (ResponseSpy) SendUnloggedId

func (self ResponseSpy) SendUnloggedId(ctx context.Context, user server.User,
	request *server.Request) error

type T

type T struct {
	Name    string
	Request Request

	// Update is called before the test, if not nil.
	Update func(t *testing.T)

	Checker Checker
}

T is a simple implementation of Test.

func (*T) Check

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

func (*T) Close

func (self *T) Close()

func (*T) GetName

func (self *T) GetName() string

func (*T) GetRequest

func (self *T) GetRequest(t *testing.T) *Request

func (*T) Prepare

func (self *T) Prepare(t *testing.T, loc *ioc.Locator) *ioc.Locator

Prepare runs before the handler is executed. If Update is not nil, it is called first. If Checker implements a method Before(*testing.T) then it is called next.

type Test

type Test interface {
	GetName() string

	// Prepare is called before the handler is created.
	Prepare(t *testing.T, loc *ioc.Locator) *ioc.Locator

	// GetRequest is called just after the handler is created.
	GetRequest(t *testing.T) *Request

	Checker
	Close()
}

Test represents a test to be executed by Run(). A simple implementation is given by T.

type WithChecker

type WithChecker struct {
	Checker Checker
}

WithChecker is a Test mixin that uses a Checker to check the response of the handler.

func (WithChecker) Check

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

func (WithChecker) Prepare

func (self WithChecker) Prepare(t *testing.T, loc *ioc.Locator) *ioc.Locator

Prepare call the Before method of the Checker, it it exists.

type WithName

type WithName struct {
	Name string
}

WithName is a Test mixin whose name is given by a field of the test.

func (WithName) GetName

func (self WithName) GetName() string

Jump to

Keyboard shortcuts

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