rr

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: ISC, BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package rr implements HTTP record and replay, mainly for use in tests.

Open creates a new RecordReplay. Whether it is recording or replaying is controlled by the -httprecord flag, which is defined by this package only in test programs (built by “go test”). See the Open documentation for more details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body struct {
	Data       []byte
	ReadOffset int
}

A Body is an io.ReadCloser used as an HTTP request body. In a Scrubber, if req.Body != nil, then req.Body is guaranteed to have type *Body, making it easy to access the body to change it.

func (*Body) Close

func (b *Body) Close() error

Close is a no-op, implementing io.Closer.

func (*Body) Read

func (b *Body) Read(p []byte) (int, error)

Read reads from the body, implementing io.Reader.

type RecordReplay

type RecordReplay struct {
	// contains filtered or unexported fields
}

A RecordReplay is an http.RoundTripper that can operate in two modes: record and replay.

In record mode, the RecordReplay invokes another RoundTripper and logs the (request, response) pairs to a file.

In replay mode, the RecordReplay responds to requests by finding an identical request in the log and sending the logged response.

func Open

func Open(file string, rt http.RoundTripper) (*RecordReplay, error)

Open opens a new record/replay log in the named file and returns a RecordReplay backed by that file.

By default Open expects the file to exist and contain a previously-recorded log of (request, response) pairs, which RecordReplay.RoundTrip consults to prepare its responses.

If the command-line flag -httprecord is set to a non-empty regular expression that matches file, then Open creates the file as a new log. In that mode, RecordReplay.RoundTrip makes actual HTTP requests using rt but then logs the requests and responses to the file for replaying in a future run.

func (*RecordReplay) Client

func (rr *RecordReplay) Client() *http.Client

Client returns an http.Client using rr as its transport. It is a shorthand for:

return &http.Client{Transport: rr}

For more complicated uses, use rr or the RecordReplay.RoundTrip method directly.

func (*RecordReplay) Close

func (rr *RecordReplay) Close() error

Close closes the RecordReplay. It is a no-op in replay mode.

func (*RecordReplay) Recording

func (rr *RecordReplay) Recording() bool

Recording reports whether the rr is in recording mode.

func (*RecordReplay) RoundTrip

func (rr *RecordReplay) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

If rr has been opened in record mode, RoundTrip passes the requests on to the RoundTripper specified in the call to Open and then logs the (request, response) pair to the underlying file.

If rr has been opened in replay mode, RoundTrip looks up the request in the log and then responds with the previously logged response.

If the log does not contain req, RoundTrip returns an error.

func (*RecordReplay) Scrub

func (rr *RecordReplay) Scrub(scrubs ...func(req *http.Request) error)

Scrub adds new scrubbing functions to rr.

Before using a request as a lookup key or saving it in the record/replay log, the RecordReplay calls each scrub function, in the order they were registered, to canonicalize non-deterministic parts of the request and remove secrets.

Scrubbing only applies to a copy of the request used in the record/replay log; the unmodified original request is sent to the actual server in recording mode. A scrub function can assume that if req.Body is not nil, then it has type *Body.

Calling Scrub adds to the list of registered scrubbing functions; it does not replace those registered by earlier calls.

Jump to

Keyboard shortcuts

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