首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在node.js服务器上使用supertest/superagent读取响应输出缓冲区/流

在node.js服务器上使用supertest/superagent读取响应输出缓冲区/流
EN

Stack Overflow用户
提问于 2012-11-27 05:18:00
回答 5查看 15.6K关注 0票数 21

我正在尝试编写一个测试来检查API路由是否输出内容正确的ZIP文件。

我使用mocha和supertest进行测试,我想实际读取输出流/缓冲区,读取zip文件内容并查看内容是否正确。

你知道我该怎么做吗?当我尝试读取res.body时,它只是一个空对象。

  request(app)
    .get( "/api/v1/orders/download?id[]=1&id=2" )
    .set( "Authorization", authData )
    .expect( 200 )
    .expect( 'Content-Type', /application\/zip/ )
    .end( function (err, res) {
      if (err) return done( err );

      console.log( 'body:', res.body )

      // Write the temp HTML file to filesystem using utf-8 encoding
      var zip = new AdmZip( res.body );
      var zipEntries = zip.getEntries();

      console.log( 'zipentries:', zipEntries );

      zipEntries.forEach(function(zipEntry) {
        console.log(zipEntry.toString()); // outputs zip entries information
      });

      done();
    });
EN

回答 5

Stack Overflow用户

发布于 2013-02-11 04:48:38

根据@Beau的回答展开,下面的代码可以用来获取任何二进制响应内容作为缓冲区,您可以在request.end()中进一步研究它

function binaryParser(res, callback) {
    res.setEncoding('binary');
    res.data = '';
    res.on('data', function (chunk) {
        res.data += chunk;
    });
    res.on('end', function () {
        callback(null, new Buffer(res.data, 'binary'));
    });
}

// example mocha test
it('my test', function(done) {
    request(app)
        .get('/path/to/image.png')
        .expect(200)
        .expect('Content-Type', 'image.png')
        .buffer()
        .parse(binaryParser)
        .end(function(err, res) {
            if (err) return done(err);

            // binary response data is in res.body as a buffer
            assert.ok(Buffer.isBuffer(res.body));
            console.log("res=", res.body);

            done();
        });
});
票数 35
EN

Stack Overflow用户

发布于 2012-12-22 04:20:38

我认为您应该为application/zip创建自己的解析器,并使用它来获取实际的响应数据;例如,JSON解析器就是here。一旦你得到了它,你就可以通过将它传递给request.parse来使用它;所以你的测试应该是:

request(app)
  .get( "/api/v1/orders/download?id[]=1&id=2" )
  .set( "Authorization", authData )
  .expect( 200 )
  .expect( 'Content-Type', /application\/zip/ )
  .parse( function (res, fn) {
    res.data = '';
    res.on( 'data', function (chunk) { res.data += chunk; } );
    res.on( 'end', function () {
      try {
        fn( null, new AdmZip( res.data ) );
      } catch ( err ) {
        fn( err );
      }
    });
  })
  .end( function (err, res) {
    if (err) return done( err );

    console.log( 'body:', res.body )

    // Write the temp HTML file to filesystem using utf-8 encoding
    var zipEntries = res.body.getEntries();

    console.log( 'zipentries:', zipEntries );

    zipEntries.forEach(function(zipEntry) {
      console.log(zipEntry.toString()); // outputs zip entries information
    });

    done();
  });

为了找到这个问题的答案,我主要依靠检查superagent测试套件。:)

票数 2
EN

Stack Overflow用户

发布于 2021-08-06 08:35:55

Testing binary response with supertest中所述,在请求上设置.responseType('blob')将导致req.body成为缓冲区。

https://visionmedia.github.io/superagent/#binary

it('test', async () => {
  await request(app)
    .get('/api/some-zip')
    .responseType('blob')
    .expect(200)
    .expect('Content-Type', /application\/zip/)
    .expect(( res) => {
      const zipEntries = new AdmZip(res.body).getEntries().map(e => e.entryName);
      expect(zipEntries).toEqual(expect.arrayContaining(['zipfile1.pdf', 'zipfile2.pdf']));
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13573315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档