|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | +const assert = require('assert'); |
| 7 | +const http2 = require('http2'); |
| 8 | +const body = |
| 9 | + '<html><head></head><body><h1>this is some data</h2></body></html>'; |
| 10 | + |
| 11 | +// Starts a server with the given request handler, sends one request and |
| 12 | +// calls onResponse with the response headers. |
| 13 | +function testResponse(onRequest, onResponse) { |
| 14 | + const server = http2.createServer(onRequest); |
| 15 | + server.on('error', common.mustNotCall()); |
| 16 | + |
| 17 | + server.listen(0, common.mustCall(() => { |
| 18 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 19 | + const req = client.request({ ':path': '/' }); |
| 20 | + req.setEncoding('utf8'); |
| 21 | + req.on('response', onResponse); |
| 22 | + |
| 23 | + let data = ''; |
| 24 | + req.on('data', (d) => data += d); |
| 25 | + req.on('end', common.mustCall(() => { |
| 26 | + assert.strictEqual(data, body); |
| 27 | + server.close(); |
| 28 | + client.close(); |
| 29 | + })); |
| 30 | + req.end(); |
| 31 | + })); |
| 32 | +} |
| 33 | + |
| 34 | +// Map, including a header name that is not lower case and a connection |
| 35 | +// header, which is dropped with a warning. |
| 36 | +{ |
| 37 | + const compatMsg = 'The provided connection header is not valid, ' + |
| 38 | + 'the value will be dropped from the header and ' + |
| 39 | + 'will never be in use.'; |
| 40 | + common.expectWarning('UnsupportedWarning', compatMsg); |
| 41 | + |
| 42 | + testResponse(common.mustCall((req, res) => { |
| 43 | + res.setHeaders(new Map([ |
| 44 | + ['foobar', 'baz'], |
| 45 | + ['X-POWERED-BY', 'node-test'], |
| 46 | + ['connection', 'connection-test'], |
| 47 | + ])); |
| 48 | + res.end(body); |
| 49 | + }), common.mustCall((headers) => { |
| 50 | + assert.strictEqual(headers[':status'], 200); |
| 51 | + assert.strictEqual(headers.foobar, 'baz'); |
| 52 | + assert.strictEqual(headers['x-powered-by'], 'node-test'); |
| 53 | + assert.strictEqual(headers.connection, undefined); |
| 54 | + })); |
| 55 | +} |
| 56 | + |
| 57 | +// Headers instance. |
| 58 | +{ |
| 59 | + testResponse(common.mustCall((req, res) => { |
| 60 | + res.setHeaders(new Headers({ foo: '1', bar: '2' })); |
| 61 | + res.writeHead(200); |
| 62 | + res.end(body); |
| 63 | + }), common.mustCall((headers) => { |
| 64 | + assert.strictEqual(headers[':status'], 200); |
| 65 | + assert.strictEqual(headers.foo, '1'); |
| 66 | + assert.strictEqual(headers.bar, '2'); |
| 67 | + })); |
| 68 | +} |
| 69 | + |
| 70 | +// An existing header is replaced. |
| 71 | +{ |
| 72 | + testResponse(common.mustCall((req, res) => { |
| 73 | + res.setHeader('foo', '1'); |
| 74 | + res.setHeader('bar', '2'); |
| 75 | + res.setHeaders(new Headers({ foo: '3' })); |
| 76 | + res.end(body); |
| 77 | + }), common.mustCall((headers) => { |
| 78 | + assert.strictEqual(headers.foo, '3'); |
| 79 | + assert.strictEqual(headers.bar, '2'); |
| 80 | + })); |
| 81 | +} |
| 82 | + |
| 83 | +// Headers passed to writeHead() as an object take precedence. |
| 84 | +{ |
| 85 | + testResponse(common.mustCall((req, res) => { |
| 86 | + res.setHeaders(new Headers({ foo: '1', bar: '2' })); |
| 87 | + res.writeHead(200, { foo: '3' }); |
| 88 | + res.end(body); |
| 89 | + }), common.mustCall((headers) => { |
| 90 | + assert.strictEqual(headers[':status'], 200); |
| 91 | + assert.strictEqual(headers.foo, '3'); |
| 92 | + assert.strictEqual(headers.bar, '2'); |
| 93 | + })); |
| 94 | +} |
| 95 | + |
| 96 | +// Headers passed to writeHead() as an array take precedence. |
| 97 | +{ |
| 98 | + testResponse(common.mustCall((req, res) => { |
| 99 | + res.setHeaders(new Headers({ foo: '1', bar: '2' })); |
| 100 | + res.writeHead(200, ['foo', '3']); |
| 101 | + res.end(body); |
| 102 | + }), common.mustCall((headers) => { |
| 103 | + assert.strictEqual(headers[':status'], 200); |
| 104 | + assert.strictEqual(headers.foo, '3'); |
| 105 | + assert.strictEqual(headers.bar, '2'); |
| 106 | + })); |
| 107 | +} |
| 108 | + |
| 109 | +// Multiple set-cookie values appended to a Headers instance. |
| 110 | +{ |
| 111 | + testResponse(common.mustCall((req, res) => { |
| 112 | + const headers = new Headers(); |
| 113 | + headers.append('Set-Cookie', 'a=b'); |
| 114 | + headers.append('Set-Cookie', 'c=d'); |
| 115 | + res.setHeaders(headers); |
| 116 | + res.end(body); |
| 117 | + }), common.mustCall((headers) => { |
| 118 | + assert.deepStrictEqual(headers['set-cookie'], ['a=b', 'c=d']); |
| 119 | + })); |
| 120 | +} |
| 121 | + |
| 122 | +// Multiple set-cookie values as an array in a Map, with a name that is not |
| 123 | +// lower case. |
| 124 | +{ |
| 125 | + testResponse(common.mustCall((req, res) => { |
| 126 | + res.setHeaders(new Map([['Set-Cookie', ['a=b', 'c=d']]])); |
| 127 | + res.end(body); |
| 128 | + }), common.mustCall((headers) => { |
| 129 | + assert.deepStrictEqual(headers['set-cookie'], ['a=b', 'c=d']); |
| 130 | + })); |
| 131 | +} |
| 132 | + |
| 133 | +// Lower case set-cookie in a Map, as an array and as a single value. |
| 134 | +{ |
| 135 | + testResponse(common.mustCall((req, res) => { |
| 136 | + res.setHeaders(new Map([['set-cookie', ['a=b', 'c=d']]])); |
| 137 | + res.end(body); |
| 138 | + }), common.mustCall((headers) => { |
| 139 | + assert.deepStrictEqual(headers['set-cookie'], ['a=b', 'c=d']); |
| 140 | + })); |
| 141 | + |
| 142 | + testResponse(common.mustCall((req, res) => { |
| 143 | + res.setHeaders(new Map([['set-cookie', 'a=b']])); |
| 144 | + res.end(body); |
| 145 | + }), common.mustCall((headers) => { |
| 146 | + assert.deepStrictEqual(headers['set-cookie'], ['a=b']); |
| 147 | + })); |
| 148 | +} |
| 149 | + |
| 150 | +// set-cookie values replace the ones set before. |
| 151 | +{ |
| 152 | + testResponse(common.mustCall((req, res) => { |
| 153 | + res.setHeader('set-cookie', ['x=y']); |
| 154 | + const headers = new Headers(); |
| 155 | + headers.append('Set-Cookie', 'a=b'); |
| 156 | + headers.append('Set-Cookie', 'c=d'); |
| 157 | + res.setHeaders(headers); |
| 158 | + res.end(body); |
| 159 | + }), common.mustCall((headers) => { |
| 160 | + assert.deepStrictEqual(headers['set-cookie'], ['a=b', 'c=d']); |
| 161 | + })); |
| 162 | +} |
| 163 | + |
| 164 | +// Throws when the headers have already been sent. |
| 165 | +{ |
| 166 | + testResponse(common.mustCall((req, res) => { |
| 167 | + res.writeHead(200); |
| 168 | + assert.throws(() => { |
| 169 | + res.setHeaders(new Headers({ foo: '1' })); |
| 170 | + }, { |
| 171 | + code: 'ERR_HTTP2_HEADERS_SENT', |
| 172 | + name: 'Error', |
| 173 | + }); |
| 174 | + res.end(body); |
| 175 | + }), common.mustCall((headers) => { |
| 176 | + assert.strictEqual(headers.foo, undefined); |
| 177 | + })); |
| 178 | +} |
| 179 | + |
| 180 | +// Throws when headers is neither a Headers instance nor a Map. |
| 181 | +{ |
| 182 | + testResponse(common.mustCall((req, res) => { |
| 183 | + for (const headers of [ |
| 184 | + ['foo', '1'], |
| 185 | + { foo: '1' }, |
| 186 | + null, |
| 187 | + undefined, |
| 188 | + 'test', |
| 189 | + 1, |
| 190 | + { keys() {} }, |
| 191 | + { get() {} }, |
| 192 | + ]) { |
| 193 | + assert.throws(() => { |
| 194 | + res.setHeaders(headers); |
| 195 | + }, { |
| 196 | + code: 'ERR_INVALID_ARG_TYPE', |
| 197 | + name: 'TypeError', |
| 198 | + }); |
| 199 | + } |
| 200 | + res.end(body); |
| 201 | + }), common.mustCall((headers) => { |
| 202 | + assert.strictEqual(headers.foo, undefined); |
| 203 | + })); |
| 204 | +} |
0 commit comments