关于使用egg.js的 单元测试 controller 如何添加header
我想单元测试 token的验证代码 我把token放在header 的 Authorization中。 请问如何添加header。 我使用下面的方法是错误的
it.only('post /data', async () => {
const result = await app.httpRequest()
.post('/data')
.header({
Authorization: '11111',
})
.type('form')
.send({ test: 'admin', test: 'root' });
console.log(result.body);
});
7 回复
egg.js的单元测试用的是egg-mock,里面请求用的是superTest,而superTest网络请求用的是superAgent,添加header根据文档查到是SuperAgent.Setting header fields
BTW,我自己也是这样用的~
希望我这样写能帮助到你。
app.httpRequest() 就是一个 supertest 实例,看它文档就知道了。
app.httpRequest().get('/').set('xx', 'aaa')