A simple testing tool for mocking HTTP sequences of request / response pairs in node.js
Installing npm (node package manager)
$ curl | sh
$ [sudo] npm install mock-request
mock-request library is designed to easily mock HTTP endpoints in tests which fit the following boilerplate:
- Tests are performed by making HTTP requests against an API server
- Assertions are made against the HTTP response (status, headers, body, etc)
- Rinse. Repeat.
The method signature used by the
mock-request library matches that of the popular request library widely used in the node.js community. Besides that method signature, there are no external test framework dependencies so use whatever your preference is: vows, expresso, nodeunit, etc.
If you're curious why mocking your HTTP requests could be helpful you can read up here.
mock-request library is designed for explicit mocking, it does not perform any interpolation or guessing beyond assuming a default response of
200 unless otherwise indicated. Here's a sample of how to use
var mockRequest =assert = ;var mockFn = mockRequest;//// The mock function returned from `mockRequest.mock()` is// synchronous because no HTTP actually takes place.//;//// Now that we've made the first `GET` request, we have to make the// `POST` request or `mock-request` will throw an `Error`//;
- Get feedback on what else could be exposed through this library.
- Improve it.
- Repeat (1) + (2).