【问题标题】:npm nock: mock post multipart-form uploading filenpm nock:模拟发布多部分形式的上传文件
【发布时间】:2019-12-26 21:32:48
【问题描述】:

目标

使用nock,我正在寻找一种解决方案来模拟通过 POST multipart/form-data 上传的微小 PNG 文件。

curl:Box API 上传 PNG 文件

以下curl 脚​​本介绍了如何在根目录'0'upload a file through Box API文件名:'dummy.png'

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'

简明回复:

Success [HTTP status: 201]
{
  "total_count": 1,
  "entries": [
    {
      "type": "file",
      "name": "dummy.png",
      "id": "584886508967"
    }
  ]
}

nock 尝试:Box API 上传 PNG 文件

下一个代码 sn-p 正在使用 npm 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'
      }
    ]
  });

nock 尝试:缺少表单属性和文件二进制文件

我不清楚如何在nock 代码中包含 curl POST 请求中包含的内容:

--header 'Content-Type: multipart/form-data' \
--form attributes='{ "name": "dummy.png", "parent": { "id": "0" } }' \
--form file=@'./files/dummy.png'

我想包含在 nock POST 请求中:

  • 文件 dummy.png 二进制文件--form file=@'./files/dummy.png' 中定义
  • 文件上传元数据--form attributes='{ "name": "dummy.png", "parent": { "id": "0" } }' 定义

谢谢,感谢您的帮助。

【问题讨论】:

    标签: multipartform-data box-api nock


    【解决方案1】:

    正如您在问题中提到的那样,Nock 不需要表单数据来拦截请求并模拟响应。但是,如果您正在测试您的代码是否发送了正确的请求正文,那么进行断言是一种很好的做法。

    --formflag in cURL is a helper 为不同的协议做不同的事情。

    对于 HTTP 协议族,这让 curl 模拟用户按下提交按钮的填写表单。这会导致 curl 根据 RFC 2388 使用 Content-Type multipart/form-data 发布数据。

    没有太大帮助,但要点是您在 Nock 中查找的数据将位于 POST 请求的正文中。您问题中的诺克代码在正确的轨道上。使用回调作为第二个参数的post 方法是您可以深入挖掘被 Nock 拦截的原始数据的方法。不太直接的部分是传递给回调的 body 参数是十六进制编码的,因为正文包含 png 文件的二进制数据。

    .post('/files/content', body => {
      const decoded = Buffer.from(body, 'hex');
      console.log(decoded);
      return true;
    })
    

    将上面的 sn-p 添加到您现有的 post 方法应输出类似于:

    ----------------------------493956036364114509087826
    Content-Disposition: form-data; name="attributes"
    
    {"name":"dummy.png","parent":{"id":"0"}}
    ----------------------------493956036364114509087826
    Content-Disposition: form-data; name="content"; filename="unused"
    Content-Type: image/png
    
    �PNG
    
    
    IHDRĉ
    IDATx�c��������IEND�B`�
    ----------------------------493956036364114509087826--
    

    此时由您决定正文是否包含您期望的数据,如果是,则返回true。 参考 RFC 2388 了解多部分表单数据的外观可能会有所帮助。

    【讨论】:

    • @matt-r-wilson 谢谢你的回复,我今天试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2016-04-05
    相关资源
    最近更新 更多