reply

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package reply implements different response definition builders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FromTypes

type FromTypes interface{ string | *url.URL }

type FunctionReply

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

FunctionReply represents a reply that will be built using the given function.

func Function

func Function(fn func(*http.Request, M, params.P) (*Response, error)) *FunctionReply

Function returns a FunctionReply that builds a response stub using the given function.

func (*FunctionReply) Build

func (f *FunctionReply) Build(r *http.Request, m M, p params.P) (*Response, error)

Build builds a response function using previously provided function.

type M

type M interface {
	// Hits return mock total hits.
	Hits() int
}

M implements mock data that should be available on reply build functions.

type ProxyReply

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

ProxyReply represents a response stub that will be the response "proxied" from the specified target. Use From or ProxyFrom to init a new ProxyReply.

func From

func From(target string) *ProxyReply

From inits a ProxyReply with the given target. It will parse the raw URL target to an url.URL. It panics if the given target cannot be parsed to a valid url.URL.

func ProxiedFrom

func ProxiedFrom(target *url.URL) *ProxyReply

ProxiedFrom inits a ProxyReply with the given target URL.

func (*ProxyReply) Build

func (r *ProxyReply) Build(req *http.Request, _ M, _ params.P) (*Response, error)

Build builds a Reply based on the ProxyReply configuration.

func (*ProxyReply) Header

func (r *ProxyReply) Header(key, value string) *ProxyReply

Header sets an extra response header that will be set after proxy target responds.

func (*ProxyReply) ProxyHeader

func (r *ProxyReply) ProxyHeader(key, value string) *ProxyReply

ProxyHeader sets an extra header to be sent to the proxy target.

func (*ProxyReply) RemoveProxyHeader

func (r *ProxyReply) RemoveProxyHeader(header string) *ProxyReply

RemoveProxyHeader removes the given before sending the request to the proxy target.

func (*ProxyReply) StripPrefix

func (r *ProxyReply) StripPrefix(prefix string) *ProxyReply

StripPrefix removes the given prefix from the URL before proxying the request.

func (*ProxyReply) StripSuffix

func (r *ProxyReply) StripSuffix(suffix string) *ProxyReply

StripSuffix removes the given suffix from the URL before proxying the request.

type RandomReply

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

RandomReply configures a Reply that serves random HTTP responses.

func Rand

func Rand() *RandomReply

Rand inits a new RandomReply.

func (*RandomReply) Add

func (mr *RandomReply) Add(reply ...Reply) *RandomReply

Add adds a new Reply to the random list.

func (*RandomReply) Build

func (mr *RandomReply) Build(r *http.Request, m M, p params.P) (*Response, error)

Build builds a response stub randomly based on previously added Reply implementations.

type Reply

type Reply interface {
	// Build returns a Response stub to be served.
	Build(*http.Request, M, params.P) (*Response, error)
}

Reply defines the contract to configure an HTTP responder.

type Response

type Response struct {
	Status  int
	Header  http.Header
	Cookies []*http.Cookie
	Body    io.Reader
	Delay   time.Duration
	Mappers []ResponseMapper
}

Response defines the HTTP response that will be served once a Mock is matched for an HTTP Request.

type ResponseMapper

type ResponseMapper func(res *Response, args ResponseMapperArgs) error

ResponseMapper is the function definition to be used to map Mock Response before serving it.

type ResponseMapperArgs

type ResponseMapperArgs struct {
	Request    *http.Request
	Parameters params.P
}

ResponseMapperArgs represents the expected arguments for every ResponseMapper.

type SequentialReply

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

SequentialReply configures a sequence of replies to be used after a mock.Mock is matched to a http.Request.

func Seq

func Seq() *SequentialReply

Seq creates a new SequentialReply.

func (*SequentialReply) Add

func (mr *SequentialReply) Add(reply ...Reply) *SequentialReply

Add adds a new response to the sequence.

func (*SequentialReply) AfterEnded

func (mr *SequentialReply) AfterEnded(reply Reply) *SequentialReply

AfterEnded sets a response to be used once the sequence is over.

func (*SequentialReply) Build

func (mr *SequentialReply) Build(r *http.Request, m M, p params.P) (*Response, error)

Build builds a new response based on current mock.Mock call sequence. When the sequence is over, it will return an error or a previously configured reply for this scenario.

type StdReply

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

StdReply holds the configuration on how the Response should be built.

func Accepted

func Accepted() *StdReply

Accepted creates a new Reply with http.StatusAccepted already.

func BadGateway

func BadGateway() *StdReply

BadGateway creates a new Reply with http.StatusBadGateway already.

func BadRequest

func BadRequest() *StdReply

BadRequest creates a new Reply with http.StatusBadRequest already.

func Created

func Created() *StdReply

Created creates a new Reply with http.StatusCreated already.

func Forbidden

func Forbidden() *StdReply

Forbidden creates a new Reply with http.StatusForbidden already.

func GatewayTimeout

func GatewayTimeout() *StdReply

GatewayTimeout creates a new Reply with http.StatusGatewayTimeout already.

func InternalServerError

func InternalServerError() *StdReply

InternalServerError creates a new Reply with http.StatusInternalServerError already.

func MethodNotAllowed

func MethodNotAllowed() *StdReply

MethodNotAllowed creates a new Reply with http.StatusMethodNotAllowed already.

func MovedPermanently

func MovedPermanently() *StdReply

MovedPermanently creates a new Reply with http.StatusMovedPermanently already.

func MultipleChoices

func MultipleChoices() *StdReply

MultipleChoices creates a new Reply with http.StatusMultipleChoices already.

func New

func New() *StdReply

New creates a new StdReply. Prefer to use factory functions for each status code.

func NoContent

func NoContent() *StdReply

NoContent creates a new Reply with http.StatusNoContent already.

func NotFound

func NotFound() *StdReply

NotFound creates a new Reply with http.StatusNotFound already.

func NotImplemented

func NotImplemented() *StdReply

NotImplemented creates a new Reply with http.StatusNotImplemented already.

func NotModified

func NotModified() *StdReply

NotModified creates a new Reply with http.StatusNotModified already.

func OK

func OK() *StdReply

OK creates a new Reply with http.StatusOK already.

func PartialContent

func PartialContent() *StdReply

PartialContent creates a new Reply with http.StatusPartialContent already.

func ServiceUnavailable

func ServiceUnavailable() *StdReply

ServiceUnavailable creates a new Reply with http.StatusServiceUnavailable already.

func Status

func Status(status int) *StdReply

Status creates a new Reply with the given HTTP status code.

func Unauthorized

func Unauthorized() *StdReply

Unauthorized creates a new Reply with http.StatusUnauthorized already.

func UnprocessableEntity

func UnprocessableEntity() *StdReply

UnprocessableEntity creates a new Reply with http.StatusUnprocessableEntity already.

func (*StdReply) Body

func (rpl *StdReply) Body(value []byte) *StdReply

Body defines the response body using a []byte,

func (*StdReply) BodyJSON

func (rpl *StdReply) BodyJSON(data any) *StdReply

BodyJSON defines the response body encoding the given value using json.Encoder.

func (*StdReply) BodyReader

func (rpl *StdReply) BodyReader(reader io.Reader) *StdReply

BodyReader defines the response body using the given io.Reader.

func (*StdReply) BodyString

func (rpl *StdReply) BodyString(value string) *StdReply

BodyString defines the response body using a string.

func (*StdReply) BodyTemplate

func (rpl *StdReply) BodyTemplate(template any) *StdReply

BodyTemplate defines the response body using a template. It accepts a string or a reply.Template implementation. If a different type is provided, it panics.

func (*StdReply) Build

func (rpl *StdReply) Build(r *http.Request, _ M, _ params.P) (*Response, error)

Build builds a Response based on StdReply definition.

func (*StdReply) Cookie

func (rpl *StdReply) Cookie(cookie http.Cookie) *StdReply

Cookie adds a http.Cookie to the Response.

func (*StdReply) Delay

func (rpl *StdReply) Delay(duration time.Duration) *StdReply

Delay sets a delay time before serving the stub Response.

func (*StdReply) ExpireCookie

func (rpl *StdReply) ExpireCookie(cookie http.Cookie) *StdReply

ExpireCookie expires a cookie.

func (*StdReply) Header

func (rpl *StdReply) Header(key, value string) *StdReply

Header adds a header to the Response.

func (*StdReply) Map

func (rpl *StdReply) Map(mapper ResponseMapper) *StdReply

Map adds ResponseMapper that will be executed after the Response was built.

func (*StdReply) Model

func (rpl *StdReply) Model(model any) *StdReply

Model sets the template data to be used.

func (*StdReply) Status

func (rpl *StdReply) Status(status int) *StdReply

Status sets the HTTP status code for the Response.

type Template

type Template interface {
	// Compile allows pre-compilation of the given template.
	Compile() error

	// Parse parses the given template.
	Parse(io.Writer, any) error
}

Template defines a template parser for response bodies.

type TemplateData

type TemplateData struct {
	// Request is HTTP request ref.
	Request *http.Request

	// Data is the model to be used with the given template.
	// This value is set using the Model() function from StdReply.
	Data any
}

TemplateData is the data model used to render the templates.

type TextTemplate

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

TextTemplate is the built-in text Template interface. It uses Go templates.

func NewTextTemplate

func NewTextTemplate() *TextTemplate

NewTextTemplate creates a new BuiltInTemplate.

func (*TextTemplate) Compile

func (gt *TextTemplate) Compile() error

func (*TextTemplate) FuncMap

func (gt *TextTemplate) FuncMap(fn template.FuncMap) *TextTemplate

FuncMap adds a new function to be used inside the Go template.

func (*TextTemplate) Name

func (gt *TextTemplate) Name(name string) *TextTemplate

Name sets the name of the template.

func (*TextTemplate) Parse

func (gt *TextTemplate) Parse(w io.Writer, data any) error

func (*TextTemplate) Template

func (gt *TextTemplate) Template(tmpl string) *TextTemplate

Template sets the actual template.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳