Skip to content

Commit b4a670a

Browse files
committed
Add test for issue 3992
Test a getAllHeaders function. It should return an object representing the headers in a request.
1 parent ef10827 commit b4a670a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var http = require('http');
24+
25+
// Verify that ServerResponse.getHeader() works correctly even after
26+
// the response header has been sent. Issue 752 on github.
27+
28+
var s = http.createServer(function(req, res) {
29+
var contentType = 'Content-Type';
30+
var plain = 'text/plain';
31+
var userAgent = 'User-Agent';
32+
var browser = 'Mozilla';
33+
var expected = {
34+
'content-type': plain,
35+
'user-agent': browser
36+
}
37+
res.setHeader(contentType, plain);
38+
res.setHeader(userAgent, browser);
39+
assert.ok(!res.headersSent);
40+
res.writeHead(200);
41+
assert.ok(res.headersSent);
42+
res.end('hello world\n');
43+
var headers;
44+
// This checks that after the headers have been sent, getAllHeaders works
45+
// and does not throw an exception
46+
assert.doesNotThrow(
47+
function() {
48+
headers = res.getAllHeaders();
49+
}
50+
);
51+
// Check that all fields in headers have the same value as expected, and
52+
// vice-versa. This ensures that there no fields which *don't* exist in one
53+
// or the other.
54+
for (var field in headers) {
55+
assert.equal(headers[field], expected[field]);
56+
}
57+
for (var field in expected) {
58+
assert.equal(headers[field], expected[field]);
59+
}
60+
});
61+
62+
s.listen(process.env.PORT, runTest);
63+
64+
function runTest() {
65+
http.get({ port: process.env.PORT }, function(response) {
66+
response.on('end', function() {
67+
s.close();
68+
});
69+
response.resume();
70+
});
71+
}

0 commit comments

Comments
 (0)