Javascript frisbyjs测试失败,因为get()未发送HTTP头
我在另一个失败的frisbyjs测试的afterJSON()中有一个frisbyjs测试。调试服务器代码时,似乎没有发送x-access-token和x-key HTTP头。我送错了吗?我肯定在做傻事 下面是外部测试。afterJSON()中的第一个测试失败:Javascript frisbyjs测试失败,因为get()未发送HTTP头,javascript,node.js,jasmine-node,Javascript,Node.js,Jasmine Node,我在另一个失败的frisbyjs测试的afterJSON()中有一个frisbyjs测试。调试服务器代码时,似乎没有发送x-access-token和x-key HTTP头。我送错了吗?我肯定在做傻事 下面是外部测试。afterJSON()中的第一个测试失败: frisby.create('Should be able to log in with test user') .post(appHost + '/login', { username:'test@voxoid.
frisby.create('Should be able to log in with test user')
.post(appHost + '/login',
{
username:'test@voxoid.com',
password:'pass123'
},
{json: true},
{headers: {'Content-Type': 'application/json'}})
.expectStatus(200)
.expectJSONTypes({ token: String })
.expectJSON({
user: {
name:'test',
role:'admin',
username:'test@voxoid.com'
}
})
.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()的结果是:
{ status: 401, message: 'Invalid Token or Key' }
下面是处理请求的服务器代码(一个快速中间件),其中令牌和密钥在调试时都是“未定义”的,res.headers不包含x-access-token或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;
}
};
var jwt=require('jwt-simple');
var validateUser=require('../routes/auth')。validateUser;
module.exports=功能(请求、恢复、下一步){
//执行跨域请求时,您将收到
//首先是预飞行请求。这是为了检查我们的应用程序
//这是安全的。
//对于[OPTIONS]请求,我们跳过token outh。
//如果(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'];
如果(令牌| |键){
试一试{
var decoded=jwt.decode(令牌,require('../config/secret.js')());
如果(decoded.exp是的,那么它很简单——“几乎是一个打字错误”。下面是工作代码:
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();
请注意,我们是如何将单个options对象传递给.get()
,而不是三个单独的对象(对于json
和头,一个在开头为空)
另外:如果您的大多数请求都包含这些标头,则全局设置它们可能会很有用。这也适用于其他选项:
frisby.globalSetup({
request: {
json: true,
headers: {
'x-access-token': res.token,
'x-key': res.user.username
}
}
});
frisby.create('Should allow access with valid token')
.get(appHost + '/api/v1/products') //no need for options - they're already set!
.inspectJSON()
.expectStatus(200)
.toss();
frisby.create('Should not allow access with invalid token')
.get(appHost + '/api/v1/products', {
// ...but you still can override them - when needed
headers: {
'x-access-token': res.token + '123',
'x-key': res.user.username
}
})
.expectStatus(401)
.toss();
谢谢你的详细回复!我做了你推荐的更改,但即使在那之后,标题仍然没有通过。还有其他想法吗?我希望Frisby.js文档和你的回复一样详细。是的,Frisby.js文档远不是完美的。它曾经是我的玩具,但我不久前放弃了它-我想主要是因为它基于jasmine node,它仍然没有使用jasmine 2.0(它是beta版),并切换到使用SuperTest:。关于你的情况-恐怕我没有主意了,我唯一能建议的是省略.get()
请求的json:true
选项。我看到这个选项在这里分解它们(Frisby端的错误)。还记得将options对象作为第二个参数传递给.get()
调用,并将第三个参数传递给.post()
。祝你好运!再次感谢bardzusny!frisbyjs占用了我太多的时间,所以我转而使用普通jasmine节点和请求,并发现我不会错过任何额外的frisbyjs可能提供的功能。但是supertest看起来不错,比普通jasmine节点/请求更简洁。