http: add response.headersSent property

Boolean property getter. True if headers was sent, false otherwise.
This commit is contained in:
Pavel Lang 2012-09-18 18:26:08 +02:00 committed by Ben Noordhuis
parent ac17dc1764
commit b38277be26
3 changed files with 12 additions and 0 deletions

View File

@ -398,6 +398,10 @@ or
response.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);
### response.headersSent
Boolean (read-only). True if headers were sent, false otherwise.
### response.sendDate
When true, the Date header will be automatically generated and sent in

View File

@ -699,6 +699,12 @@ OutgoingMessage.prototype._renderHeaders = function() {
};
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
configurable: true,
enumerable: true,
get: function () { return !!this._header; }
});
OutgoingMessage.prototype.write = function(chunk, encoding) {
if (!this._header) {

View File

@ -30,7 +30,9 @@ var s = http.createServer(function(req, res) {
var contentType = 'Content-Type';
var plain = 'text/plain';
res.setHeader(contentType, plain);
assert.ok(!res.headersSent);
res.writeHead(200);
assert.ok(res.headersSent);
res.end('hello world\n');
// This checks that after the headers have been sent, getHeader works
// and does not throw an exception (Issue 752)