Documentation
¶
Index ¶
- Constants
- Variables
- func NewHTTPServer(t testing.TB, router http.Handler) *httptest.Server
- type ContextRouter
- type Response
- type ResponseOption
- func ROptAccepted() *ResponseOption
- func ROptBadGateway() *ResponseOption
- func ROptBadRequest() *ResponseOption
- func ROptCreated() *ResponseOption
- func ROptForbidden() *ResponseOption
- func ROptGatewayTimeout() *ResponseOption
- func ROptNoContent() *ResponseOption
- func ROptNotFound() *ResponseOption
- func ROptNotImplemented() *ResponseOption
- func ROptServerError() *ResponseOption
- func ROptServiceUnavailable() *ResponseOption
- func ROptStatusOK() *ResponseOption
- func ROptUnauthorized() *ResponseOption
- func ROptUnknownError() *ResponseOption
- func ROptWithHeaders(keyValues ...string) *ResponseOption
- func ROptWithOK() *ResponseOption
- type Router
- type StackedRouter
Constants ¶
const (
// HTTPUnknownError sets http server unknown error.
HTTPUnknownError = 520
)
Exportable constants for HTTP: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Variables ¶
var NotFound = &Response{ Body: bytes.NewBuffer([]byte("Not found")), StatusCode: http.StatusNotFound, }
NotFound is a response that returns http non found (404) status with plain text. This response is used as a default response, if a test server wasn't configured to send any other responses.
Functions ¶
Types ¶
type ContextRouter ¶
type ContextRouter struct {
Router
// contains filtered or unexported fields
}
ContextRouter extends Router with a context.
func (*ContextRouter) ServeHTTP ¶
func (router *ContextRouter) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP implements http.Handler, which implements a simple http server that emulates remote server behavior.
func (*ContextRouter) WithServerURL ¶
func (router *ContextRouter) WithServerURL(url string) *ContextRouter
WithServerURL sets context server url
type Response ¶
type Response struct {
// StatusCode keeps http return status code, e.g. HTTP 200 OK.
StatusCode int
// Headers in a plain form, a slice can be unbalanced, in this case blank value is used.
Headers []string
// Body represents response contents. For testing purposes there's only [io.Reader]
// requirement. A consumer should maintain Close operation on their own.
Body io.Reader
// Reload keeps a switch if [Body] has to be reloaded (i.e. seek back to the beginning).
Reload bool
}
Response reflect a simple object which is used in Router responses.
func NewResponse ¶
func NewResponse(reader io.Reader, options ...*ResponseOption) *Response
NewResponse creates Response with any given options, see ROpt... functions as source of available options.
func NewResponseString ¶
func NewResponseString(src string, options ...*ResponseOption) *Response
NewResponseString is the same as NewResponse, src string will be used.
func (*Response) SetHeaders ¶
SetHeaders write headers regarding the context. In case of unbalanced Headers slice the blank value will be used.
type ResponseOption ¶
type ResponseOption struct {
// StatusCode sets response status code, by default its 200 HTTP OK.
StatusCode int
// Headers keeps a "key, value" format for headers.
// Unbalanced slice is valid, in this case a value will be blank.
Headers []string
// Reload rewinds contents back after it has been served. Otherwise blank
// content will be used.
//
// TODO: on false there could be [NotFound] use instead of blank contents.
// we need to reconsider.
Reload *bool
}
ResponseOption keeps different options for responses used in [HTTPServer] objects.
func ROptGatewayTimeout ¶
func ROptGatewayTimeout() *ResponseOption
ROptGatewayTimeout : 503 GATEWAY TIMEOUT
func ROptNotImplemented ¶
func ROptNotImplemented() *ResponseOption
ROptNotImplemented : 501 NOT IMPLEMENTED
func ROptServerError ¶
func ROptServerError() *ResponseOption
ROptServerError : 500 INTERNAL SERVER ERROR
func ROptServiceUnavailable ¶
func ROptServiceUnavailable() *ResponseOption
ROptServiceUnavailable : 503 SERVICE UNAVAILABLE
func ROptUnknownError ¶
func ROptUnknownError() *ResponseOption
ROptUnknownError : 520 Web Server Returned an Unknown Error.
func ROptWithHeaders ¶
func ROptWithHeaders(keyValues ...string) *ResponseOption
ROptWithHeaders sets http headers using key, value repeating arguments.
Example:
ROptWithHeaders("X-Key", "value", "X-Ref") // the same as below
ROptWithHeaders("X-Key", "value", "X-Ref", "") // the same as above
type Router ¶
Router represents an http router based on simple endpoints (i.e. /) and given responses. Note, Router is not performance optimized. It might work slow for intense operations. see [Router.match] for implementation details.
TODO: decide if match should be optimized.
type StackedRouter ¶
type StackedRouter struct {
Responses []*Response
// contains filtered or unexported fields
}
StackedRouter is a Router that returns responses as a stack. For example, you have a single endpoint, but it's required to return different responses. In this case StackedRouter might help. Note, that once stack (Responses) are blank NotFound will be used.
func NewStackedRouter ¶
func NewStackedRouter(responses []*Response) *StackedRouter
NewStackedRouter initializes StackedRouter with the given responses. Might be nil, but in this case NotFound response will be used.
func (*StackedRouter) ServeHTTP ¶
func (router *StackedRouter) ServeHTTP(writer http.ResponseWriter, _ *http.Request)
ServeHTTP implements http.Handler, which implements a simple http server that emulates remote server behavior.
func (*StackedRouter) WithServerURL ¶
func (router *StackedRouter) WithServerURL(url string) *StackedRouter
WithServerURL sets Server URL to context.