Gathering detailed insights and metrics for node-mocks-http-self
Gathering detailed insights and metrics for node-mocks-http-self
Gathering detailed insights and metrics for node-mocks-http-self
Gathering detailed insights and metrics for node-mocks-http-self
npm install node-mocks-http-self
Typescript
Module System
Min. Node Version
Node Version
NPM Version
70.7
Supply Chain
98.1
Quality
74.1
Maintenance
100
Vulnerability
100
License
JavaScript (73.19%)
TypeScript (26.78%)
Shell (0.03%)
Total Downloads
1,008
Last Day
1
Last Week
2
Last Month
5
Last Year
82
764 Stars
517 Commits
134 Forks
18 Watching
15 Branches
77 Contributors
Latest Version
1.6.8
Package Id
node-mocks-http-self@1.6.8
Size
16.67 kB
NPM Version
3.10.8
Node Version
6.9.1
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
66.7%
5
Compared to previous month
Last year
-28.7%
82
Compared to previous year
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.
1$ 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:
1var httpMocks = require('node-mocks-http');
Suppose you have the following Express route:
1app.get('/user/:id', routeHandler);
And you have created a function to handle that route's call:
1var routeHandler = function( request, response ) { ... };
You can easily test the routeHandler
function with some code like
this using the testing framework of your choice:
1exports['routeHandler - Simple testing'] = function(test) { 2 3 var request = httpMocks.createRequest({ 4 method: 'GET', 5 url: '/user/42', 6 params: { 7 id: 42 8 } 9 }); 10 11 var response = httpMocks.createResponse(); 12 13 routeHandler(request, response); 14 15 var data = JSON.parse( response._getData() ); 16 test.equal("Bob Dog", data.name); 17 test.equal(42, data.age); 18 test.equal("bob@dog.com", data.email); 19 20 test.equal(200, response.statusCode ); 21 test.ok( response._isEndCalled()); 22 test.ok( response._isJSON()); 23 test.ok( response._isUTF8()); 24 25 test.done(); 26 27};
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 | {} |
1httpMocks.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:
1var httpMocks = require('node-mocks-http'); 2var res = httpMocks.createResponse({ 3 eventEmitter: require('events').EventEmitter 4}); 5 6// ... 7 it('should do something', function(done) { 8 res.on('end', function() { 9 assert.equal(...); 10 done(); 11 }); 12 }); 13// ...
1httpMocks.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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
6 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 6
Reason
SAST tool is not run on all commits -- score normalized to 4
Details
Reason
Found 9/29 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More