http

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HTTPUnknownError sets http server unknown error.
	HTTPUnknownError = 520
)

Exportable constants for HTTP: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Variables

View Source
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

func NewHTTPServer

func NewHTTPServer(t testing.TB, router http.Handler) *httptest.Server

NewHTTPServer creates a testing http server, testing.T object is used to properly close all resources automatically.

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

func (r *Response) SetHeaders(c context, header http.Header)

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 ROptAccepted

func ROptAccepted() *ResponseOption

ROptAccepted : HTTP 202 ACCEPTED

func ROptBadGateway

func ROptBadGateway() *ResponseOption

ROptBadGateway : 502 BAD GATEWAY

func ROptBadRequest

func ROptBadRequest() *ResponseOption

ROptBadRequest : 400 BAD REQUEST

func ROptCreated

func ROptCreated() *ResponseOption

ROptCreated : HTTP 201 CREATED

func ROptForbidden

func ROptForbidden() *ResponseOption

ROptForbidden : HTTP 403 FORBIDDEN

func ROptGatewayTimeout

func ROptGatewayTimeout() *ResponseOption

ROptGatewayTimeout : 503 GATEWAY TIMEOUT

func ROptNoContent

func ROptNoContent() *ResponseOption

ROptNoContent : HTTP 204 NO CONTENT

func ROptNotFound

func ROptNotFound() *ResponseOption

ROptNotFound : 404 NOT FOUND

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 ROptStatusOK

func ROptStatusOK() *ResponseOption

ROptStatusOK : HTTP 200 OK

func ROptUnauthorized

func ROptUnauthorized() *ResponseOption

ROptUnauthorized : 401 UNAUTHORIZED

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

func ROptWithOK

func ROptWithOK() *ResponseOption

ROptWithOK alias for ROptStatusOK.

type Router

type Router map[string]*Response

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.

func (Router) ServeHTTP

func (r Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP implements http.Handler.

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.

Jump to

Keyboard shortcuts

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