testserver

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HandleFunc

type HandleFunc func(requests []*HttpRequest, w http.ResponseWriter, r *http.Request)

HandleFunc is a request handler function that has access to accumulated HttpRequest. Whether the initial requests being nil or empty depends on the test implementation.

type HttpHandler

type HttpHandler struct {
	// contains filtered or unexported fields
}

HttpHandler is an implementation of http.Handler to be used for testing.

func (*HttpHandler) ServeHTTP

func (h *HttpHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP accumulates incoming request into server.requests, then pass it down to its HandleFunc to handle at once.

type HttpRequest

type HttpRequest struct {
	*http.Request
	// contains filtered or unexported fields
}

HttpRequest is a wrapper for *http.Request that provides time recorded when the request has arrived.

func (*HttpRequest) GetTime

func (r *HttpRequest) GetTime() time.Time

GetTime returns time.Time that are recorded when received request.

type TestServer

type TestServer struct {
	*httptest.Server
	// contains filtered or unexported fields
}

TestServer is a wrapper for httptest.Server such that it could support accumulation of incoming requests.

func NewServer

func NewServer(h HandleFunc) *TestServer

NewServer returns a server with given HandleFunc.

func WithResponseAfter

func WithResponseAfter(t *testing.T, initial, after []byte, n int) *TestServer

WithResponseAfter returns a test server that responds differently after n times of requests. Do note that if n is set to 5, 5th request will receive initial.

func WithRotatingResponses

func WithRotatingResponses(t *testing.T, payload ...[]byte) *TestServer

WithRotatingResponses creates a server with rotating response. If no payload is given, the payload will be

func (*TestServer) Requests

func (f *TestServer) Requests() []*HttpRequest

Requests returns accumulated requests that this server instance have received.

Jump to

Keyboard shortcuts

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