monkey

package
v0.0.0-...-7b93ee8 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 7 Imported by: 1

README

monkey

GoDoc

Adds bad behaviour to a http.Handler from a YAML config. Useful for simulating those troublesome integration points

---
# Writes a different body 50% of the time
- body: "This is wrong :( "
  frequency: 0.5

# Delays initial writing of response by a second 20% of the time
- delay: 1000
  frequency: 0.2

# Returns a 404 30% of the time
- status: 404
  frequency: 0.3

# Write 10,000,000 garbage bytes 10% of the time
- garbage: 10000000
  frequency: 0.09

Documentation

Overview

Package monkey lets you wrap bad behaviour around your http.Handlers, such as request delays, incorrect responses, bodies and returning garbage from configuration. By using monkey you can simulate the unpredictable nature of calling services over HTTP.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(server http.Handler, configPath string) (http.Handler, error)

NewServer creates a http.Handler which wraps it's monkey business around it, to return a new http.Handler. If no behaviours are defined in the config it will return the original handler, otherwise an error

Example

ExampleNewServer is an example as to how use monkey to wrap around a http.Handler and change it's behaviour

// Create a fake server from YAML
testYAML := `
---
# Writes a different body 50% of the time
- body: "This is wrong :( "
  frequency: 0.5

# Delays initial writing of response by a second 20% of the time
- delay: 1000
  frequency: 0.2

# Returns a 404 30% of the time
- status: 404
  frequency: 0.3

# Write 10,000,000 garbage bytes 10% of the time
- garbage: 10000000
  frequency: 0.09
 `

// your server you want to monkey with
var server http.Handler

// monkey will create you a new server from your YAML
server, err := NewServerFromYAML(server, []byte(testYAML))

// err will be returned if the config is bad
if err != nil {
	log.Fatal(err)
}

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

func NewServerFromYAML

func NewServerFromYAML(server http.Handler, YAML []byte) (http.Handler, error)

NewServerFromYAML creates a http.Handler which wraps monkey business defined from YAML around it, to return a new http.Handler. If the YAML is invalid, it will return an error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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