Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
node-mocks-http-self
Advanced tools
Mock 'http' objects for testing Express
routing functions, but could be used for testing any
Node.js web server applications that have
code that requires mockups of the request
and response
objects.
This project is available as a NPM package.
$ npm install --save-dev node-mocks-http
Our example includes
--save-dev
based on the assumption that node-mocks-http will be used as a development dependency..
After installing the package include the following in your test files:
var httpMocks = require('node-mocks-http');
Suppose you have the following Express route:
app.get('/user/:id', routeHandler);
And you have created a function to handle that route's call:
var routeHandler = function( request, response ) { ... };
You can easily test the routeHandler
function with some code like
this using the testing framework of your choice:
exports['routeHandler - Simple testing'] = function(test) {
var request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});
var response = httpMocks.createResponse();
routeHandler(request, response);
var data = JSON.parse( response._getData() );
test.equal("Bob Dog", data.name);
test.equal(42, data.age);
test.equal("bob@dog.com", data.email);
test.equal(200, response.statusCode );
test.ok( response._isEndCalled());
test.ok( response._isJSON());
test.ok( response._isUTF8());
test.done();
};
httpMocks.createRequest(options)
Where options is an object hash with any of the following values:
option | description | default value |
---|---|---|
method | request HTTP method | 'GET' |
url | request URL | '' |
originalUrl | request original URL | url |
baseUrl | request base URL | url |
path | request path | '' |
params | object hash with params | {} |
session | object hash with session values | undefined |
cookies | object hash with request cookies | {} |
signedCookies | object hash with signed cookies | undefined |
headers | object hash with request headers | {} |
body | object hash with body | {} |
query | object hash with query values | {} |
files | object hash with values | {} |
httpMocks.createResponse(options)
Where options is an object hash with any of the following values:
option | description | default value |
---|---|---|
eventEmitter | event emitter used by response object | mockEventEmitter |
writableStream | writable stream used by response object | mockWritableStream |
req | Request object being responded to | null |
NOTE: The out-of-the-box mock event emitter included with
node-mocks-http
is not a functional event emitter and as such does not actually emit events. If you wish to test your event handlers you will need to bring your own event emitter.
Here's an example:
var httpMocks = require('node-mocks-http');
var res = httpMocks.createResponse({
eventEmitter: require('events').EventEmitter
});
// ...
it('should do something', function(done) {
res.on('end', function() {
assert.equal(...);
done();
});
});
// ...
httpMocks.createMocks(reqOptions, resOptions)
Merges createRequest
and createResponse
. Passes given options object to each
constructor. Returns an object with properties req
and res
.
We wanted some simple mocks without a large framework.
We also wanted the mocks to act like the original framework being mocked, but allow for setting of values before calling and inspecting of values after calling.
We are looking for more volunteers to bring value to this project, including the creation of more objects from the HTTP module.
This project doesn't address all features that must be mocked, but it is a good start. Feel free to send pull requests, and a member of the team will be timely in merging them.
If you wish to contribute please read our Contributing Guidelines.
Most releases fix bugs with our mocks or add features similar to the
actual Request
and Response
objects offered by Node.js and extended
by Express.
See the Release History for details.
Licensed under MIT.
FAQs
Mock 'http' objects for testing Express routing functions
We found that node-mocks-http-self demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.