|
| 1 | +// Flags: --expose-http2 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const http2 = require('http2'); |
| 7 | +const body = |
| 8 | + 'Cannot set HTTP/2 pseudo-headers'; |
| 9 | + |
| 10 | +const invalidHeaders = [ |
| 11 | + { key: ':status', value: 200 }, |
| 12 | + { key: ':method', value: 'GET' }, |
| 13 | + { key: ':path', value: '/' }, |
| 14 | + { key: ':authority', value: 'example.com' }, |
| 15 | + { key: ':scheme', value: 'http' } |
| 16 | +]; |
| 17 | + |
| 18 | +const checkServer = (invalidHeader, value) => { |
| 19 | + const server = http2.createServer((req, res) => { |
| 20 | + res.setHeader('foobar', 'baz'); |
| 21 | + res.setHeader('X-POWERED-BY', 'node-test'); |
| 22 | + try { |
| 23 | + res.setHeader(invalidHeader, value); |
| 24 | + } catch (e) { |
| 25 | + res.statusCode = 500; |
| 26 | + res.end(e.message); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + server.listen(0, common.mustCall(() => { |
| 31 | + const client = http2.connect(`http://localhost:${server.address().port}`); |
| 32 | + const headers = { ':path': '/' }; |
| 33 | + const req = client.request(headers); |
| 34 | + req.on('response', common.mustCall((headers) => { |
| 35 | + assert.strictEqual(headers[':status'], 500); |
| 36 | + assert.strictEqual(headers['foobar'], 'baz'); |
| 37 | + assert.strictEqual(headers['x-powered-by'], 'node-test'); |
| 38 | + })); |
| 39 | + |
| 40 | + let data = ''; |
| 41 | + req.on('data', (d) => data += d); |
| 42 | + req.on('end', common.mustCall(() => { |
| 43 | + assert.strictEqual(body, data); |
| 44 | + server.close(); |
| 45 | + client.destroy(); |
| 46 | + })); |
| 47 | + req.end(); |
| 48 | + })); |
| 49 | + server.on('error', common.mustNotCall()); |
| 50 | +}; |
| 51 | + |
| 52 | +invalidHeaders.forEach(({ key, value }) => { |
| 53 | + checkServer(key, value); |
| 54 | +}); |
0 commit comments