Documentation
¶
Overview ¶
Package reply implements different response definition builders.
Index ¶
- type FromTypes
- type FunctionReply
- type M
- type ProxyReply
- func (r *ProxyReply) Build(req *http.Request, _ M, _ params.P) (*Response, error)
- func (r *ProxyReply) Header(key, value string) *ProxyReply
- func (r *ProxyReply) ProxyHeader(key, value string) *ProxyReply
- func (r *ProxyReply) RemoveProxyHeader(header string) *ProxyReply
- func (r *ProxyReply) StripPrefix(prefix string) *ProxyReply
- func (r *ProxyReply) StripSuffix(suffix string) *ProxyReply
- type RandomReply
- type Reply
- type Response
- type ResponseMapper
- type ResponseMapperArgs
- type SequentialReply
- type StdReply
- func Accepted() *StdReply
- func BadGateway() *StdReply
- func BadRequest() *StdReply
- func Created() *StdReply
- func Forbidden() *StdReply
- func GatewayTimeout() *StdReply
- func InternalServerError() *StdReply
- func MethodNotAllowed() *StdReply
- func MovedPermanently() *StdReply
- func MultipleChoices() *StdReply
- func New() *StdReply
- func NoContent() *StdReply
- func NotFound() *StdReply
- func NotImplemented() *StdReply
- func NotModified() *StdReply
- func OK() *StdReply
- func PartialContent() *StdReply
- func ServiceUnavailable() *StdReply
- func Status(status int) *StdReply
- func Unauthorized() *StdReply
- func UnprocessableEntity() *StdReply
- func (rpl *StdReply) Body(value []byte) *StdReply
- func (rpl *StdReply) BodyJSON(data any) *StdReply
- func (rpl *StdReply) BodyReader(reader io.Reader) *StdReply
- func (rpl *StdReply) BodyString(value string) *StdReply
- func (rpl *StdReply) BodyTemplate(template any) *StdReply
- func (rpl *StdReply) Build(r *http.Request, _ M, _ params.P) (*Response, error)
- func (rpl *StdReply) Cookie(cookie http.Cookie) *StdReply
- func (rpl *StdReply) Delay(duration time.Duration) *StdReply
- func (rpl *StdReply) ExpireCookie(cookie http.Cookie) *StdReply
- func (rpl *StdReply) Header(key, value string) *StdReply
- func (rpl *StdReply) Map(mapper ResponseMapper) *StdReply
- func (rpl *StdReply) Model(model any) *StdReply
- func (rpl *StdReply) Status(status int) *StdReply
- type Template
- type TemplateData
- type TextTemplate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FunctionReply ¶
type FunctionReply struct {
// contains filtered or unexported fields
}
FunctionReply represents a reply that will be built using the given 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) 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 (*RandomReply) Add ¶
func (mr *RandomReply) Add(reply ...Reply) *RandomReply
Add adds a new Reply to the random list.
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 ¶
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 (*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.
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 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 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) BodyJSON ¶
BodyJSON defines the response body encoding the given value using json.Encoder.
func (*StdReply) BodyReader ¶
BodyReader defines the response body using the given io.Reader.
func (*StdReply) BodyString ¶
BodyString defines the response body using a string.
func (*StdReply) BodyTemplate ¶
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) ExpireCookie ¶
ExpireCookie expires a cookie.
func (*StdReply) Map ¶
func (rpl *StdReply) Map(mapper ResponseMapper) *StdReply
Map adds ResponseMapper that will be executed after the Response was built.
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) Template ¶
func (gt *TextTemplate) Template(tmpl string) *TextTemplate
Template sets the actual template.