v1

package
Version: v0.0.0-...-8b41792 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: MIT Imports: 9 Imported by: 0

README

V1.0.0

Syntax guide for V1.0.0 ersatz fixtures configuration.

Complete Syntax

  • version: Must be 1.0.0.
  • fixtures: A map (key: endpoint path, value: Resource object), which contains the fixtures you wish to configure.
Resource Object

A map (key: HTTP Method, value: Response Object). Accepted HTTP Methods are get | put | post | delete. You must not specify the same HTTP Method twice, or the second will be overwritten.

Response Object
  • Required status: The http status code to return in response.
  • headers: Headers to return in the response. If Content-Type is set, this will dictate the format of the body. Supported content types are application/json | text/plain | application/x-yaml
  • body: Polymorphic property, which supports values either of type string (should be used for text/plain responses) or of type Object, which will be serialised by default to JSON.
  • expectations: Polymorphic property which supports passing either a single expectation, or multiple expectations in an array. Expectations check the request for provided header or queryParam values. If multiple expectations are provided, at least one set of expectations must pass for ersatz to proceed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(expected string, actual ...string) bool

Contains compares the expected values to the actual

func MockPaths

func MockPaths(r Router, paths *Fixtures)

MockPaths adds endpoints to the provided router as per the ersatz-fixtures.yml

Types

type Expectation

type Expectation struct {
	Headers     ExpectedHeaders     `json:"headers"`
	QueryParams ExpectedQueryParams `json:"queryParams"`
}

Expectation contains expectations for the endpoint

func NewExpectation

func NewExpectation() Expectation

NewExpectation returns a setup Expectation used for test cases.

func (*Expectation) Validate

func (e *Expectation) Validate(r *http.Request) bool

Validate validates the query params and headers if they exist

type Expectations

type Expectations []Expectation

func (Expectations) AtLeastOneExpectationPasses

func (e Expectations) AtLeastOneExpectationPasses(r *http.Request) bool

AtLeastOneExpectationPasses verifies that for multiple expectations at least one passes successfully, allowing the simulation to proceed

func (*Expectations) UnmarshalJSON

func (e *Expectations) UnmarshalJSON(d []byte) error

UnmarshalJSON allows expectations to be declared either as an array or as a singular expectation

type ExpectedHeaders

type ExpectedHeaders struct {
	textproto.MIMEHeader
}

ExpectedHeaders does what it says on the tin

func (*ExpectedHeaders) UnmarshalJSON

func (e *ExpectedHeaders) UnmarshalJSON(d []byte) error

UnmarshalJSON supports non-array declaration of headers

func (ExpectedHeaders) Validate

func (e ExpectedHeaders) Validate(actual http.Header) error

Validate validates the expected headers against the received headers

type ExpectedQueryParams

type ExpectedQueryParams struct {
	url.Values
}

ExpectedQueryParams does what it says on the tin

func (*ExpectedQueryParams) UnmarshalJSON

func (e *ExpectedQueryParams) UnmarshalJSON(d []byte) error

UnmarshalJSON supports non-array declaration of headers

func (ExpectedQueryParams) Validate

func (e ExpectedQueryParams) Validate(actual url.Values) error

Validate validates the expected query params vs the actual received params for the request

type Fixtures

type Fixtures map[string]Path

Fixtures is the top level object with which endpoints are configured

func (Fixtures) Version

func (v Fixtures) Version() int

Version returns the fixtures version number represented by this package

type Path

type Path map[string]Resource

Path maps absolute paths to http resources

type Resource

type Resource struct {
	Status       int               `json:"status"`
	Headers      map[string]string `json:"headers"`
	Body         interface{}       `json:"body"`
	Expectations Expectations      `json:"expectations"`
}

Resource mocks a particular http method for a given path

type Router

type Router interface {
	Get(path string, handler http.HandlerFunc, middleware ...vestigo.Middleware)
	Put(path string, handler http.HandlerFunc, middleware ...vestigo.Middleware)
	Post(path string, handler http.HandlerFunc, middleware ...vestigo.Middleware)
	Delete(path string, handler http.HandlerFunc, middleware ...vestigo.Middleware)
}

Router allows us to test that paths are configured properly

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL