Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all headers 3992 #170

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});