Request utilities for Mocha
This was built as a repetitive test utility to request results and assert later on.
Install the module with:
npm install request-mocha
// In your test suitevar request = require'request';var httpUtils = require'request-mocha'request;describe'A server receiving a request'beforestartServer;// Make request and save results to `this.err`, `this.res`, and `this.body`httpUtilssave'';// Assert against mocha's `this` contextit'responded with "Hello World!"'expectthiserrtoequalnull;expectthisresstatusCodetoequal200;expectthisbodytoequal'Hello World!';;;
request-mocha provides a function,
requestMocha, as its
Create a set of utilities bound to a specific version of
This interface is necessary to prevent cross-version conflicts (e.g.
requestlibrary to use for utility functions
requestMocha returns an object which we will refer to as
Object- Parameters to pass through to request's
Results will be saved to mocha's
this context. The same
this context is shared between all mocha
Error|null- Error if one occurred while making the request (e.g.
Response- Response from the server
String- Response body from the server (alias for
request/save mechanism without
The parameters are the same as
The returned value is a
function with a signature of
(done). When invoked, it will write to
this.body as done in
It is expected that you invoke the returned function via a
.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
// Prepare some `this` databeforethiscredentials =username: 'todd'password: 'keyboardcat';;// In an asynchronous `before` blockbefore// Prepare the save callhttpUtils_savemethod: 'POST'url: ''form: thiscredentials// Invoke on the current context with the current callbackcallthis done;;
The alternative is to use
beforeblocks. Unfortunately, those cannot be re-used while a
beforefunction can be.
httpUtilssavemethod: 'POST'url: ''form:my: 'data';
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
Copyright (c) 2014 Uber
Licensed under the MIT license.