recorder

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: BSD-2-Clause Imports: 10 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMode = errors.New("invalid recorder mode")

ErrInvalidMode is returned when attempting to start the recorder with invalid mode

Functions

This section is empty.

Types

type Hook added in v3.1.0

type Hook struct {
	// Handler is the function which will be invoked
	Handler HookFunc

	// Kind represents the hook kind
	Kind HookKind
}

Hook represents a function hook of a given kind. Depending on the hook kind, the function will be invoked in different stages of the playback.

func NewHook added in v3.1.0

func NewHook(handler HookFunc, kind HookKind) *Hook

NewHook creates a new hook

type HookFunc added in v3.1.0

type HookFunc func(i *cassette.Interaction) error

HookFunc represents a function, which will be invoked in different stages of the playback. The hook functions allow for plugging in to the playback and transform an interaction, if needed. For example a hook function might redact or remove sensitive data from a request/response before it is added to the in-memory cassette, or before it is saved on disk. Another use case would be to transform the HTTP response before it is returned to the client during replay mode.

type HookKind added in v3.1.0

type HookKind int

Hook kinds

const (
	// AfterCaptureHook represents a hook, which will be invoked
	// after capturing a request/response pair.
	AfterCaptureHook HookKind = iota

	// BeforeSaveHook represents a hook, which will be invoked
	// right before the cassette is saved on disk.
	BeforeSaveHook

	// BeforeResponseReplayHook represents a hook, which will be
	// invoked before replaying a previously recorded response to
	// the client.
	BeforeResponseReplayHook

	// OnRecorderStopHook is a hook, which will be invoked when the recorder
	// is about to be stopped. This hook is useful for performing any
	// post-actions such as cleanup or reporting.
	OnRecorderStopHook
)

type Mode

type Mode int

Mode represents the mode of operation of the recorder

const (
	// ModeRecordOnly specifies that VCR will run in recording
	// mode only. HTTP interactions will be recorded for each
	// interaction. If the cassette file is present, it will be
	// overwritten.
	ModeRecordOnly Mode = iota

	// ModeReplayOnly specifies that VCR will only replay
	// interactions from previously recorded cassette. If an
	// interaction is missing from the cassette it will return
	// ErrInteractionNotFound error. If the cassette file is
	// missing it will return ErrCassetteNotFound error.
	ModeReplayOnly

	// ModeReplayWithNewEpisodes starts the recorder in replay
	// mode, where existing interactions are returned from the
	// cassette, and missing ones will be recorded and added to
	// the cassette. This mode is useful in cases where you need
	// to update an existing cassette with new interactions, but
	// don't want to wipe out previously recorded interactions.
	// If the cassette file is missing it will create a new one.
	ModeReplayWithNewEpisodes

	// ModeRecordOnce will record new HTTP interactions once only.
	// This mode is useful in cases where you need to record a set
	// of interactions once only and replay only the known
	// interactions. Unknown/missing interactions will cause the
	// recorder to return an ErrInteractionNotFound error. If the
	// cassette file is missing, it will be created.
	ModeRecordOnce

	// ModePassthrough specifies that VCR will not record any
	// interactions at all. In this mode all HTTP requests will be
	// forwarded to the endpoints using the real HTTP transport.
	// In this mode no cassette will be created.
	ModePassthrough
)

Recorder states

type Options

type Options struct {
	// CassetteName is the name of the cassette
	CassetteName string

	// Mode is the operating mode of the Recorder
	Mode Mode

	// RealTransport is the underlying http.RoundTripper to make
	// the real requests
	RealTransport http.RoundTripper

	// SkipRequestLatency, if set to true will not simulate the
	// latency of the recorded interaction. When set to false
	// (default) it will block for the period of time taken by the
	// original request to simulate the latency between our
	// recorder and the remote endpoints.
	SkipRequestLatency bool
}

Option represents the Recorder options

type PassthroughFunc

type PassthroughFunc func(req *http.Request) bool

PassthroughFunc is handler which determines whether a specific HTTP request is to be forwarded to the original endpoint. It should return true when a request needs to be passed through, and false otherwise.

type Recorder

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

Recorder represents a type used to record and replay client and server interactions

func New

func New(cassetteName string) (*Recorder, error)

New creates a new recorder

func NewWithOptions

func NewWithOptions(opts *Options) (*Recorder, error)

NewWithOptions creates a new recorder based on the provided options

func (*Recorder) AddHook added in v3.1.0

func (rec *Recorder) AddHook(handler HookFunc, kind HookKind)

AddHook appends a hook to the recorder. Depending on the hook kind, the handler will be invoked in different stages of the playback.

func (*Recorder) AddPassthrough

func (rec *Recorder) AddPassthrough(pass PassthroughFunc)

AddPassthrough appends a hook to determine if a request should be ignored by the recorder.

func (*Recorder) CancelRequest

func (rec *Recorder) CancelRequest(req *http.Request)

CancelRequest implements the github.com/coreos/etcd/client.CancelableTransport interface

func (*Recorder) GetDefaultClient

func (rec *Recorder) GetDefaultClient() *http.Client

GetDefaultClient returns an HTTP client with a pre-configured transport

func (*Recorder) IsNewCassette added in v3.0.1

func (rec *Recorder) IsNewCassette() bool

IsNewCassette returns true, if the recorder was started with a new/empty cassette. Returns false, if it was started using an existing cassette, which was loaded.

func (*Recorder) IsRecording added in v3.0.1

func (rec *Recorder) IsRecording() bool

IsRecording returns true, if the recorder is recording interactions, returns false otherwise. Note, that in some modes (e.g. ModeReplayWithNewEpisodes and ModeRecordOnce) the recorder might be recording new interactions. For example in ModeRecordOnce, we are replaying interactions only if there was an existing cassette, and we are recording it, if the cassette is a new one. ModeReplayWithNewEpisodes would replay interactions, if they are present in the cassette, but will also record new ones, if they are not part of the cassette already. In these cases the recorder is considered to be recording for these modes.

func (*Recorder) Mode

func (rec *Recorder) Mode() Mode

Mode returns recorder state

func (*Recorder) RoundTrip

func (rec *Recorder) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface

func (*Recorder) SetMatcher

func (rec *Recorder) SetMatcher(matcher cassette.MatcherFunc)

SetMatcher sets a function to match requests against recorded HTTP interactions.

func (*Recorder) SetRealTransport

func (rec *Recorder) SetRealTransport(t http.RoundTripper)

SetRealTransport can be used to configure the real HTTP transport of the recorder.

func (*Recorder) SetReplayableInteractions

func (rec *Recorder) SetReplayableInteractions(replayable bool)

SetReplayableInteractions defines whether to allow interactions to be replayed or not. This is useful in cases when you need to hit the same endpoint multiple times and want to replay the interaction from the cassette, instead of hiting the endpoint.

func (*Recorder) Stop

func (rec *Recorder) Stop() error

Stop is used to stop the recorder and save any recorded interactions if running in one of the recording modes. When running in ModePassthrough no cassette will be saved on disk.

Jump to

Keyboard shortcuts

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