v2

package
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: 8 Imported by: 0

README

V2.0.0

Syntax guide for V2.0.0 ersatz fixtures configuration.

Complete Syntax

  • version: Must be 2.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: Either Response Object or Request Discriminator Object. Accepted HTTP Methods are get | put | post | delete. You must not specify the same HTTP Method twice, or the second will be overwritten.

Values may either be a single Response object, or many Request Discriminator objects. Request Discriminators are declared in an array, and allow you to specify different responses for different requests (discriminated by request properties other than the Path).

Discriminators are matched in order; if many discriminators match the same request, the first will be used.

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.
Request Discriminator Object
  • Required when: Contains either headers or queryParams which are used to identify which response to use for the request.
    • headers: A map (key: string, value: string) of headers to look for the in the request.
    • queryParams: A map (key: string, value: string) of query parameters to look for the in the request.
  • Required response: A Response Object which will be used if the request matches the headers and query parameters specified.

Additionally, values included in the when statement can take the following formats:

  • ${exists}: Specifies that any value is acceptable for the header or query parameter, but it must be present.
  • ${missing}: Specifies that the value must not be present in the request.

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 Exists

func Exists(value string) bool

func Missing

func Missing(value string) bool

func MockPaths

func MockPaths(r Router, paths *Fixtures)

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

Types

type Discriminator

type Discriminator struct {
	When     RequestDiscriminator `json:"when"`
	Response Response             `json:"response"`
}

type Discriminators

type Discriminators []Discriminator

func (Discriminators) AtLeastOneDiscriminatorIsSatisfied

func (arr Discriminators) AtLeastOneDiscriminatorIsSatisfied(req *http.Request) bool

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 Headers

type Headers struct {
	textproto.MIMEHeader
	TemplatedValues TemplatedValues
}

Headers does what it says on the tin

func (*Headers) UnmarshalJSON

func (h *Headers) UnmarshalJSON(d []byte) error

UnmarshalJSON creates a textproto.MIMEHeader compliant struct using provided map values

func (Headers) Validate

func (h Headers) Validate(actual http.Header) bool

Validate validates the expected headers against the received headers

type Path

type Path map[string]Resource

Path maps absolute paths to http resources

type QueryParams

type QueryParams struct {
	url.Values
	TemplatedValues TemplatedValues
}

QueryParams does what it says on the tin

func (*QueryParams) UnmarshalJSON

func (q *QueryParams) UnmarshalJSON(d []byte) error

UnmarshalJSON creates a url.Values compliant struct using the provided map values

func (QueryParams) Validate

func (q QueryParams) Validate(actual url.Values) bool

type RequestDiscriminator

type RequestDiscriminator struct {
	Headers     Headers     `json:"headers"`
	QueryParams QueryParams `json:"queryParams"`
}

func NewRequestDiscriminator

func NewRequestDiscriminator() RequestDiscriminator

func (RequestDiscriminator) SatisfiesDiscriminator

func (r RequestDiscriminator) SatisfiesDiscriminator(req *http.Request) bool

type Resource

type Resource struct {
	Discriminators Discriminators
	Response       Response
}

func (*Resource) UnmarshalJSON

func (r *Resource) UnmarshalJSON(d []byte) error

type Response

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

Response 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

type TemplatedFunction

type TemplatedFunction func(string) bool

type TemplatedValues

type TemplatedValues map[string]TemplatedFunction

TemplatedValues holds "special" values which can be used for fuzzy discriminators - i.e. ${exists} checks for the existence of the header

func ParseRequestValues

func ParseRequestValues(rawValues map[string]string) (TemplatedValues, map[string]string)

Jump to

Keyboard shortcuts

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