Documentation ¶
Overview ¶
Package httpfake provides a simple wrapper for httptest with a handful chainable API for setting up handlers to a fake server. This package is aimed to be used in tests where the original external server must not be reached. Instead is used in its place a fake server which can be configured to handle any request as desired.
Index ¶
- func Respond(w http.ResponseWriter, r *http.Request, rh *Request)
- type HTTPFake
- type Request
- func (r *Request) Delete(path string) *Request
- func (r *Request) Get(path string) *Request
- func (r *Request) Handle(handle Responder)
- func (r *Request) Head(path string) *Request
- func (r *Request) Patch(path string) *Request
- func (r *Request) Post(path string) *Request
- func (r *Request) Put(path string) *Request
- func (r *Request) Reply(status int) *Response
- type Responder
- type Response
- func (r *Response) AddHeader(key, value string) *Response
- func (r *Response) Body(body []byte) *Response
- func (r *Response) BodyString(body string) *Response
- func (r *Response) BodyStruct(body interface{}) *Response
- func (r *Response) SetHeader(key, value string) *Response
- func (r *Response) Status(status int) *Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type HTTPFake ¶
HTTPFake is the root struct for the fake server
func New ¶
func New() *HTTPFake
New starts a httptest.Server as the fake server and sets up the initial configuration to this server's request handlers
func (*HTTPFake) NewHandler ¶
NewHandler initializes the configuration for a new request handler
func (*HTTPFake) ResolveURL ¶
ResolveURL resolves the full URL to the fake server for a given path
type Request ¶
Request stores the settings for a request handler Such as how to match this handler for the incoming requests And how this request will respond back
func (*Request) Handle ¶
Handle sets a custom handle By setting this responder it gives full control to the user over this request handler
type Responder ¶
type Responder func(w http.ResponseWriter, r *http.Request, rh *Request)
Responder are callbacks to handle the request and write the response
type Response ¶
Response stores the settings defined by the request handler of how it will respond the request back
func (*Response) BodyString ¶
BodyString sets the response body from a string Example:
BodyString(`[{"username": "dreamer"}]`)
func (*Response) BodyStruct ¶ added in v1.1.0
BodyStruct sets the response body from a struct. The provided struct will be marsheled to json internally. Example:
BodyStruct(&entity.User{UserName: "dreamer"})