
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
Stub request and response objects for testing express applications and middleware
Stub request and response objects for testing express applications and middleware
Creates default request and response object with configurable values, and methods stubbed with sinon.stub.
Using default request properties:
const middleware = function (req, res, next) {
req.session.path = req.path;
};
const reqres = require('reqres');
describe('my middleware', function () {
let req;
let res;
beforeEach(function () {
req = reqres.req(),
res = reqres.res()
});
it('request has properties', function () {
middleware(req, res, function (err) {
req.session.path.should.equal('/');
done(err);
});
});
});
const router = require('express').Router();
router.route('/foo')
.get(function (req, res, next) {
req.user = req.session.username;
next();
})
.post(function (req, res, next) {
req.session.username = req.body.user;
next();
});
const reqres = require('reqres');
describe('my router', function () {
let req;
let res;
beforeEach(function () {
req = reqres.req({ url: '/foo', session: { username: 'lennym' } });
res = reqres.res();
});
it('sets username from session to req.user', function (done) {
router(req, res, function () {
req.user.should.equal('lennym');
done();
});
});
it('sets POST-ed username to session', function (done) {
req.method = 'POST';
req.body = { user: 'user' };
router(req, res, function () {
req.session.username.should.equal('user');
done();
});
});
});
All methods which would normally send a response (and so end the middleware chain) e.g. json, send will emit an "end" event, so this can be bound onto for running assertions.
const router = require('express').Router();
router.route('/foo')
.get(function (req, res) {
res.json({ user: req.session.username });
});
const reqres = require('reqres');
describe('my router', function () {
let req;
let res;
beforeEach(function () {
req = reqres.req({ url: '/foo', session: { username: 'lennym' } });
res = reqres.res();
});
it('responds with json showing user from session', function (done) {
router.handle(req, res);
res.on('end', function () {
res.json.should.have.been.calledWithExactly({ user: 'lennym' });
done();
});
});
});
If you want to use a different version of sinon to that which is included in reqres - or indeed a completely different stub/spy library - then you can set the sinon property to your own local version.
const reqres = require('reqres');
reqres.sinon = require('sinon');
FAQs
Stub request and response objects for testing express applications and middleware
The npm package reqres receives a total of 2,512 weekly downloads. As such, reqres popularity was classified as popular.
We found that reqres 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.