Netmorphic
Netmorphic is a library for testing networked applications, such as REST clients over HTTP, and database clients over TCP.
The easiest way to get started is to use the netmorphic template application.
table of contents
installation
clone the repo
git clone https://github.com/ql-io/netmorphic.gitcd netmorphic make clean install
use NPM
npm install netmorphic
var netmorphic =
Configurations
Configure your proxy with a json file. See the examples below.
HTTP Configuration
RESTful routing is made available through the use of the Router module.
"/path/to/endpoint": // this is static url for the endpoint "host":"endpoint.host.example.com" // the host of the endpoint server "port":80 // the port of the endpoint "type":"slow" // the handler type to use. See section on handlers below "method":"get" // request method "latency": 100 // latency parameter to use with "slow" handler type "/product/{id}": // this is a dynamic url. The value of id will be found at req.params.id "host":"endpoint.host.example.com" "port":80 "type":"flakey" // another type of handler "method":"get" "hi" : 2000 "lo" : 500 "addresses" : // leave this empty if you don't need to bother with multi-tenancy
TCP configuration
TCP configuration is similar to the above, with two major exceptions. The first is that multi-tenancy is not currently supported, so there is no 'global' key at the top level. The second is that urls are replaced with the the port number that the proxy server will listen on.
"10001": // the port the proxy will listen on "host" : "127.0.0.1" // and proxy incoming streams to this host "port" : 3124 // and this port "type" : "normal" // using this TCP handler "10002": // use this port for a slow proxy "host" : "127.0.0.1" "port" : 3124 "type" : "slow" "latency" : 5000 "10003": // use this port for a drop proxy "host" : "127.0.0.1" "port" : 3124 "type" : "drop" "lo" : 1000 "high" : 5000 "10004": // use this port for a bumpy proxy "host" : "127.0.0.1" "port" : 3124 "type" : "bumpy" "lo" : 3500 "high" : 7000
quick start
HTTP
var netmorphic = http config = USE_CLUSTER = false CUSTOM_HANDLERS = false; var proxy = ; proxyserver
Cluster2
HTTP withvar netmorphic = http monitor = monitor Cluster = config = USE_CLUSTER = true CUSTOM_HANDLERS = false; var proxy = ; var cluster = port: 8000 monitor: cluster
TCP
var TCProxy = tcp config = CUSTOM_HANDLERS = false USE_CLUSTER = false; // returns an array of serversvar servers = //iterate over the TCP servers and start each oneservers
Cluster2
TCP withvar TCProxy = tcp monitor = monitor config = Cluster = CUSTOM_HANDLERS = false USE_CLUSTER = true; var servers = var cluster = monitor: cluster
handlers
Handlers are are the "morph" in netmorphic. They act upon your requests and streams. Parameters for your handlers are set in the config files. Netmorphic ships with a few handlers out of the box, namely:
- normal - plain proxy
- slow - buffers the outgoing request by a minimum latency
- flakey - buffers the request for a random latency between hi and lo values
- drop - drops request immediately or
- unresponsive - does not respond to the request
Additionally, custom handlers can be written to do anything. Pass an object of handler functions to the netmorphic constructor, like so:
var TCProxy = tcp config = CUSTOM_HANDLERS = USE_CLUSTER = true; var servers =
a custom HTTP handler file would look like this:
// hint: it's just a function that handles the request and response streams... moduleexports'just proxy' = { var config = reqserConfig; // the service config for this particular client var proxy = reqproxy; // a proxy to use, if you need a proxy proxy;}
For TCP, it looks like this:
var ps = ; // a stream that pauses moduleexports'vanilla tcp proxy' = { // socket is the client stream // service is a TCP socket to use to proxy to the endpoint var buffer = ; // create a pause stream socket; // pipe the request to the buffer and pause it service; service; // pipe the endpoint connection back to the client connection}