目标
[使用nock
,我正在寻找一种方法来模拟通过POST nock
上传的小PNG文件。
curl:Box API上传PNG文件
以下multipart/form-data
脚本介绍了如何在根目录'0'中的curl
,文件名:'dummy.png'。
upload a file through Box API简明回应:
curl 'https://upload.box.com/api/2.0/files/content' \ --request POST \ --verbose \ --silent \ --header 'authorization: Bearer [** Access Token **]' \ --header 'Content-Type: multipart/form-data' \ --form attributes='{ "name": "dummy.png", "parent": { "id": "0" } }' \ --form file=@'./files/dummy.png'
尝试尝试:Box API上传PNG文件
下一个代码段使用npm Success [HTTP status: 201]
{
"total_count": 1,
"entries": [
{
"type": "file",
"name": "dummy.png",
"id": "584886508967"
}
]
}
起作用,但是,此模拟是不完整的:
nock尝试尝试:缺少表单属性和文件二进制文件
我不清楚如何在const accessToken = v4();
const randomFileId = v4();
let boundary = '';
const scope = nock('https://upload.box.com/api/2.0/')
.log((m, d) => logger.debug(m, d))
.matchHeader('authorization', `Bearer ${accessToken}`);
scope
.matchHeader('content-type', val => {
const matches = val.match(/^multipart\/form-data; boundary=([a-zA-Z0-9\-]+)$/);
if (matches && matches.length > 1) {
boundary = matches[1];
}
return !!matches;
})
.post('/files/content', body => {
return true;
})
.reply(201, {
entries: [
{
id: randomFileId,
name: 'dummy.png',
type: 'file'
}
]
});
代码中包含curl POST请求中包含的内容:
nock我想在nock POST请求中包括:
--header 'Content-Type: multipart/form-data' \
--form attributes='{ "name": "dummy.png", "parent": { "id": "0" } }' \
--form file=@'./files/dummy.png'
中定义--form file=@'./files/dummy.png'
]定义>谢谢,感谢您的帮助。
Goal with nock,我正在寻找一种解决方案,可通过POST multipart / form-data模拟一个很小的PNG文件。 curl:Box API上传PNG文件以下curl脚本介绍了如何通过...
--form attributes='{ "name": "dummy.png", "parent": { "id": "0" } }'
--form
对不同的协议执行不同的操作。