简体   繁体   English

如何使用 chai-http 下载文件?

[英]How to download a file with chai-http?

Here is my problem: I have to test a route which enriches files and makes them downloadable.这是我的问题:我必须测试一个丰富文件并使它们可下载的路由。 The problem I have is that I cannot get the enriched file during my test.我遇到的问题是我在测试期间无法获得丰富的文件。

I manage to recover this file with Axios (for a terminal command) but I have to use chai-http for the tests.我设法使用 Axios (用于终端命令)恢复此文件,但我必须使用 chai-http 进行测试。

router.js路由器.js

const router = require('express').Router();
const path = require('path');

const { enrichmentFileJSON } = require('../services/enrich/json');

router.post('/enrich/json', async (req, res) => {
  let { args } = req.query;
  await enrichmentFileJSON(req, args);
  return res.status(200).download(path.resolve(tmpDir, 'enriched.jsonl'));
});

testEnrichment.js testEnrichment.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const fs = require('fs-extra');
const path = require('path');

chai.should();
chai.use(chaiHttp);

const url = 'http://localhost:8080';

describe('/enrich/json enrich a jsonl file', () => {
    it('should return the enriched file', async () => {
      const response = await chai.request(url)
        .post('/enrich/json')
        .attach('fileField', path.resolve(__dirname, 'enrichment', 'file.jsonl'), 'file.jsonl')
        .set('Access-Control-Allow-Origin', '*')
        .set('Content-Type', 'multipart/form-data')

      // i want to use something like this
      const writer = fs.createWriteStream(path.resolve(__dirname, 'tmp', 'enriched.jsonl'));
      response.data.pipe(writer);
    });
  });

Thank you in advance !先感谢您 !

For "chai-http": "^4.3.0" , Call .buffer() and .parse() methods on response object to get the buffer of the downloaded file.对于"chai-http": "^4.3.0" ,在response object 上调用.buffer().parse()方法来获取下载文件的缓冲区。

Then, use Readable.from(buffer) of stream module to convert the buffer to a readable stream.然后,使用stream模块的Readable.from(buffer)将缓冲区转换为可读的 stream。

Eg例如

index.js : index.js

const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();

var upload = multer({ dest: path.resolve(__dirname, 'uploads/') });

app.post('/enrich/json', upload.single('fileField'), async (req, res) => {
  return res.status(200).download(path.resolve(__dirname, 'file.jsonl'));
});

module.exports = app;

index.test.js : index.test.js

const chai = require('chai');
const chaiHttp = require('chai-http');
const fs = require('fs');
const path = require('path');
const { Readable } = require('stream');
const app = require('./');
chai.use(chaiHttp);

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

describe('66245355', () => {
  it('should pass', async () => {
    const response = await chai
      .request(app)
      .post('/enrich/json')
      .attach('fileField', path.resolve(__dirname, 'file.jsonl'), 'file.jsonl')
      .set('Content-Type', 'multipart/form-data')
      .buffer()
      .parse(binaryParser);

    const writer = fs.createWriteStream(path.resolve(__dirname, 'enriched.jsonl'));
    Readable.from(response.body.toString()).pipe(writer);
  });
});

file.jsonl , the file you try to upload: file.jsonl ,您尝试上传的文件:

{
  'ok': true
}

enriched.jsonl , the file you enriched and downloaded: enriched.jsonl的.jsonl ,您丰富和下载的文件:

{
  'ok': true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM