Skip to content

Commit

Permalink
fix: intercept ClientRequest.flushHeaders() (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
ierceg authored and gr2m committed Nov 6, 2017
1 parent 4f83d90 commit 970b037
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/request_overrider.js
Expand Up @@ -159,6 +159,16 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
};

req.flushHeaders = function() {
debug('req.flushHeaders');
if (!aborted && !ended) {
end(cb);
}
if (aborted) {
emitError(new Error('Request aborted'));
}
};

req.abort = function() {
if (aborted) {
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_common.js
Expand Up @@ -157,7 +157,7 @@ tap.test('deleteHeadersField deletes fields with case-insensitive field names',

});

tap.test('matchStringOrRegexp', {only: true}, function (t) {
tap.test('matchStringOrRegexp', function (t) {
t.true(common.matchStringOrRegexp('to match', 'to match'), 'true if pattern is string and target matches');
t.false(common.matchStringOrRegexp('to match', 'not to match'), 'false if pattern is string and target doesn\'t match');

Expand Down
24 changes: 20 additions & 4 deletions tests/test_intercept.js
Expand Up @@ -61,7 +61,7 @@ test("allow unmocked works (2)", function(t) {
});
});

test("allow unmocked works after one interceptor is removed", {only: true}, function(t) {
test("allow unmocked works after one interceptor is removed", function(t) {
nock("https://example.org",{allowUnmocked: true}).
get("/").
reply(200, "Mocked");
Expand All @@ -72,9 +72,6 @@ test("allow unmocked works after one interceptor is removed", {only: true}, func

mikealRequest("https://example.org/unmocked", function(err, resp, body) {
t.error(err);
console.log(`\nbody ==============================`)
console.log(body)

t.assert(~body.indexOf('Example Domain'));
t.end();
});
Expand Down Expand Up @@ -5235,6 +5232,25 @@ test('correctly parse request without specified path (#1003)', function(t) {
}).end();
});

test('data is sent with flushHeaders', function(t) {
nock.cleanAll();

var scope1 = nock('https://example.com')
.get('')
.reply(200, 'this is data');

https.request({hostname: 'example.com'}, function(res) {
t.equal(res.statusCode, 200);
res.on('data', function(data) {
t.equal(data.toString(), 'this is data');
});
res.on('end', function() {
scope1.done();
t.end();
});
}).flushHeaders();
});

test("teardown", function(t) {
var leaks = Object.keys(global)
.splice(globalCount, Number.MAX_VALUE);
Expand Down

0 comments on commit 970b037

Please sign in to comment.