request-mocha
This was built as a repetitive test utility to request results and assert later on.
Getting Started
Install the module with: npm install request-mocha
// In your test suitevar request = ;var httpUtils = request;;
Documentation
request-mocha
provides a function, requestMocha
, as its module.exports
.
requestMocha(request)
Create a set of utilities bound to a specific version of request
.
This interface is necessary to prevent cross-version conflicts (e.g.
jar
problems)
- request
Request
-request
library to use for utility functions
requestMocha
returns an object which we will refer to as httpUtils
.
httpUtils.save(options)
Make a request to a server via request inside of a mocha before/setup
block.
- options
Object
- Parameters to pass through to request'srequest
function
Results will be saved to mocha's this
context. The same this
context is shared between all mocha before
, beforeEach
, after
, afterEach
, and it
invocations.
- this.err
Error|null
- Error if one occurred while making the request (e.g.ECONNREFUSED
) - this.res
Response
- Response from the server - this.body
String
- Response body from the server (alias forres.body
)
httpUtils._save(options)
Invoke request/save
mechanism without before/setup
wrapper.
The parameters are the same as httpUtils.save
.
The returned value is a function
with a signature of (done)
. When invoked, it will write to this.err
, this.res
, and this.body
as done in httpUtils.save
.
It is expected that you invoke the returned function via a .call
or .apply
to an asynchronous before
context with its callback. This is practical when there is data locked into a this
context that needs to be used for a request
.
// Prepare some `this` data; // In an asynchronous `before` block;
The alternative is to use
var's
outside ofbefore
blocks. Unfortunately, those cannot be re-used while abefore
function can be.
Examples
POST
request inside of tests
Making a httpUtils;
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
License
Copyright (c) 2014 Uber
Licensed under the MIT license.