Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supertest pipe and end #241

Closed
ganna-shmatova opened this issue Jun 3, 2015 · 4 comments
Closed

supertest pipe and end #241

ganna-shmatova opened this issue Jun 3, 2015 · 4 comments

Comments

@ganna-shmatova
Copy link

var ws = fs.createWriteStream('./test/dir/test_download.txt');
ws.on('error', function(err){
    console.log(err);
});
ws.on('close', function(){
    console.log('done download');
});
request.get('/download').expect(200)
    .send({'file': './test/nonbinary.txt'})
    .pipe(ws);
    //.end(assert('download'));

cannot call end() after piping.
If you try to end() after pipe(), writestream prematurely ends & never finishes writing the file (so file is empty) & writestream tries to write again & it throws a 'writing after end' error.

the readme says one can use pipe() with supertest. What was the test case used to determine that?

@drauschenbach
Copy link

Confirming that this is a problem. I see no way of uploading a non-multipart application/octet-stream in a PUT or POST request.

@shaunc
Copy link

shaunc commented Jan 22, 2017

I had a related problem... seems to be possible to upload (at least) using the promise interface. However, the "onend()" of the stream doesn't chain a callback to Test.end().

var csvStream = csv.createWriteStream();
var req = request.post("/sheets/" + sheet3.id + "/load").type("csv");
csvStream.pipe(req);

rows.forEach(function(row) {
  csvStream.write(row);
});
return new Promise(function(res) {
  csvStream.end(res);
}).then(function() {
 return req;
});

Intercepting the call to Test.end, it is called from stream.end() without a callback:

  at Test.end (/Volumes/Macintosh_HD/Users/shauncutts/src/xquery-rest/node_modules/supertest/lib/test.js:125:7)
  at CsvTransformStream.onend (_stream_readable.js:511:10)
  at CsvTransformStream.g (events.js:292:16)
  at emitNone (events.js:91:20)
  at CsvTransformStream.emit (events.js:185:7)
  at endReadableNT (_stream_readable.js:974:12)
  at _combinedTickCallback (internal/process/next_tick.js:74:11)
  at process._tickDomainCallback (internal/process/next_tick.js:122:9)

resulting in an error when assert tries to chain to the callback. This error also doesn't seem to be caught in the promise chain correctly.

@dkadrios
Copy link

dkadrios commented Mar 23, 2017

Turns out writeable streams have a 'finish' event.

This is working for me:

after((done) => {
    request(server)
        .get('/coverage/download')
        .pipe(fs.createWriteStream('./coverage.zip'))
        .on('finish', done);
});

@isaachinman
Copy link

@dkadrios Any idea how to do this in reverse, eg using .pipe for a POST upload request?

@rimiti rimiti closed this as completed Mar 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants