Skip to content

Commit

Permalink
fix `Server shutdown with "Error: Can't set headers after they are se…
Browse files Browse the repository at this point in the history
…nt." ` (close #937)
  • Loading branch information
LavrovArtem committed Nov 15, 2016
1 parent 3d567d1 commit 9202cba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/request-pipeline/destination-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ export default class DestinationRequest extends EventEmitter {
this._send();
}

else if (this._isDNSErr(err))
else if (this._isDNSErr(err)) {
this._abort();
this.emit('fatalError', getText(MESSAGE.cantResolveUrl, this.opts.url));
}

else
this.emit('error');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ test('document.baseURI (GH-920)', function () {
}
};

documentMock.toString.toString = function () {
return 'function test() { [native code] }';
};

strictEqual(getProperty(documentMock, 'baseURI'), url);
});

Expand Down
42 changes: 42 additions & 0 deletions test/server/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ describe('Proxy', function () {
res.end();
});

app.get('/wait/:ms', function (req, res) {
setTimeout(function () {
res.end('text');
}, req.params.ms);
});

destServer = app.listen(2000);


Expand Down Expand Up @@ -1415,5 +1421,41 @@ describe('Proxy', function () {
});
});

it('Should abort destination request after fatal error (GH-937)', function (done) {
var savedReqTimeout = DestinationRequest.TIMEOUT;
var fatalErrorEventCount = 0;

DestinationRequest.TIMEOUT = 100;

var destReq = new DestinationRequest({
url: 'http://127.0.0.1:2000/wait/150',
protocol: 'http:',
hostname: '127.0.0.1',
host: '127.0.0.1:2000',
port: 2000,
path: '/wait/150',
method: 'GET',
body: new Buffer([]),
isXhr: false,
headers: []
});

destReq.on('error', function () {
});
destReq.on('fatalError', function () {
fatalErrorEventCount++;
});

setTimeout(function () {
destReq._onError({ message: 'ECONNREFUSED' });
}, 50);

setTimeout(function () {
DestinationRequest.TIMEOUT = savedReqTimeout;

expect(fatalErrorEventCount).eql(1);
done();
}, 150);
});
});
});

0 comments on commit 9202cba

Please sign in to comment.