在测试请求的同时/之后,有什么方法可以直接访问 中的req
对象?supertest
我想测试我的passport
策略,所以我想检查req.user
, req.session
,也许还有其他的。我知道我可以测试页面重定向或 Flash,因为这些是我的策略所做的,但是查看对象上是否有用户似乎也很有用req
。如果我这样做,我还可以随时检查有多少用户。
我将使用“本地注册”策略为用户注册,该策略是这样定义的:
'use strict';
// get passport & mongoose
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var UserModel = require('mongoose').model('User');
module.exports = function() {
// signup function
passport.use('local-signup', new LocalStrategy({
passReqToCallback: true // pass the entire request to the callback
},
function(req, username, password, done) {
process.nextTick(function() {
// find a user with the same username
UserModel.findOne({username: username}, function(err, user) {
// if there is an error, log it then return it
if(err) {
console.log("Error finding a user in the database: " + err);
return done(err);
}
// if a user was already found
if(user) {
return done(null, false, "User already exists");
}
// if we get this far, create a new user from the request body
var newUser = new UserModel(req.body);
// save it and sign it in
newUser.save(function(err) {
if(err) {
console.log("Error during signup: " + err);
return done(err);
}
return done(null, newUser);
});
});
});
}
));
};
我使用这种策略的一种方式是这样的:
我的“本地”策略定义如下:
'use strict';
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var UserModel = require('mongoose').model('User');
module.exports = function() {
// create our local passport strategy & use it
passport.use(new LocalStrategy({
// use the default names for the username & password fields
usernameField: 'username',
passwordField: 'password'
},
// main strategy function
function(username, password, done) {
// find user with given username
UserModel.findOne({
username: username
},
// with this username, do this
function(err, user) {
// if there's an error, log it then pass it along
if(err) {
console.log("Error during login: " + err);
return done(err);
}
// if the username and/or password is incorrect, return an error
// along with a message
if(!user || !user.authenticate(password)) {
return done(null, false, {
message: 'Invalid username and/or password'
});
}
// if everything is correct, return the user document from the database
return done(null, user);
});
}
));
};
我使用这两种策略,例如:
app.route(pageName).post(function(req, res, next) {
passport.authenticate(strategyName, function(err, user, info) {
if(err || !user) {
res.status(401).send(info);
}
else {
req.login(user, function(err) {
if(err) {
res.status(400).send(err);
}
else {
res.send(null);
}
});
}
})(req, res, next);
});
我试过了
request = require('supertest');
this.authServer = require('../my-server');
request(this.authServer)
.put('/signup')
.set('Content-Type', 'application/json')
.set('Host', 'konneka.org')
.send(this.fullUser)
.end(function(req, res, done) {
console.log(res);
});
res
我在函数内部记录的对象end()
太长而无法在此处显示,req
它上面定义了一个对象,但它似乎只有在打开请求之前定义的对象和函数。换句话说,它没有我想要的req.user
、req.session
或其他对象,因为它们是在请求完成并启动新请求之后定义的。我注意到它也有状态码,它们只在请求完成后才定义,所以我一定遗漏了一些东西。
req
在您正在测试的请求结束后,有什么方法可以访问该对象?或者我会以完全错误的方式解决这个问题?