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 Assertor
- type CustomAssertor
- type HTTPFake
- type Request
- func (r *Request) AssertBody(body []byte) *Request
- func (r *Request) AssertCustom(assertor Assertor) *Request
- func (r *Request) AssertHeaderValue(key, value string) *Request
- func (r *Request) AssertHeaders(keys ...string) *Request
- func (r *Request) AssertQueries(key ...string) *Request
- func (r *Request) AssertQueryValue(key, value string) *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
- type ServerOption
- type ServerOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Assertor ¶ added in v1.2.0
type Assertor interface { Assert(r *http.Request) error Log(t testing.TB) Error(t testing.TB, err error) }
Assertor provides an interface for setting assertions for http requests
type CustomAssertor ¶ added in v1.2.2
CustomAssertor provides a function signature that implements the Assertor interface. This allows for adhoc creation of a custom assertion for use with the AssertCustom assertor.
func (CustomAssertor) Assert ¶ added in v1.2.2
func (c CustomAssertor) Assert(r *http.Request) error
Assert runs the CustomAssertor assertion against the provided request
func (CustomAssertor) Error ¶ added in v1.2.2
func (c CustomAssertor) Error(t testing.TB, err error)
Error prints a testing error for the CustomAssertor
func (CustomAssertor) Log ¶ added in v1.2.2
func (c CustomAssertor) Log(t testing.TB)
Log prints a testing info log for the CustomAssertor
type HTTPFake ¶
type HTTPFake struct { Server *httptest.Server RequestHandlers []*Request // contains filtered or unexported fields }
HTTPFake is the root struct for the fake server
func New ¶
func New(opts ...ServerOption) *HTTPFake
New starts a httptest.Server as the fake server and sets up the initial configuration to this server's request handlers
func (*HTTPFake) Close ¶ added in v1.2.0
func (f *HTTPFake) Close()
Close shuts down the HTTP Test server, this will block until all outstanding requests on the server have completed. If the WithTesting option was specified when setting up the server Close will assert that each http handler specified for this server was called
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 ¶
type Request struct { sync.Mutex Method string URL *url.URL Response *Response CustomHandle Responder // contains filtered or unexported fields }
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) AssertBody ¶ added in v1.2.0
AssertBody will assert that that the provided body matches in the requests to this handler
func (*Request) AssertCustom ¶ added in v1.2.0
AssertCustom will run the provided assertor against requests to this handler
func (*Request) AssertHeaderValue ¶ added in v1.2.0
AssertHeaderValue will assert that the provided header key and value are present in the requests to this handler
func (*Request) AssertHeaders ¶ added in v1.2.0
AssertHeaders will assert that the provided header keys are present in the requests to this handler
func (*Request) AssertQueries ¶ added in v1.2.0
AssertQueries will assert that the provided query parameters are present in the requests to this handler
func (*Request) AssertQueryValue ¶ added in v1.2.0
AssertQueryValue will assert that the provided query parameter and value are present in the requests to this handler
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"})
type ServerOption ¶ added in v1.2.0
type ServerOption func(opts *ServerOptions)
ServerOption provides a functional signature for providing configuration options to the fake server
func WithTesting ¶ added in v1.2.0
func WithTesting(t testing.TB) ServerOption
WithTesting returns a configuration function that allows you to configure the testing object on the fake server. The testing object is utilized for assertions set on the request object and will throw a testing error if an endpoint is not called.
type ServerOptions ¶ added in v1.2.0
type ServerOptions struct {
// contains filtered or unexported fields
}
ServerOptions a configuration object for the fake test server