real-nock
Create stubbed HTTP servers that you can modify on the fly.
Sample usage
Say you have a program that queries a backend system, and multiplies its response by 2. You might want to do some black-box testing, spinning up an actual HTTP server so see how it reacts.
Stub = require 'real-nock' describe 'my program'-> backend = port: 6789 before backendstartdone after backendstopdone beforeEach -> backendreset it 'multiplies the backend response by 2' backendstubget'/value'reply200value: 4 programmultiply valshouldeql 8 done it 'also works for large numbers' backendstubget'/value'reply200value: 10000 programmultiply valshouldeql 20000 done it 'fails gracefully when the backend is slow' backendstubget'/value'delayConnection1000reply'slow' programmultiply errmessageshouldeql 'Failed to call backend' done
Why black-box testing?
In many cases, mocking outbound HTTP calls is a great option. However, sometimes you might need to rely on a real backend server:
- if you want to test the actual HTTP connection
- if you want to write tests that are completely independant of the implementation
- if the program you're testing isn't written in Node
That's great, what type of stubs can I set up?
real-nock
uses nock behind the scenes,
so you should refer to their documentation for all possible operations.
For example:
# reply with hello world after 1 second backendstub get'/' delayConnection1000 reply200'Hello world' # reply with the contents of a file # if the request payload matches backendstub post'/users'name: 'Bob' replyWithFile200__dirname + '/bob.json';
Note that stubs are consumed as soon as they are called. Any subsequent call will be considered an unknown route, and trigger the default behaviour (see below).
This allows you to define two stubs in series, and get the corresponding responses in that order:
backendstubget'/message'reply200'hello'backendstubget'/message'reply200'goodbye'
You can also configure them to apply more than once:
backendstub get'/value' times5 reply200'hello world'
Default behaviour
By default, the HTTP server will ignore any route that wasn't stubbed explicitly,
or where the stub has been consumed. The corresponding request will get ETIMEDOUT
.
You can also configure the following:
# default behaviour (ETIMEDOUT) port: 6789default: 'timeout' # request will get ECONNRESET port: 6789default: 'reset' # request will get a custom status code port: 6789default: 404 # apply a custom (req, res) function port: 6789default: myHandler
This behaviour can be changed at runtime by setting the default
property.
backend.default = 'reset'
Troubleshooting
For debugging, you can log most events on the stub server to stdout
.
backend = port: 9000debug: true
which prints
[localhost:8001] Starting
[localhost:8001] Started
[localhost:8001] GET /users/1 (not stubbed)
[localhost:8001] GET /users/2 (HTTP 200)
[localhost:8001] Stopping
[localhost:8001] Stopped