你怎么能用superagent来使用cookies?
我正在用类似这样的快递进行cookie会话pipe理:
req.session.authentication = auth;
我用类似的东西来validationauthentication的url
if(!req.session.authentication){res.send(401);}
现在我正在用摩卡, superagent和应该build立testing,但是我似乎无法find一种方法来获取/设置与superagent的cookie。 我甚至试图在authenticationtesting之前请求login,但它不工作,
我已经尝试在摩卡BDD套件的before语句中将请求添加到login中,但是它仍然告诉我该请求是未经授权的,我已经testing了从浏览器执行请求的身份validation,但是它不能从套房有什么想法?
使用superagent.agent()
(而不是普通的老superagent
)使请求有持久的cookie。 请参阅superagent文档中的“保留Cookie”或代码示例: agency.js , controller.test.js 。
看起来像下面的代码工作正常;
req.set('Cookie',“cookieName1 = cookieValue1; cookieName2 = cookieValue2”);
如果问题是在发送CORS请求的Cookie,请使用这里描述的 .withCredentials()
方法
request .get('http://localhost:4001/') .withCredentials() .end(function(err, res) { })
既然你提到你需要同时获取和设置cookie:
得到:
const request = await Superagent.get('...') const cookie = request.header['set-cookie']
组:
Superagent.post('...').set('Cookie', 'cookie_info')