mockingjay-server: Index | Examples | Files

package mockingjay

import ""

Package mockingjay allows you to create a HTTP server to return canned responses for certain requests. These operations are configured via YAML. The aim of this is to let you easily create fake servers for testing purposes.



Package Files

compatibility.go fakeendpoint.go generator_utils.go mj_decoder.go mockingjay.go regexField.go request.go response.go server.go validationHelpers.go yamlHelper.go


const DefaultHTTPTimeoutSeconds = 5

DefaultHTTPTimeoutSeconds is the default http timeout for compatability checks

type CompatibilityChecker Uses

type CompatibilityChecker struct {
    // contains filtered or unexported fields

CompatibilityChecker is responsible for checking endpoints are compatible

func NewCompatabilityChecker Uses

func NewCompatabilityChecker(logger *log.Logger, httpTimeout time.Duration) *CompatibilityChecker

NewCompatabilityChecker creates a new CompatabilityChecker. The httpTimeout refers to the http timeout when making requests to the real server

func (*CompatibilityChecker) CheckCompatibility Uses

func (c *CompatibilityChecker) CheckCompatibility(endpoints []FakeEndpoint, realURL string) bool

CheckCompatibility checks the endpoints against a "real" URL

type FakeEndpoint Uses

type FakeEndpoint struct {
    Name        string //A description of what this endpoint is.
    CDCDisabled bool   // When set to true it will not be included in the consumer driven contract tests against real server
    Request     Request
    Response    response

FakeEndpoint represents the information required to listen to a particular request and respond to it

func NewFakeEndpoints Uses

func NewFakeEndpoints(data io.ReadCloser) (endpoints []FakeEndpoint, err error)

NewFakeEndpoints returns an array of Endpoints from a YAML byte array. Returns an error if YAML cannot be parsed or there are validation concerns

func NewFakeEndpointsFromJSON Uses

func NewFakeEndpointsFromJSON(data io.ReadCloser) (endpoints []FakeEndpoint, err error)

NewFakeEndpointsFromJSON returns an array of Endpoints from a JSON byte array. Returns an error if JSON cannot be parsed or there are validation concerns

func (FakeEndpoint) Generate Uses

func (f FakeEndpoint) Generate(rand *rand.Rand, size int) reflect.Value

Generate creates a random endpoint typically used for testing

func (*FakeEndpoint) String Uses

func (f *FakeEndpoint) String() string

type MJDecoder Uses

type MJDecoder interface {
    Decode(interface{}) error

type MJDecoderFunc Uses

type MJDecoderFunc func(interface{}) error

func (MJDecoderFunc) Decode Uses

func (m MJDecoderFunc) Decode(target interface{}) error

type RegexField Uses

type RegexField struct {

RegexField allows you to work with regex fields in YAML

func (*RegexField) MarshalJSON Uses

func (r *RegexField) MarshalJSON() ([]byte, error)

MarshalJSON returns a string for the regex

func (*RegexField) MarshalYAML Uses

func (r *RegexField) MarshalYAML() (interface{}, error)

MarshalYAML returns the string of the regex

func (*RegexField) UnmarshalJSON Uses

func (r *RegexField) UnmarshalJSON(data []byte) error

UnmarshalJSON will unhmarshal a JSON field into regexp

func (*RegexField) UnmarshalYAML Uses

func (r *RegexField) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML will unhmarshal a YAML field into regexp

type Request Uses

type Request struct {
    URI      string            `yaml:"uri"`
    RegexURI *RegexField       `yaml:"regexuri,omitempty" json:"RegexURI,omitempty"`
    Method   string            `yaml:"method"`
    Headers  map[string]string `yaml:"headers,omitempty"`
    Body     string            `yaml:"body,omitempty"`
    Form     map[string]string `yaml:"form,omitempty"`

Request is a simplified version of a http.Request

func NewRequest Uses

func NewRequest(httpRequest *http.Request) (req Request)

NewRequest creates a mockingjay request from a http request

func (Request) AsCURL Uses

func (r Request) AsCURL(baseURL string) (string, error)

AsCURL returns a string which is the curl command to match the reqquest

func (Request) AsHTTPRequest Uses

func (r Request) AsHTTPRequest(baseURL string) (req *http.Request, err error)

AsHTTPRequest tries to create a http.Request from a given baseURL

func (Request) Hash Uses

func (r Request) Hash() string

func (Request) String Uses

func (r Request) String() string

type Server Uses

type Server struct {
    Endpoints []FakeEndpoint
    // contains filtered or unexported fields

Server allows you to configure a HTTP server for a slice of fake endpoints

func NewServer Uses

func NewServer(endpoints []FakeEndpoint, debugMode bool, newConfigStateWriter io.Writer) *Server

NewServer creates a new Server instance. debugMode will log additional info at runtime and newConfigStateWriter will write out the new state of the config if it gets changed at runtime

ExampleNewServer is an example as to how to make a fake server. The mockingjay server implements what is needed to mount it as a standard web server.


// Create a fake server from YAML
testYAML := `
 - name: Test endpoint
 uri: /hello
 method: GET
   content-type: application/json
 body: foobar
 code: 200
 body: hello, world
   content-type: text/plain

 - name: Test endpoint 2
 uri: /world
 method: DELETE
 code: 200
 body: hello, world

 - name: Failing endpoint
 uri: /card
 method: POST
 body: Greetings
 code: 500
 body: Oh bugger

 - name: Endpoint not used for CDC
   cdcdisabled: true
 uri: /burp
 method: POST
 body: Belch
 code: 500
 body: Oh no

endpoints, _ := NewFakeEndpoints(yamlToReadCloser(testYAML))
server := NewServer(endpoints, false, ioutil.Discard)

// Mount it just like any other server
http.Handle("/", server)
http.ListenAndServe(":9090", nil)

func (*Server) ServeHTTP Uses

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Package mockingjay imports 24 packages (graph) and is imported by 4 packages. Updated 2021-01-18. Refresh now. Tools for package owners.