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

Implement callback on req.write and req.end #1047

Merged
merged 1 commit into from
Jan 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,20 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

req.socket = response.socket = Socket({ proto: options.proto });

req.write = function(buffer, encoding) {
req.write = function(buffer, encoding, callback) {
debug('write', arguments);
if (buffer && !aborted) {
if (! Buffer.isBuffer(buffer)) {
buffer = new Buffer(buffer, encoding);
if (!aborted) {
if (buffer) {
if (!Buffer.isBuffer(buffer)) {
buffer = new Buffer(buffer, encoding);
}
requestBodyBuffers.push(buffer);
}
if (typeof callback === 'function') {
callback();
}
requestBodyBuffers.push(buffer);
}
if (aborted) {
else {
emitError(new Error('Request aborted'));
}

Expand All @@ -146,13 +151,17 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
return false;
};

req.end = function(buffer, encoding) {
req.end = function(buffer, encoding, callback) {
debug('req.end');
if (!aborted && !ended) {
req.write(buffer, encoding);
end(cb);
req.emit('finish');
req.emit('end');
req.write(buffer, encoding, function () {
if (typeof callback === 'function') {
callback();
}
end(cb);
req.emit('finish');
req.emit('end');
});
}
if (aborted) {
emitError(new Error('Request aborted'));
Expand Down
59 changes: 59 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -3297,6 +3297,65 @@ test('delay works with replyWithError', function (t) {

});

test("write callback called", function(t) {
var scope = nock('http://www.filterboddiezregexp.com')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply(200, "Hello World!");

var callbackCalled = false;
var req = http.request({
host: "www.filterboddiezregexp.com"
, method: 'POST'
, path: '/'
, port: 80
}, function(res) {
t.equal(callbackCalled, true);
t.equal(res.statusCode, 200);
res.on('end', function() {
scope.done();
t.end();
});
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume();
});

req.write('mamma mia', null, function() {
callbackCalled = true;
req.end();
});
});

test("end callback called", function(t) {
var scope = nock('http://www.filterboddiezregexp.com')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply(200, "Hello World!");

var callbackCalled = false;
var req = http.request({
host: "www.filterboddiezregexp.com"
, method: 'POST'
, path: '/'
, port: 80
}, function(res) {
t.equal(callbackCalled, true);
t.equal(res.statusCode, 200);
res.on('end', function() {
scope.done();
t.end();
});
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume();
});

req.end('mamma mia', null, function() {
callbackCalled = true;
});
});

test("finish event fired before end event (bug-139)", function(t) {
var scope = nock('http://www.filterboddiezregexp.com')
.filteringRequestBody(/mia/, 'nostra')
Expand Down