2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2017-01-28 00:49:02 +09:00
|
|
|
const common = require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
2014-02-10 19:56:09 +01:00
|
|
|
|
|
|
|
// Verify that ServerResponse.writeHead() works as setHeader.
|
|
|
|
// Issue 5036 on github.
|
|
|
|
|
2017-01-28 00:49:02 +09:00
|
|
|
const s = http.createServer(common.mustCall((req, res) => {
|
2014-02-10 19:56:09 +01:00
|
|
|
res.setHeader('test', '1');
|
2014-09-29 12:32:42 -07:00
|
|
|
|
|
|
|
// toLowerCase() is used on the name argument, so it must be a string.
|
2017-01-08 13:19:00 +00:00
|
|
|
let threw = false;
|
2014-09-29 12:32:42 -07:00
|
|
|
try {
|
|
|
|
res.setHeader(0xf00, 'bar');
|
|
|
|
} catch (e) {
|
|
|
|
assert.ok(e instanceof TypeError);
|
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
assert.ok(threw, 'Non-string names should throw');
|
|
|
|
|
2015-02-26 10:36:40 -08:00
|
|
|
// undefined value should throw, via 979d0ca8
|
|
|
|
threw = false;
|
|
|
|
try {
|
|
|
|
res.setHeader('foo', undefined);
|
|
|
|
} catch (e) {
|
|
|
|
assert.ok(e instanceof Error);
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(e.message,
|
|
|
|
'"value" required in setHeader("foo", value)');
|
2015-02-26 10:36:40 -08:00
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
assert.ok(threw, 'Undefined value should throw');
|
|
|
|
|
2014-02-10 19:56:09 +01:00
|
|
|
res.writeHead(200, { Test: '2' });
|
2017-01-28 00:49:02 +09:00
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
res.writeHead(100, {});
|
|
|
|
}, /^Error: Can't render headers after they are sent to the client$/);
|
|
|
|
|
2014-02-10 19:56:09 +01:00
|
|
|
res.end();
|
2017-01-28 00:49:02 +09:00
|
|
|
}));
|
2014-02-10 19:56:09 +01:00
|
|
|
|
2017-01-28 00:49:02 +09:00
|
|
|
s.listen(0, common.mustCall(runTest));
|
2014-02-10 19:56:09 +01:00
|
|
|
|
|
|
|
function runTest() {
|
2017-01-28 00:49:02 +09:00
|
|
|
http.get({ port: this.address().port }, common.mustCall((response) => {
|
|
|
|
response.on('end', common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(response.headers['test'], '2');
|
2016-09-17 12:32:33 +02:00
|
|
|
assert.notStrictEqual(response.rawHeaders.indexOf('Test'), -1);
|
2014-02-10 19:56:09 +01:00
|
|
|
s.close();
|
2017-01-28 00:49:02 +09:00
|
|
|
}));
|
2014-02-10 19:56:09 +01:00
|
|
|
response.resume();
|
2017-01-28 00:49:02 +09:00
|
|
|
}));
|
2014-02-10 19:56:09 +01:00
|
|
|
}
|