1
我有一个frisbyjs测试在另一个frisbyjs测试的afterJSON()失败。当我调试服务器代码时,看起来x-access-token和x-key HTTP头没有发送。我发错了吗?我当然在做一些愚蠢的事情。frisbyjs测试失败,因为get()没有发送HTTP标头
以下是外部测试。在afterJSON()第一个测试是失败的一个:处理请求,其中
{ status: 401, message: 'Invalid Token or Key' }
这里是服务器代码(特急中间件):
frisby.create('Should be able to log in with test user')
.post(appHost + '/login',
{
username:'[email protected]',
password:'pass123'
},
{json: true},
{headers: {'Content-Type': 'application/json'}})
.expectStatus(200)
.expectJSONTypes({ token: String })
.expectJSON({
user: {
name:'test',
role:'admin',
username:'[email protected]'
}
})
.afterJSON(function(res) {
// TODO: The functionality works, but this test does not; the headers do not get sent.
console.log('x-access-token: ' + res.token);
console.log('x-key: ' + res.user.username);
// **************** THIS IS THE TEST THAT FAILS ********************
frisby.create('Should allow access with valid token')
.get(appHost + '/api/v1/products',{},
{json:true},
{headers:{
'x-access-token': res.token,
'x-key': res.user.username
}})
.inspectJSON()
.expectStatus(200)
.toss();
frisby.create('Should not allow access with invalid token')
.get(appHost + '/api/v1/products',{},
{json:true},
{headers:{
'x-access-token': res.token + '123',
'x-key': res.user.username
}})
.expectStatus(401)
.toss();
})
.toss();
的inspectJSON()的结果令牌和密钥都结束了“不确定”,而调试和res.headers不包含X-访问令牌,也不X-KEY头:
var jwt = require('jwt-simple');
var validateUser = require('../routes/auth').validateUser;
module.exports = function(req, res, next) {
// When performing a cross domain request, you will recieve
// a preflighted request first. This is to check if our the app
// is safe.
// We skip the token outh for [OPTIONS] requests.
//if(req.method == 'OPTIONS') next();
var token = (req.body && req.body.access_token) || (req.query && req.query.access_token) || req.headers['x-access-token'];
var key = (req.body && req.body.x_key) || (req.query && req.query.x_key) || req.headers['x-key'];
if (token || key) {
try {
var decoded = jwt.decode(token, require('../config/secret.js')());
if (decoded.exp <= Date.now()) {
res.status(400);
res.json({
"status": 400,
"message": "Token Expired"
});
return;
}
// Authorize the user to see if s/he can access our resources
var dbUser = validateUser(key); // The key would be the logged in user's username
if (dbUser) {
if ((req.url.indexOf('admin') >= 0 && dbUser.role == 'admin') || (req.url.indexOf('admin') < 0 && req.url.indexOf('/api/v1/') >= 0)) {
next(); // To move to next middleware
} else {
res.status(403);
res.json({
"status": 403,
"message": "Not Authorized"
});
return;
}
} else {
// No user with this name exists, respond back with a 401
res.status(401);
res.json({
"status": 401,
"message": "Invalid User"
});
return;
}
} catch (err) {
res.status(500);
res.json({
"status": 500,
"message": "Oops something went wrong",
"error": err
});
}
} else {
res.status(401);
res.json({
"status": 401,
"message": "Invalid Token or Key"
});
return;
}
};
谢谢您的详细回复!我做了你推荐的更改,但即使在那之后,标题仍然没有通过。任何其他想法?我希望frisbyjs的文档和你的回复一样详细。 – voxoid
是的,Frisby.js文档远非完美。它曾经是我的playtoy,但我之前放弃了它 - 我猜主要是因为它基于茉莉花节点,它仍然没有使用Jasmine 2.0(它处于测试版) - 并切换到使用SuperTest:https:// github.com/visionmedia/supertest。关于你的情况 - 恐怕我没有想法,我只能建议省略'.get()'请求的'json:true'选项。我看到这个选项在这里被打破(Frisby方面的错误)。还要记住将选项对象作为* second *参数传递给'.get()'调用,并将* third *传递给'.post()'。祝你好运! – bardzusny
再次感谢bardzusny! frisbyjs是吃更多的时间比它值得我,所以我切换到使用普通的茉莉花节点和要求,发现我不会错过任何额外frisbyjs可能会提供。但是supertest看起来不错,比简单的jasmine-node/request更简洁。 – voxoid