Node js 使用 mocha chai 测试受保护的路由?
Node js testing protected routes using mocha chai?
函数检查路由是否可访问
function isSessionCookieValid(req, res, next) {
if (!isValid(req.session)) {
return res.status(401).json({
isLoggedIn: false
});
}
return next();
}
在另一个文件中,我做了一个 post 请求,这是一个使用上述函数的受保护路由
app
.route('/url')
.post(utils.isSessionCookieValid, (req, res, next) => {})
测试部分
问题是我不知道如何模拟 isSessionCookieValid,因为它需要下一个但无法在我的测试中传递下一个回调:
describe('testing routes', () => {
it('should enter the route body', (done) => {
utils.isSessionCookieValid(req, res, 'next should be here...');
chai.request(server)
.post('/url')
.end(function (error, response, body) {
if (error) {
done(error);
} else {
done();
}
});
});
});
错误: TypeError: next 不是函数
我将使用 sinonjs 作为模拟库。模拟isSessionCookieValid
个中间件的实现,让它一直到下一个中间件。
例如
server.ts
:
import express from 'express';
import * as utils from './utils';
const app = express();
const port = 3000;
app.route('/url').post(utils.isSessionCookieValid, (req, res, next) => {
res.sendStatus(200);
});
if (require.main === module) {
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
}
export { app };
utils.ts
:
function isSessionCookieValid(req, res, next) {
if (!isValid(req.session)) {
return res.status(401).json({
isLoggedIn: false,
});
}
return next();
}
function isValid(session) {
return true;
}
export { isSessionCookieValid };
server.test.ts
:
import * as utils from './utils';
import sinon from 'sinon';
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
chai.use(chaiHttp);
describe('testing routes', () => {
it('should enter the route body', (done) => {
const isSessionCookieValidStub = sinon.stub(utils, 'isSessionCookieValid').callsFake((req, res, next) => {
next();
});
const { app } = require('./server');
chai
.request(app)
.post('/url')
.end((error, response) => {
if (error) {
return done(error);
}
sinon.assert.calledOnce(isSessionCookieValidStub);
expect(response).to.have.status(200);
done();
});
});
});
单元测试结果:
testing routes
✓ should enter the route body (500ms)
1 passing (510ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 60 | 25 | 25 | 60 |
server.ts | 80 | 50 | 50 | 80 | 12-13
utils.ts | 20 | 0 | 0 | 20 | 2-11
-----------|---------|----------|---------|---------|-------------------
源代码:https://github.com/mrdulin/expressjs-research/tree/master/src/Whosebug/54462600
函数检查路由是否可访问
function isSessionCookieValid(req, res, next) {
if (!isValid(req.session)) {
return res.status(401).json({
isLoggedIn: false
});
}
return next();
}
在另一个文件中,我做了一个 post 请求,这是一个使用上述函数的受保护路由
app
.route('/url')
.post(utils.isSessionCookieValid, (req, res, next) => {})
测试部分
问题是我不知道如何模拟 isSessionCookieValid,因为它需要下一个但无法在我的测试中传递下一个回调:
describe('testing routes', () => {
it('should enter the route body', (done) => {
utils.isSessionCookieValid(req, res, 'next should be here...');
chai.request(server)
.post('/url')
.end(function (error, response, body) {
if (error) {
done(error);
} else {
done();
}
});
});
});
错误: TypeError: next 不是函数
我将使用 sinonjs 作为模拟库。模拟isSessionCookieValid
个中间件的实现,让它一直到下一个中间件。
例如
server.ts
:
import express from 'express';
import * as utils from './utils';
const app = express();
const port = 3000;
app.route('/url').post(utils.isSessionCookieValid, (req, res, next) => {
res.sendStatus(200);
});
if (require.main === module) {
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
}
export { app };
utils.ts
:
function isSessionCookieValid(req, res, next) {
if (!isValid(req.session)) {
return res.status(401).json({
isLoggedIn: false,
});
}
return next();
}
function isValid(session) {
return true;
}
export { isSessionCookieValid };
server.test.ts
:
import * as utils from './utils';
import sinon from 'sinon';
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
chai.use(chaiHttp);
describe('testing routes', () => {
it('should enter the route body', (done) => {
const isSessionCookieValidStub = sinon.stub(utils, 'isSessionCookieValid').callsFake((req, res, next) => {
next();
});
const { app } = require('./server');
chai
.request(app)
.post('/url')
.end((error, response) => {
if (error) {
return done(error);
}
sinon.assert.calledOnce(isSessionCookieValidStub);
expect(response).to.have.status(200);
done();
});
});
});
单元测试结果:
testing routes
✓ should enter the route body (500ms)
1 passing (510ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 60 | 25 | 25 | 60 |
server.ts | 80 | 50 | 50 | 80 | 12-13
utils.ts | 20 | 0 | 0 | 20 | 2-11
-----------|---------|----------|---------|---------|-------------------
源代码:https://github.com/mrdulin/expressjs-research/tree/master/src/Whosebug/54462600