Browse Source

http: add response.headersSent property

Boolean property getter. True if headers was sent, false otherwise.
v0.9.3-release
Pavel Lang 12 years ago
committed by Ben Noordhuis
parent
commit
b38277be26
  1. 4
      doc/api/http.markdown
  2. 6
      lib/http.js
  3. 2
      test/simple/test-http-header-read.js

4
doc/api/http.markdown

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

6
lib/http.js

@ -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) { OutgoingMessage.prototype.write = function(chunk, encoding) {
if (!this._header) { if (!this._header) {

2
test/simple/test-http-header-read.js

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

Loading…
Cancel
Save