我正在创建一个与API连接的网络应用,以便进行登录和其他操作,目前,我正在尝试使用/authenticate
,{ {1}}和chai
。
chai-http
测试通过,但是我遇到了这个错误,据此,页面没有重定向,因为调用没有被nock捕获,而是直接发送到API:
nock
但是当我使用真实有效的电子邮件和密码时,此测试通过了,没有发现错误:
var chai = require('chai');
var expect = chai.expect;
var chaiHttp = require('chai-http');
var nock = require('nock');
chai.use(chaiHttp);
describe('/authenticate', function() {
var agent = chai.request.agent('http://localhost:3000');
afterEach(function() {
agent.close();
nock.cleanAll();
});
describe('User authorized', function() {
it('redirects to /dashboard', function() {
// I'm trying to mock the response here but is not working.
nock('http://the.api.com:8080')
.post('/v1/authenticate')
.reply(201, {
'authorized': true,
'jwt': 'thejwtasdf'
});
agent
.post('/authenticate')
.send({ email: 'test@gmail.com', password: 'TheAmazingPass' })
.then(function(res) {
expect(res).to.redirectTo('http://localhost:3000/dashboard');
expect(res.text).to.match(/Dashboard/);
})
.catch(function(e) { console.log(e); });
});
});
});
通过此代码,测试通过了,我是否缺少nock?
===编辑===
这是我要测试的代码:
这是我的登录路由器(flashMessages是帮助处理Flash消息的自定义中间件)。
{ AssertionError: expected redirect with 30X status code but got 200
at Proxy.<anonymous>
... rest of the error omitted.
登录路由器使用loginService,这是用于登录的部分:
var chai = require('chai');
var expect = chai.expect;
var chaiHttp = require('chai-http');
var nock = require('nock');
chai.use(chaiHttp);
describe('/authenticate', function() {
var agent = chai.request.agent('http://localhost:3000')
afterEach(function() {
agent.close();
nock.cleanAll();
});
describe('User authorized', function() {
it('redirects to /dashboard', function() {
agent
.post('/authenticate')
.send({ email: 'realemail@gmail.com', password: 'RealPass' })
.then(function(res) {
expect(res).to.redirectTo('http://localhost:3000/dashboard');
expect(res.text).to.match(/Dashboard/);
})
.catch(function(e) { console.log(e); });
});
});
});
最后我有一个用于http请求的工具:
var loginService = require('../services/login');
var flashMessages = require('../utils/flash_messages').flashMessages;
var router = require('express').Router();
// check if user is logged in
var sessionChecker = function(req, res, next) {
if (req.session.auth && req.cookies.user_sid) {
res.redirect('/dashboard');
} else {
next();
}
};
router.get('/', sessionChecker, flashMessages, function(req, res, next){
res.render('login', {
title: 'Welcome',
errors: res.locals.flash.errors,
typeError: res.locals.flash.errorType,
});
});
router.post('/authenticate', function(req, res, next){
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Invalid email format').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
req.getValidationResult().then(function(result){
if (result.isEmpty()) {
loginService.authenticate(req.body).then(function(result){
if (result.authorized){
// success
req.session.auth = result;
req.session.auth.user = loginService.currentUser(result.jwt)
res.redirect('/dashboard');
} else {
// user not found error
req.session.flash = {
errors: [{msg: result.msg}],
errorType: 'anotherError'
};
res.redirect('/');
}
}).catch(function(e){
// server errors
req.session.flash = {
errors: [e],
errorType: 'anotherError'
};
res.redirect('/');
});
} else {
//validation errors
req.session.flash = {
errors: result.array(),
errorType: 'validationError'
};
res.redirect('/');
}
});
});
module.exports = router;
任何帮助将不胜感激。
致谢。