cassette

package
v4.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: BSD-2-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CassetteFormatVersion is the supported cassette version.
	CassetteFormatVersion = 2
)

Variables

View Source
var (
	// ErrInteractionNotFound indicates that a requested interaction was not
	// found in the cassette file.
	ErrInteractionNotFound = errors.New("requested interaction not found")

	// ErrCassetteNotFound indicates that a requested casette doesn't exist.
	ErrCassetteNotFound = errors.New("requested cassette not found")

	// ErrUnsupportedCassetteFormat is returned when attempting to use an
	// older and potentially unsupported format of a cassette.
	ErrUnsupportedCassetteFormat = fmt.Errorf("unsupported cassette version format")
)
View Source
var DefaultMatcher = NewDefaultMatcher()

DefaultMatcher is the default matcher used to match HTTP requests with recorded interactions

Functions

func TestInteractionReplay

func TestInteractionReplay(t *testing.T, handler http.Handler, interaction *Interaction)

TestInteractionReplay replays an Interaction with the provided Handler and compares the response

func TestServerReplay

func TestServerReplay(t *testing.T, cassetteName string, handler http.Handler)

TestServerReplay loads a Cassette and replays each Interaction with the provided Handler, then compares the response

Types

type Cassette

type Cassette struct {
	sync.Mutex `yaml:"-"`

	// Name of the cassette
	Name string `yaml:"-"`

	// File name of the cassette as written on disk
	File string `yaml:"-"`

	// Cassette format version
	Version int `yaml:"version"`

	// Interactions between client and server
	Interactions []*Interaction `yaml:"interactions"`

	// ReplayableInteractions defines whether to allow
	// interactions to be replayed or not
	ReplayableInteractions bool `yaml:"-"`

	// Matches actual request with interaction requests.
	Matcher MatcherFunc `yaml:"-"`

	// IsNew specifies whether this is a newly created cassette.
	// Returns false, when the cassette was loaded from an
	// existing source, e.g. a file.
	IsNew bool `yaml:"-"`
	// contains filtered or unexported fields
}

Cassette represents a cassette containing recorded interactions.

func Load

func Load(name string) (*Cassette, error)

Load reads a cassette file from disk

func New

func New(name string) *Cassette

New creates a new empty cassette

func (*Cassette) AddInteraction

func (c *Cassette) AddInteraction(i *Interaction)

AddInteraction appends a new interaction to the cassette

func (*Cassette) GetInteraction

func (c *Cassette) GetInteraction(r *http.Request) (*Interaction, error)

GetInteraction retrieves a recorded request/response interaction

func (*Cassette) Save

func (c *Cassette) Save() error

Save writes the cassette data on disk for future re-use

type DefaultMatcherOption

type DefaultMatcherOption func(m *defaultMatcher)

DefaultMatcherOption is a function which configures the default matcher.

func WithIgnoreAuthorization added in v4.0.2

func WithIgnoreAuthorization() DefaultMatcherOption

WithIgnoreAuthorization is a DefaultMatcherOption, which configures the default matcher to ignore matching on the Authorization HTTP header.

func WithIgnoreHeaders added in v4.0.2

func WithIgnoreHeaders(val ...string) DefaultMatcherOption

WithIgnoreHeaders is a DefaultMatcherOption, which configures the default matcher to ignore matching on the defined HTTP headers.

func WithIgnoreUserAgent

func WithIgnoreUserAgent() DefaultMatcherOption

WithIgnoreUserAgent is a DefaultMatcherOption, which configures the default matcher to ignore matching on the User-Agent HTTP header.

type Interaction

type Interaction struct {
	// ID is the id of the interaction
	ID int `yaml:"id"`

	// Request is the recorded request
	Request Request `yaml:"request"`

	// Response is the recorded response
	Response Response `yaml:"response"`

	// DiscardOnSave if set to true will discard the interaction as a whole
	// and it will not be part of the final interactions when saving the
	// cassette on disk.
	DiscardOnSave bool `yaml:"-"`
	// contains filtered or unexported fields
}

Interaction type contains a pair of request/response for a single HTTP interaction between a client and a server.

func (*Interaction) GetHTTPRequest

func (i *Interaction) GetHTTPRequest() (*http.Request, error)

GetHTTPRequest converts the recorded interaction request to http.Request instance.

func (*Interaction) GetHTTPResponse

func (i *Interaction) GetHTTPResponse() (*http.Response, error)

GetHTTPResponse converts the recorded interaction response to http.Response instance.

func (*Interaction) WasReplayed

func (i *Interaction) WasReplayed() bool

WasReplayed returns a boolean indicating whether the given interaction was already replayed.

type MatcherFunc

type MatcherFunc func(*http.Request, Request) bool

MatcherFunc is a predicate, which returns true when the actual request matches an interaction from the cassette.

func NewDefaultMatcher

func NewDefaultMatcher(opts ...DefaultMatcherOption) MatcherFunc

NewDefaultMatcher returns the default matcher.

type ReplayAssertFunc

type ReplayAssertFunc func(t *testing.T, expected *Interaction, actual *httptest.ResponseRecorder)

ReplayAssertFunc is used to assert the results of replaying a recorded request against a handler. It receives the current Interaction and the httptest.ResponseRecorder.

var DefaultReplayAssertFunc ReplayAssertFunc = func(t *testing.T, expected *Interaction, actual *httptest.ResponseRecorder) {
	if expected.Response.Code != actual.Result().StatusCode {
		t.Errorf("status code does not match: expected=%d actual=%d", expected.Response.Code, actual.Result().StatusCode)
	}

	if expected.Response.Body != actual.Body.String() {
		t.Errorf("body does not match: expected=%s actual=%s", expected.Response.Body, actual.Body.String())
	}

	if !headersEqual(expected.Response.Headers, actual.Header()) {
		t.Errorf("header values do not match. expected=%v actual=%v", expected.Response.Headers, actual.Header())
	}
}

DefaultReplayAssertFunc compares the response status code, body, and headers. It can be overridden for more specific tests or to use your preferred assertion libraries

type Request

type Request struct {
	Proto            string      `yaml:"proto"`
	ProtoMajor       int         `yaml:"proto_major"`
	ProtoMinor       int         `yaml:"proto_minor"`
	ContentLength    int64       `yaml:"content_length"`
	TransferEncoding []string    `yaml:"transfer_encoding"`
	Trailer          http.Header `yaml:"trailer"`
	Host             string      `yaml:"host"`
	RemoteAddr       string      `yaml:"remote_addr"`
	RequestURI       string      `yaml:"request_uri"`

	// Body of request
	Body string `yaml:"body"`

	// Form values
	Form url.Values `yaml:"form"`

	// Request headers
	Headers http.Header `yaml:"headers"`

	// Request URL
	URL string `yaml:"url"`

	// Request method
	Method string `yaml:"method"`
}

Request represents a client request as recorded in the cassette file.

type Response

type Response struct {
	Proto            string      `yaml:"proto"`
	ProtoMajor       int         `yaml:"proto_major"`
	ProtoMinor       int         `yaml:"proto_minor"`
	TransferEncoding []string    `yaml:"transfer_encoding"`
	Trailer          http.Header `yaml:"trailer"`
	ContentLength    int64       `yaml:"content_length"`
	Uncompressed     bool        `yaml:"uncompressed"`

	// Body of response
	Body string `yaml:"body"`

	// Response headers
	Headers http.Header `yaml:"headers"`

	// Response status message
	Status string `yaml:"status"`

	// Response status code
	Code int `yaml:"code"`

	// Response duration
	Duration time.Duration `yaml:"duration"`
}

Response represents a server response as recorded in the cassette file.

Jump to

Keyboard shortcuts

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