Documentation ¶
Index ¶
- Variables
- func Fixture(t *testing.T) (addr string, teardown func())
- func ListenAndRecord(network, laddr string, callback func(*Transmission)) (net.Listener, error)
- func NgrepMarshal(w io.Writer, l *Log) (err error)
- func NgrepUnmarshal(r io.Reader, l *Log) error
- func Record(lis net.Listener, callback func(*Transmission)) (net.Listener, error)
- func SplitHeaderBody(p []byte) (header []byte, body []byte)
- func WriteLog(file string, l *Log) error
- type Connection
- type Connections
- type Log
- type Proxy
- type Server
- type TCPConn
- type Transmission
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyRunning = errors.New("fakerpc: server is already running")
ErrAlreadyRunning is returned when calling ListenAndServe on a server which was already started by ListenAndServe.
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 ¶
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 ¶
NgrepMarshal writes to w the l encoded as a ngrep output.
func NgrepUnmarshal ¶
NgrepUnmarshal parses the ngrep output read from r and stores the result in the l.
func Record ¶
Record records all network communication on the listener.
On failure it closes the listener returning non-nil error.
func SplitHeaderBody ¶
SplitHeaderBody splits raw HTTP request/response into header and body.
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.
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 ¶
NewProxy gives new Proxy for the given target URL and listening on the given TCP network address.
func (*Proxy) ListenAndServe ¶
ListenAndServe starts listening for connections, recording them and proxying to the target URL.
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 (*Server) Addr ¶
Addr returns the Server's network address. It blocks if the srv is not running.
func (*Server) ListenAndServe ¶
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.
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.