core

package
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package core is the hearth of Mocha. It implements request and response mocking and matching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expectation

type Expectation struct {
	// Target is an optional metadata that describes the target of the matcher.
	// Example: the target could have the "header", meaning that the matcher will be applied to one request header.
	Target string

	// Matcher associated with this Expectation.
	Matcher expect.Matcher

	// ValueSelector will extract the http.Request or a portion of it and feed it to the associated Matcher.
	ValueSelector expect.ValueSelector

	// Weight of this Expectation.
	Weight Weight
}

Expectation holds metadata related to one http.Request Matcher.

type FindResult

type FindResult struct {
	Matches         bool
	Matched         *Mock
	ClosestMatch    *Mock
	MismatchDetails []MismatchDetail
}

FindResult holds the results for an attempt to match a mock to a request.

func FindMockForRequest

func FindMockForRequest(storage Storage, params expect.Args) (*FindResult, error)

FindMockForRequest tries to find a mock to the incoming HTTP request. It runs all matchers of all eligible mocks on request until it finds one that matches every one of then. It returns a FindResult with the find result, along with a possible closest match.

type MatchResult

type MatchResult struct {
	// MismatchDetails is the list of non matches messages.
	MismatchDetails []MismatchDetail

	// Weight for the Matcher. It helps determine the closest match.
	Weight int

	// IsMatch indicates whether it matched or not.
	IsMatch bool
}

MatchResult holds information related to a matching operation.

type MismatchDetail

type MismatchDetail struct {
	Name        string
	Target      string
	Description string
}

type Mock

type Mock struct {
	// ID is unique identifier for a Mock
	ID int

	// Name is an optional metadata. It helps to find and debug mocks.
	Name string

	// Priority sets the priority for a Mock.
	Priority int

	// Expectations are a list of Expectation. These will run on every request to find the correct Mock.
	Expectations []Expectation

	// PostExpectations are a list of Expectation. They will be executed after the request was matched to a Mock.
	// This allows stateful matchers whose state data should not be evaluated every match attempt.
	PostExpectations []Expectation

	// Reply is the responder that will be used to serve the HTTP response stub, once matched against an
	// HTTP request.
	Reply Reply

	// Hits holds the amount of time this Mock was called and served.
	Hits int

	// Enabled indicates if the Mock is enabled or disabled. Only enabled mocks are matched.
	Enabled bool

	// PostActions holds PostAction list to be executed after the Mock was matched and served.
	PostActions []PostAction
	// contains filtered or unexported fields
}

Mock holds metadata and expectations to be matched against HTTP requests in order to serve mocked responses. This is core entity of this project, mostly features works based on it.

func NewMock

func NewMock() *Mock

NewMock returns a new Mock with default values set.

func (*Mock) Called

func (m *Mock) Called() bool

Called checks if the Mock was called at least once.

func (*Mock) Dec

func (m *Mock) Dec()

Dec reduce one Mock call.

func (*Mock) Disable

func (m *Mock) Disable()

Disable disables the Mock. The Mock will not be eligible to be matched.

func (*Mock) Enable

func (m *Mock) Enable()

Enable enables the Mock. The Mock will be eligible to be matched.

func (*Mock) Hit

func (m *Mock) Hit()

Hit notify that the Mock was called.

func (*Mock) Matches

func (m *Mock) Matches(params expect.Args, expectations []Expectation) (MatchResult, error)

Matches checks if current Mock matches against a list of expectations. Will iterate through all expectations even if it doesn't match early.

type PostAction

type PostAction interface {
	// Run runs the PostAction implementation.
	Run(args PostActionArgs) error
}

PostAction defines the contract for an action that will be executed after serving a mocked HTTP response.

type PostActionArgs

type PostActionArgs struct {
	Request  *http.Request
	Response *Response
	Mock     *Mock
	Params   parameters.Params
}

PostActionArgs represents the arguments that will be passed to every PostAction implementation

type Reply

type Reply interface {
	// Build returns a Response stub to be served.
	Build(*http.Request, *Mock, parameters.Params) (*Response, error)
}

Reply defines the contract to configure an HTTP responder.

type Response

type Response struct {
	Status  int
	Header  http.Header
	Cookies []*http.Cookie
	Body    io.Reader
	Delay   time.Duration
	Mappers []ResponseMapper
}

Response defines the HTTP response that will be served once a Mock is matched for an HTTP Request.

type ResponseMapper

type ResponseMapper func(res *Response, args ResponseMapperArgs) error

ResponseMapper is the function definition to be used to map Mock Response before serving it.

type ResponseMapperArgs

type ResponseMapperArgs struct {
	Request    *http.Request
	Parameters parameters.Params
}

ResponseMapperArgs represents the expected arguments for every ResponseMapper.

type Storage

type Storage interface {
	// Save saves the Mock.
	Save(mock *Mock)

	// FetchEligible returns mocks that can be matched against requests.
	FetchEligible() []*Mock

	// FetchAll returns all stored Mock instances.
	FetchAll() []*Mock

	// Delete removes a Mock by its ID.
	Delete(id int)

	// Flush removes all stored mocks.
	Flush()
}

Storage is the definition for Mock repository.

func NewStorage

func NewStorage() Storage

NewStorage returns Mock storage implementation.

type T

type T interface {
	Helper()
	Logf(string, ...any)
	Errorf(string, ...any)
	FailNow()
}

T is based on testing.T and allow mocha components to log information and errors.

type Weight

type Weight int

Weight helps to detect the closest mock match.

const (
	WeightNone Weight = iota
	WeightVeryLow
	WeightLow
	WeightRegular
	WeightHigh
)

Enums of Weight.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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