我喜欢使用SuperTest来处理我的身份验证系统:
[0]
valid_token()会插入有效的访问令牌
const request = require('./valid_access_token')(require('supertest'));
request(app)
.get('/v1/bots')
.valid_token()
.expect('Content-Type', /json/)
...
然而,当我运行它时,我收到此错误:
module.exports = function (Request) {
const Token = require('../../../app/v1/models/loader')('token');
const UserFactory = require('../fixtures/user');
Request.prototype.valid_token = function()
{
return UserFactory.createAsync('user')
.then(function(user){
return new Token({username: user.username}).createWeeklyAsync()
.then(function(userToken){
this.set("access-token",userToken[0].token);
return this;
})
})
}
return Request;
}
我尝试了各种不同的方法,没有运气。
答案 0 :(得分:0)
所有功劳归于https://github.com/acabrera91
const request = require('supertest');
const Test = request.Test;
Test.prototype.authenticate = function(user) {
const {token, xsrfToken} = user.tokens;
return this
.set('Authorization', `Bearer ${token}`)
.set('X-XSRF-TOKEN', xsrfToken);
}
然后在你的测试中
return request(app)
.post('/user/settings')
.authenticate(user)
.send(...)
https://github.com/visionmedia/supertest/issues/381#issuecomment-282198972