我正在尝试在使用 Supertest 进行测试时在 POST 请求的 header 部分中添加承载。我尝试了很多方法。我是测试的初学者。请提出一些更好的方法来实现这一目标。
这是我的示例代码。这有什么问题吗?
it('POST/userAuth', async (done) => {
const res = await request
.post('/v1/user/haha')
.set('Authorization', `bearer ${Token}`)
.send({
title: 'Some random text',
options: [
{ start: hello, end: world },
{ start: good, end: bye },
],
});
最佳答案
您可以像这样设置请求 header :
const request = require('supertest');
const express = require('express');
const app = express();
const TOKEN = 'some_token';
describe('POST /some-url', function() {
it('does something', function(done) {
request(app)
.post('/some-url')
.send({ body: 'some-body' })
.set('Authorization', `Bearer ${TOKEN}`)
.expect(200, done);
});
});
关于node.js - 使用 Supertest 进行测试时在 header 部分设置 Bearer Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65856278/