Skip to content

Commit

Permalink
Add a getAllHeaders function to the http module
Browse files Browse the repository at this point in the history
Users should not have to access private attributes like res._headers in
order to get all the headers in a request. Define a method getAllHeaders
to return an object which is a copy of re._headers

Fixes issue #3992
  • Loading branch information
iankronquist committed Jan 27, 2015
1 parent b4a670a commit 427fd00
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 71 deletions.
9 changes: 9 additions & 0 deletions doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ Example:

var contentType = response.getHeader('content-type');

### response.getAllHeaders()

Reads out all headers that are already been queued but not yet sent to the
client. This can only be called before headers get implicitly flushed.

Example:

var headers = response.getAllHeaders();

### response.removeHeader(name)

Removes a header that's queued for implicit sending.
Expand Down
8 changes: 8 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ OutgoingMessage.prototype.getHeader = function(name) {
};


OutgoingMessage.prototype.getAllHeaders = function() {
if (!this._headers)
return;
else
return util._extend({}, this._headers);
};


OutgoingMessage.prototype.removeHeader = function(name) {
if (arguments.length < 1) {
throw new Error('`name` is required for removeHeader().');
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-http-header-get-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var common = require('../common');
var assert = require('assert');
var http = require('http');

// Verify that ServerResponse.getHeader() works correctly even after
// the response header has been sent. Issue 752 on github.

var rando = Math.random();
var expected = util._extend({}, {
'X-Random-Thing': rando,
});
var server = http.createServer(function(req, res) {
res.setHeader('X-Random-Thing', rando);
headers = res.getAllHeaders();
res.end('hello');
assert.strictEqual(res.getAllHeaders(), null);
});
server.listen(common.PORT, function() {
http.get({port: common.PORT}, function(resp) {
assert.deepEqual(response.headers, expected);
server.close();
});
});
71 changes: 0 additions & 71 deletions test/simple/test-http-header-get-all.js

This file was deleted.

0 comments on commit 427fd00

Please sign in to comment.