fakerpc

package module
v0.0.0-...-04ae650 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2015 License: MIT Imports: 21 Imported by: 0

README

fakerpc GoDoc Build Status Build Status Build status Coverage Status

A fake server for recording and mocking HTTP-based RPC services.

Installation

~ $ go get -u github.com/rjeczalik/fakerpc

Documentation

godoc.org/github.com/rjeczalik/fakerpc#Fixture

cmd/fakerpc GoDoc

Installation

~ $ go get -u github.com/rjeczalik/fakerpc/cmd/fakerpc

Documentation

godoc.org/github.com/rjeczalik/fakerpc/cmd/fakerpc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyRunning = errors.New("fakerpc: server is already running")

ErrAlreadyRunning is returned when calling ListenAndServe on a server which was already started by ListenAndServe.

View Source
var ErrNotRunning = errors.New("fakerpc: server is not running")

ErrNotRunning is returned when calling Stop on a server which wasn't started by ListenAndServe or was already stopped by Stop.

Functions

func Fixture

func Fixture(t *testing.T) (addr string, teardown func())

Fixture provides a fake server for mocking HTTP-based RPC services. A single fixture has TestXxx-function scope, which makes it sutiable for parallel test execution. Typical usage of the Fixture function is to setup the mock at the begining of the test, deferring returned "teardown" func; the "addr" return value is new network address of the RPC service being tested. Example:

func TestXxxx(t *testing.T) {
  addr, teardown := fakerpc.Fixture(t)
  defer teardown()
  // ...
  client, err := rpc.DialHTTP("tcp", addr)
  // ...

Control

The default behavior of the fake is to reply to requests issued within a test with previously-recorded responses. The fake looks up the record-log file under the ./testdata/{{.testxxxx}}.gzob which is relative to the *_test.go file from which the Fixture was called. The ".testname" is a lower-cased TestXxxx- function name.

A mock server created by the Fixture can be configured to act as:

  • a reply-server (default behavior)
  • a proxy service by setting the target URL with FAKERPC environment variable
  • a recording proxy service - accordingly FAKERPC_RECORD environment variable

In order to create record-log files for the first use of a reply-server, run the tests with a FAKERPC_RECORD environment variable pointing to your service's end point. Example:

$ FAKERPC_RECORD="http://rpc.int.mycompany.com:8079" go test ./...

func ListenAndRecord

func ListenAndRecord(network, laddr string, callback func(*Transmission)) (net.Listener, error)

ListenAndRecord announces on the local network address laddr, recording all the communication.

It calls provided callback after each successful transmission.

func NgrepMarshal

func NgrepMarshal(w io.Writer, l *Log) (err error)

NgrepMarshal writes to w the l encoded as a ngrep output.

func NgrepUnmarshal

func NgrepUnmarshal(r io.Reader, l *Log) error

NgrepUnmarshal parses the ngrep output read from r and stores the result in the l.

func Record

func Record(lis net.Listener, callback func(*Transmission)) (net.Listener, error)

Record records all network communication on the listener.

On failure it closes the listener returning non-nil error.

func SplitHeaderBody

func SplitHeaderBody(p []byte) (header []byte, body []byte)

SplitHeaderBody splits raw HTTP request/response into header and body.

func WriteLog

func WriteLog(file string, l *Log) error

WriteLog writes gzipped, gob-encoded Log struct to the file.

Types

type Connection

type Connection struct {
	Req     *http.Request // a HTTP header of the request
	ReqBody []byte        // a body of the request
	Res     []byte        // raw response
}

A Connection represents a single request/reponse communication.

type Connections

type Connections [][]Connection

Connections represent Log's transmissions grouped per connection.

func NewConnections

func NewConnections(log *Log) (Connections, error)

NewConnections gives a Connections for the given log.

type Log

type Log struct {
	// Network is an address of the networks, in which communication took place.
	Networks []*net.IPNet
	// Filter is an effective pcap filter applied to the recording session.
	Filter string
	// T holds captured transmissions.
	T []Transmission
}

A Log represents communication session, either captured by a Proxy or parsed from a ngrep output.

func NewLog

func NewLog() *Log

NewLog gives a new Log.

func ReadLog

func ReadLog(file string) (*Log, error)

ReadLog gives Log decoded from the given file. It assumes the file contains gzipped, gob-encoded Log struct. If it does not, it treats the file as a ngrep output.

func (*Log) Net

func (l *Log) Net() []string

Net returns the network names with the mask printed in a IP form instead of hexadecimal one.

type Proxy

type Proxy struct {
	// Record function is called after each transmission is successfully completed.
	Record func(*Transmission)
	// contains filtered or unexported fields
}

A Proxy represents a single host HTTP reverse proxy which records all the transmission it handles.

func NewProxy

func NewProxy(addr, target string) (*Proxy, error)

NewProxy gives new Proxy for the given target URL and listening on the given TCP network address.

func (*Proxy) Addr

func (p *Proxy) Addr() (addr net.Addr)

Addr returns the Proxy's network address. It blocks when the p is not running.

func (*Proxy) ListenAndServe

func (p *Proxy) ListenAndServe() (err error)

ListenAndServe starts listening for connections, recording them and proxying to the target URL.

func (*Proxy) Stop

func (p *Proxy) Stop() (l *Log, err error)

Stop stops the Proxy from accepting new connections. It waits for on-going connections to finish, ensuring all of them were captured in the l.

type Server

type Server struct {
	// Reply function is called after each transmission is successfully completed.
	Reply func(src, dst *net.TCPAddr, bodyLen int64, err error)
	// contains filtered or unexported fields
}

A Server represents a HTTP server, which serves connections by replying with recorded responses.

func NewServer

func NewServer(addr string, log *Log) (srv *Server, err error)

NewServer gives new Server for the given address and log.

func (*Server) Addr

func (srv *Server) Addr() (addr net.Addr)

Addr returns the Server's network address. It blocks if the srv is not running.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() (err error)

ListenAndServe starts the server which handles only specific number of connections, determined by the Log argument given during creation of the server; after those connections were served, Server stops itself.

func (*Server) ServeConn

func (srv *Server) ServeConn(rw net.Conn, c []Connection)

ServeConn copies a response from c for every of the rw coonection's request.

func (*Server) Stop

func (srv *Server) Stop() (err error)

Stop stops the Server from accepting new connections. It waits for on-going connections to finish.

type TCPConn

type TCPConn interface {
	TCPConn() *net.TCPConn
}

type Transmission

type Transmission struct {
	// Src is a TCP address of the source.
	Src *net.TCPAddr
	// Dst is a TCP address of the destination.
	Dst *net.TCPAddr
	// Raw contains all the recorded bytes sent from Src to Dst until Dst began
	// replying back to Src.
	Raw []byte
}

A Transmission represents a single raw data transmission between two TCP end points.

Directories

Path Synopsis
cmd
fakerpc
Fakerpc is a command line interface to the fakerpc server.
Fakerpc is a command line interface to the fakerpc server.

Jump to

Keyboard shortcuts

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