knockttp: Index | Files

package knockttp

import ""

Package knockttp is a Go library providing to mock a http server for testing purposes

It has been initially designed to test crawlers reproducing different web scenarios.

There are two modes of operation:

Transport - works by setting a http.RoundTripper in your client requests. Due the nature of this implementation, a multiple host/service setup can be implemented.

Server - creates a test server into a random port


Package Files

doc.go method.go network.go response.go routes.go server.go transport.go


var WildcardMethod = "*"

WildcardMethod points to the default handler when there is no explicit method defined

type Data Uses

type Data map[string]interface{}

type Handler Uses

type Handler struct {
    ContentType string `json:"content_type,omitempty"`
    StatusCode  int    `json:"status_code,omitempty"`
    //Location is specified if a redirect is required
    Location string      `json:"location,omitempty"`
    Filename string      `json:"filename,omitempty"`
    Body     []byte      `json:"body,omitempty"`
    Headers  http.Header `json:"headers,omitempty"`
    // contains filtered or unexported fields

Handler contains the elements to build a response

func (*Handler) Handle Uses

func (m *Handler) Handle(w http.ResponseWriter, r *http.Request, Data Data)

Handle processes a http response

type Host Uses

type Host string

type MethodHandlers Uses

type MethodHandlers map[string]*Handler

MethodHandlers contains handlers for specific methods

func (*MethodHandlers) GetHandler Uses

func (m *MethodHandlers) GetHandler(Method string) (*Handler, bool)

GetHandler returns a Handler given a Method

type Network Uses

type Network struct {
    Services `json:""`

func NewNetworkFromReader Uses

func NewNetworkFromReader(r io.Reader) (*Network, error)

type Response Uses

type Response struct {

func NewResponse Uses

func NewResponse(r *http.Request) *Response

func (*Response) Header Uses

func (m *Response) Header() http.Header

func (*Response) Write Uses

func (m *Response) Write(p []byte) (int, error)

func (*Response) WriteHeader Uses

func (m *Response) WriteHeader(statusCode int)

type Route Uses

type Route struct {
    Path    string         `json:"query,omitempty"`
    Methods MethodHandlers `json:"methods,omitempty"`

func (*Route) GetHandler Uses

func (r *Route) GetHandler(Method string) (*Handler, bool)

type Routes Uses

type Routes []*Route

func NewRoutesFromFile Uses

func NewRoutesFromFile(Filename string) (*Routes, error)

func NewRoutesFromReader Uses

func NewRoutesFromReader(r io.Reader) (*Routes, error)

func (*Routes) Find Uses

func (q *Routes) Find(r *http.Request) (*Route, bool)

func (*Routes) GetHandler Uses

func (r *Routes) GetHandler(req *http.Request) (*Route, *Handler, bool)

type Services Uses

type Services map[Host]Routes

type TestServer Uses

type TestServer struct {

    Data   map[string]interface{}
    Routes Routes

func NewTestServer Uses

func NewTestServer(routes Routes) (*TestServer, error)

func (*TestServer) ServeFunc Uses

func (ts *TestServer) ServeFunc(route *Route) func(w http.ResponseWriter, r *http.Request)

type Transport Uses

type Transport struct {

func NewTransport Uses

func NewTransport(Network Network, Data Data) *Transport

NewTransport instantiates a transport given a Network and Data

func (*Transport) RoundTrip Uses

func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper

Package knockttp imports 9 packages (graph). Updated 2018-10-23. Refresh now. Tools for package owners.