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

Handle stream payload errors before socket connection #225

Merged
merged 3 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,17 @@ internals.deferPipeUntilSocketConnects = function (req, stream) {
};
const onSocketConnect = () => {

stream.removeListener('error', onStreamError);
stream.pipe(req);
};
const onStreamError = (err) => {

req.emit('error', err);
};


req.once('socket', onSocket);
stream.on('error', onStreamError);
};


Expand Down
31 changes: 21 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,25 +1127,19 @@ describe('read()', () => {
const stream = new Stream.Readable({
read() {

piped = true;
read = true;
this.push(null);
}
});
const onPiped = () => {

piped = true;
};
let piped = false;

stream.on('pipe', onPiped);
let read = false;

const promiseA = Wreck.request('post', 'http://localhost:0', {
agent,
payload: stream
});

await expect(promiseA).to.reject(Error, /Unable to obtain socket/);
expect(piped).to.equal(false);
expect(read).to.equal(false);

const handler = (req, res) => {

Expand All @@ -1158,10 +1152,27 @@ describe('read()', () => {
payload: stream
});
expect(res.statusCode).to.equal(200);
expect(piped).to.equal(true);
expect(read).to.equal(true);
server.close();
});

it('will handle stream payload errors between request creation and connection establishment', async () => {

const agent = new internals.SlowAgent();
const stream = new Stream.Readable();
const promiseA = Wreck.request('post', 'http://localhost:0', {
agent,
payload: stream
});

process.nextTick(() => {

stream.emit('error', new Error('Asynchronous stream error'));
});

await expect(promiseA).to.reject(Error, /Asynchronous stream error/);
});

it('times out when stream read takes too long', async () => {

const TestStream = function () {
Expand Down