HTTP Mocker
httpmocker
is a testing tool for mocking any requests that you are not able to access in your test file.
Features
- don't change any source codes from your lib directory
- configurable (easy to filter requests)
Why?
Sometimes, you may use these third-party modules like node-aws
, node-semantics3
those control how the program call requests over HTTP/HTTPS, so if you wanna accurately filter these requests, it's hacky and ugly.
Imagine the following aws codes:
// create the AWS.Request objectvar request = ; // register a callback to report on the datarequest; // send the requestrequest;
if you wanna filter your program in your testing progress, you have to add 2 lines in your source code:
if processenvNODE_ENV === 'test' // return or callback
once there are the certain amount number of third-party function calls in your source, you then need to write the corresponding number of above blocks, that's aweful.
httpmocker
Let's be graceful from In your test file:
var configmock = config;
then when you do request:
request;
it will return:
{ // res.statusCode = 200; // res.headers['Content-Type'] = 'application/json'; // res.body = {foo: 'bar'};}
API
httpmocker
exports one function httpmocker.config(config)
, it provides a way to define responses which you are going to expect to mock.
The config
is an object that maps the router, Object.keys(config)
should return urls(prefix) that you wanna mock for testing. And every url prefix should hold a object that like:
"error": "...your error if you wanna throw" "statusCode": 400 // or status "headers": "head1_key": "head1_value" "body": "string/buffer/object, will write to readable instance"
httpmocker
exports 2nd function httpmocker.clear([url])
, it provides a way to clear config that you set, it receives one optional argument, if provided, will remove config by url
that you would pass, if not, will remove all configurations.
NOTE: please use httpmocker
with NODE_ENV=test
Installation
Recommended command to install
$ npm install httpmocker --save-dev
License
MIT. Copyright (c) Yorkie Liu