This tool provides a simple framework for generating performance test-ready mocks from configuration files.
The tool can be built and run entirely via docker using the following command.
$> docker build -t ncatelli/mockserver .
The tool can also be built and installed locally by running a pip install from the root of the project.
$> make build
Tests can be run using the built in go testing library and a convenient wrapper to test all subpackages has been provided below.
Local tests default to running tests on all subpackages along with coverage tests. Tests can be run with the following make command.
$> make test
The mockserver service can be configured via the following environment variables:
string The server address mockserver binds to.string A filesystem path to the simple driver config file.url.URL A URL path to fetch the configuration file from. This
is useful for when a service wants to publish its own configuration file.It's worth noting that EITHER CONFIG_PATH or CONFIG_URL should be sent. If both are set, CONFIG_PATH takes priority.
All response bodies in for handlers are valid go templates. In addition some helper data is included in each template variable to be referenced for rendering. This includes the following:
Template parameters are passed directly into the template via the data argument at time of execution and include both the http.Request object for the request that generated the template as well as any path variables that are parsed from the request URL.
The mockserver allow for the parsing of variables directly out of a url path through the gorilla/mux router and more information on what kind of pattern matching can be accomplished by the router can be found at the preceeding link.
Mocking functionality is implatemented via golang's stdlib template functions. A few additional libraries and features have been included to aid in extending this functionality.
GTF is a template function library with the stated goal of implementing the functions included in jinja2. Further documentation on the functions included can be found at their github page.
Custom generators provides a simple way to add new functions that will be compiled into the mockserver at build time.
New generators can be added by:
Generator type.Generator MUST satisfy the interface github.com/ncatelli/mockserver/pkg/router/generator.Generatormake to generate the correct package imports and build mockserver with the new plugin.The yaml driver implements a simple configuration format that maps directly to the implementation of the Route struct.
Required
This field represents a url path to be passed to the router and supports all gorilla path matching and variables. All variables specified in the path are passed back to the handlers via path variables.
Required The HTTP that this route will match against. This field currently only matches 1 method.
This field takes a map of logging drivers and a map of strings to be passed in for configuring the middlewares. Further information on the available middleware and their configuration parameters and their settings can be found in the middlewares section.
This field represents a key-value mapping of headers that must be defined to be routeable to the defined route.
This field represents a key-value mapping of query parameters that must be set to be routable to the defined route.
The handlers field takes a weighted list of objects that map directly to the Handler structure. Subfields of handlers represent
---
- path: "/test/pathvar/{embed}"
method: GET
middleware:
logging:
target: stdout
handlers:
- weight: 1
response_headers:
content-type: text/plain
static_response: '{{ .PathVars.embed }}'
response_status: 200
- path: "/test/weighted"
method: GET
handlers:
- weight: 2
response_headers:
content-type: application/json
response_path: /examples/example_response_body.txt
response_status: 200
- weight: 1
response_headers:
content-type: text/plain
static_response: ''
response_status: 500
- path: "/test/with/required/headers"
method: GET
request_headers:
status: ok
handlers:
- weight: 1
response_headers:
content-type: text/plain
static_response: 'ok'
response_status: 200
- path: "/test/with/required/query/params"
method: GET
query_params:
status: ok
handlers:
- weight: 1
response_headers:
content-type: text/plain
static_response: 'ok'
response_status: 200
The logging handler implements the gorilla logging handler and outputs logs to a target in Apache CLF format.
target (default: stdout): a target to write files to. Currently this only supports stdout.
The latency middleware allows injection of artificial latency into a route to mimic either transit or processing time. This latency can be specified either as a static value or as a range of time.
latency (default: 0): A static latency in milliseconds to inject into a response.
min (default: 0): A minimum value for a range of latency in a response.
max (default: 0): A maximum value for a range of latency in a response.
Content type
Image
Digest
Size
14.2 MB
Last updated
over 6 years ago
docker pull ncatelli/mockserver