knockttp

package module
v0.0.0-...-9830a89 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2018 License: MIT Imports: 9 Imported by: 0

README

KnockTTP

A opinionated http mock service for golang with a focus on building testing environment for crawlers.

Documentation

See godoc.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var WildcardMethod = "*"

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

Functions

This section is empty.

Types

type Data

type Data map[string]interface{}

type Handler

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

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

Handle processes a http response

type Host

type Host string

type MethodHandlers

type MethodHandlers map[string]*Handler

MethodHandlers contains handlers for specific methods

func (*MethodHandlers) GetHandler

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

GetHandler returns a Handler given a Method

type Network

type Network struct {
	Services `json:""`
}

func NewNetworkFromReader

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

type Response

type Response struct {
	http.Response
	*bytes.Buffer
}

func NewResponse

func NewResponse(r *http.Request) *Response

func (*Response) Header

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

func (*Response) Write

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

func (*Response) WriteHeader

func (m *Response) WriteHeader(statusCode int)

type Route

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

func (*Route) GetHandler

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

type Routes

type Routes []*Route

func NewRoutesFromFile

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

func NewRoutesFromReader

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

func (*Routes) Find

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

func (*Routes) GetHandler

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

type Services

type Services map[Host]Routes

type TestServer

type TestServer struct {
	*httptest.Server

	Data   map[string]interface{}
	Routes Routes
}

func NewTestServer

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

func (*TestServer) ServeFunc

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

type Transport

type Transport struct {
	Network
	Data
}

func NewTransport

func NewTransport(Network Network, Data Data) *Transport

NewTransport instantiates a transport given a Network and Data

func (*Transport) RoundTrip

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

RoundTrip implements http.RoundTripper

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL