如何使用Passport验证超级测试请求? [英] How to authenticate Supertest requests with Passport?
本文介绍了如何使用Passport验证超级测试请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Passport.js进行身份验证(本地策略),并通过Mocha和Supertest进行测试.
I'm using Passport.js for authentication (local strategy) and testing with Mocha and Supertest.
如何使用Supertest创建会话并发出经过身份验证的请求?
How can I create a session and make authenticated requests with Supertest?
推荐答案
您应该为此使用 superagent .它是较低级别的模块,由supertest
使用.看看持久代理的部分:
You should use superagent for that. It is lower level module and used by supertest
. Take a look at the section Persisting an agent:
var request = require('superagent');
var user1 = request.agent();
user1
.post('http://localhost:4000/signin')
.send({ user: 'hunter@hunterloftis.com', password: 'password' })
.end(function(err, res) {
// user1 will manage its own cookies
// res.redirects contains an Array of redirects
});
现在您可以使用user1
发出经过身份验证的请求.
Now you can use user1
to make authenticated requests.
这篇关于如何使用Passport验证超级测试请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文