mockingjay

package
v0.0.0-...-1dcaa55 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2016 License: MIT Imports: 11 Imported by: 1

README

mockingjay

GoDoc

Create a server from configuration. Can be useful for:

  • Integration tests
  • Consumer driven contracts
  • Performance tests when combined with monkey

Example

package main

import (
	"github.com/quii/mockingjay-server/mockingjay"
	"log"
	"net/http"
)

func main() {
	testYAML := `
---
 - name: Test endpoint
   request:
     uri: /hello
     method: GET
   response:
     code: 200
     body: hello, world
     headers:
       content-type: text/plain

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

 - name: Failing endpoint
   request:
     uri: /card
     method: POST
     body: Greetings
   response:
     code: 500
     body: Oh bugger
 `
	endpoints, err := mockingjay.NewFakeEndpoints([]byte(testYAML))

	if err != nil {
		log.Fatal(err)
	}

	server := mockingjay.NewServer(endpoints)

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

Building

  • Requires Go 1.3+
  • godeps

## Todo

  • Although it supports request/response headers, it only supports one value per header (http allows you to set multiple values)
  • Tests for stuff inside request.go

Documentation

Overview

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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FakeEndpoint

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

func NewFakeEndpoints(data []byte) (endpoints []FakeEndpoint, err error)

NewFakeEndpoints returns an array of Endpoints from a YAML byte array. Returns an error if YAML cannot be parsed

func (*FakeEndpoint) String

func (f *FakeEndpoint) String() string

type RegexYAML

type RegexYAML struct {
	regexp.Regexp
}

RegexYAML allows you to work with regex fields in YAML

func (*RegexYAML) MarshalJSON

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

MarshalJSON returns a string for the regex

func (*RegexYAML) UnmarshalYAML

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

UnmarshalYAML will unhmarshal a YAML field into regexp

type Request

type Request struct {
	URI      string
	RegexURI *RegexYAML
	Method   string
	Headers  map[string]string
	Body     string
}

Request is a simplified version of a http.Request

func NewRequest

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

NewRequest creates a mockingjay request from a http request

func (Request) AsHTTPRequest

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

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

func (Request) String

func (r Request) String() string

type Server

type Server struct {
	// contains filtered or unexported fields
}

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

func NewServer

func NewServer(endpoints []FakeEndpoint) *Server

NewServer creates a new Server instance

Example

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
   request:
     uri: /hello
     method: GET
     headers:
       content-type: application/json
     body: foobar
   response:
     code: 200
     body: hello, world
     headers:
       content-type: text/plain

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

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

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

endpoints, _ := NewFakeEndpoints([]byte(testYAML))
server := NewServer(endpoints)

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

func (*Server) ServeHTTP

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

Jump to

Keyboard shortcuts

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