httpfake: Index | Files

package httpfake

import ""

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.


Package Files

assertions.go httpfake.go request.go responder.go response.go

func Respond Uses

func Respond(w http.ResponseWriter, r *http.Request, rh *Request)

Respond writes the response based in the request handler settings

type Assertor Uses

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 HTTPFake Uses

type HTTPFake struct {
    Server          *httptest.Server
    RequestHandlers []*Request
    // contains filtered or unexported fields

HTTPFake is the root struct for the fake server

func New Uses

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 Uses

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 Uses

func (f *HTTPFake) NewHandler() *Request

NewHandler initializes the configuration for a new request handler

func (*HTTPFake) Reset Uses

func (f *HTTPFake) Reset() *HTTPFake

Reset wipes the request handlers definitions

func (*HTTPFake) ResolveURL Uses

func (f *HTTPFake) ResolveURL(path string, args ...interface{}) string

ResolveURL resolves the full URL to the fake server for a given path

type Request Uses

type Request struct {
    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 NewRequest Uses

func NewRequest() *Request

NewRequest creates a new Request

func (*Request) AssertBody Uses

func (r *Request) AssertBody(body []byte) *Request

AssertBody will assert that that the provided body matches in the requests to this handler

func (*Request) AssertCustom Uses

func (r *Request) AssertCustom(assertor Assertor) *Request

AssertCustom will run the provided assertor against requests to this handler

func (*Request) AssertHeaderValue Uses

func (r *Request) AssertHeaderValue(key, value string) *Request

AssertHeaderValue will assert that the provided header key and value are present in the requests to this handler

func (*Request) AssertHeaders Uses

func (r *Request) AssertHeaders(keys ...string) *Request

AssertHeaders will assert that the provided header keys are present in the requests to this handler

func (*Request) AssertQueries Uses

func (r *Request) AssertQueries(key ...string) *Request

AssertQueries will assert that the provided query parameters are present in the requests to this handler

func (*Request) AssertQueryValue Uses

func (r *Request) AssertQueryValue(key, value string) *Request

AssertQueryValue will assert that the provided query parameter and value are present in the requests to this handler

func (*Request) Delete Uses

func (r *Request) Delete(path string) *Request

Delete ...

func (*Request) Get Uses

func (r *Request) Get(path string) *Request

Get sets a GET request handler for a given path

func (*Request) Handle Uses

func (r *Request) Handle(handle Responder)

Handle sets a custom handle By setting this responder it gives full control to the user over this request handler

func (*Request) Head Uses

func (r *Request) Head(path string) *Request

Head sets a HEAD request handler for a given path

func (*Request) Patch Uses

func (r *Request) Patch(path string) *Request

Patch sets a PATCH request handler for a given path

func (*Request) Post Uses

func (r *Request) Post(path string) *Request

Post sets a POST request handler for a given path

func (*Request) Put Uses

func (r *Request) Put(path string) *Request

Put sets a PUT request handler for a given path

func (*Request) Reply Uses

func (r *Request) Reply(status int) *Response

Reply sets a response status for this request And returns the Response struct to allow chaining the response settings

type Responder Uses

type Responder func(w http.ResponseWriter, r *http.Request, rh *Request)

Responder are callbacks to handle the request and write the response

type Response Uses

type Response struct {
    StatusCode int
    BodyBuffer []byte
    Header     http.Header

Response stores the settings defined by the request handler of how it will respond the request back

func NewResponse Uses

func NewResponse() *Response

NewResponse creates a new Response

func (*Response) AddHeader Uses

func (r *Response) AddHeader(key, value string) *Response

AddHeader adds a HTTP header into the response

func (*Response) Body Uses

func (r *Response) Body(body []byte) *Response

Body sets the response body from a byte array

func (*Response) BodyString Uses

func (r *Response) BodyString(body string) *Response

BodyString sets the response body from a string Example:

BodyString(`[{"username": "dreamer"}]`)

func (*Response) BodyStruct Uses

func (r *Response) BodyStruct(body interface{}) *Response

BodyStruct sets the response body from a struct. The provided struct will be marsheled to json internally. Example:

BodyStruct(&entity.User{UserName: "dreamer"})

func (*Response) SetHeader Uses

func (r *Response) SetHeader(key, value string) *Response

SetHeader sets the a HTTP header to the response

func (*Response) Status Uses

func (r *Response) Status(status int) *Response

Status sets the response status

type ServerOption Uses

type ServerOption func(opts *ServerOptions)

ServerOption provides a functional signature for providing configuration options to the fake server

func WithTesting Uses

func WithTesting(t *testing.T) 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 Uses

type ServerOptions struct {
    // contains filtered or unexported fields

ServerOptions a configuration object for the fake test server

Package httpfake imports 9 packages (graph). Updated 2020-07-04. Refresh now. Tools for package owners.