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

A missing callback on request still allows client requests to work #130

Merged
merged 2 commits into from
May 12, 2016
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
47 changes: 23 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ internals.Client.prototype.request = function (method, url, options, callback, _
err.trace = _trace;
return finishOnce(Boom.badGateway('Client request error', err));
};

req.once('error', onError);

const onResponse = (res) => {
Expand Down Expand Up @@ -201,7 +200,7 @@ internals.Client.prototype.request = function (method, url, options, callback, _

const finish = (err, res) => {

if (!callback || err) {
if (err) {
req.abort();
}

Expand All @@ -228,6 +227,27 @@ internals.Client.prototype.request = function (method, url, options, callback, _
delete options.timeout;
}

// Custom abort method to detect early aborts

const _abort = req.abort;
let aborted = false;
req.abort = () => {

if (!aborted && !req.res && !req.socket) {
process.nextTick(() => {

// Fake an ECONNRESET error

const error = new Error('socket hang up');
error.code = 'ECONNRESET';
finishOnce(error);
});
}

aborted = true;
return _abort.call(req);
};

// Write payload

if (payloadSupported) {
Expand All @@ -245,33 +265,12 @@ internals.Client.prototype.request = function (method, url, options, callback, _
}

stream.pipe(req);
return;
return req;
}

req.write(options.payload);
}

// Custom abort method to detect early aborts

const _abort = req.abort;
let aborted = false;
req.abort = () => {

if (!aborted && !req.res && !req.socket) {
process.nextTick(() => {

// Fake an ECONNRESET error

const error = new Error('socket hang up');
error.code = 'ECONNRESET';
finishOnce(error);
});
}

aborted = true;
return _abort.call(req);
};

// Finalize request

req.end();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"boom": "3.x.x",
"hoek": "3.x.x"
"hoek": "4.x.x"
},
"devDependencies": {
"code": "2.x.x",
Expand Down
35 changes: 35 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,41 @@ describe('read()', () => {
});
});
});

it('allows custom handling of response event and read works', (done) => {

const path = Path.join(__dirname, '../images/wreck.png');
const stats = Fs.statSync(path);
const fileStream = Fs.createReadStream(path);

const server = Http.createServer((req, res) => {

res.writeHead(200);
Wreck.read(req, null, (err, body) => {

expect(err).to.not.exist();
res.end(body);
});
});

server.listen(0, () => {

const req = Wreck.request('post', 'http://localhost:' + server.address().port, { payload: fileStream });

req.once('response', (res) => {

expect(res.statusCode).to.equal(200);

Wreck.read(res, null, (err, body) => {

expect(err).to.not.exist();
expect(body.length).to.equal(stats.size);
server.close();
done();
});
});
});
});
});

describe('parseCacheControl()', () => {
Expand Down