npm nock:模拟发布多部分形式的上传文件

问题描述 投票:0回答:1

目标

[使用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请求中包括:

  • [File dummy.png二进制文件在--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脚本介绍了如何通过...

multipartform-data box-api nock
1个回答
0
投票
--form attributes='{ "name": "dummy.png", "parent": { "id": "0" } }' --form对不同的协议执行不同的操作。
© www.soinside.com 2019 - 2024. All rights reserved.