Documentation
¶
Overview ¶
Package servertest provides methods and types to test server.Handler implementations.
Index ¶
- Variables
- func FindCookie(response *http.Response, name string) (found *http.Cookie)
- func Run(t *testing.T, tests []Test, handler server.Handler)
- func RunFunc(t *testing.T, tests []Test, handler server.HandleFunction)
- type CheckCookieIsSet
- type CheckError
- type CheckJSON
- type CheckStatus
- type Checker
- type CheckerFun
- type ClientStore
- type MockResponse
- func (self MockResponse) SendError(ctx context.Context, err error)
- func (self MockResponse) SendJSON(ctx context.Context, data interface{})
- func (self MockResponse) SendLoginAccepted(ctx context.Context, user server.User, request *server.Request)
- func (self MockResponse) SendUnloggedId(ctx context.Context, user server.User, request *server.Request) error
- type Request
- type T
- type Test
Constants ¶
This section is empty.
Variables ¶
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)
}
})
var (
Unimplemented = errors.New("Unimplemented")
)
Functions ¶
func FindCookie ¶ added in v0.1.2
FindCookie returns a cookie by name.
func Run ¶
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.
Types ¶
type CheckCookieIsSet ¶ added in v0.1.2
type CheckCookieIsSet struct {
Name string
}
type CheckError ¶ added in v0.0.4
CheckError checks the statusCode and error message of a response.
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.
type CheckStatus ¶ added in v0.0.4
type CheckStatus struct {
Code int
}
CheckStatus checks only the statusCode of a response.
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 ¶ added in v0.0.4
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) 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)
UnloggedFct func(*testing.T, context.Context, server.User, *server.Request) error
}
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 (MockResponse) SendUnloggedId ¶ added in v0.1.2
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 ¶
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.