11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) 51cb0ef41Sopenharmony_ci common.skip('missing crypto'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst h2 = require('http2'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci{ 101cb0ef41Sopenharmony_ci // Http2ServerRequest should have header helpers 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci const server = h2.createServer(); 131cb0ef41Sopenharmony_ci server.listen(0, common.mustCall(function() { 141cb0ef41Sopenharmony_ci const port = server.address().port; 151cb0ef41Sopenharmony_ci server.once('request', common.mustCall(function(request, response) { 161cb0ef41Sopenharmony_ci const expected = { 171cb0ef41Sopenharmony_ci ':path': '/foobar', 181cb0ef41Sopenharmony_ci ':method': 'GET', 191cb0ef41Sopenharmony_ci ':scheme': 'http', 201cb0ef41Sopenharmony_ci ':authority': `localhost:${port}`, 211cb0ef41Sopenharmony_ci 'foo-bar': 'abc123' 221cb0ef41Sopenharmony_ci }; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci assert.strictEqual(request.path, undefined); 251cb0ef41Sopenharmony_ci assert.strictEqual(request.method, expected[':method']); 261cb0ef41Sopenharmony_ci assert.strictEqual(request.scheme, expected[':scheme']); 271cb0ef41Sopenharmony_ci assert.strictEqual(request.url, expected[':path']); 281cb0ef41Sopenharmony_ci assert.strictEqual(request.authority, expected[':authority']); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci const headers = request.headers; 311cb0ef41Sopenharmony_ci for (const [name, value] of Object.entries(expected)) { 321cb0ef41Sopenharmony_ci assert.strictEqual(headers[name], value); 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci const rawHeaders = request.rawHeaders; 361cb0ef41Sopenharmony_ci for (const [name, value] of Object.entries(expected)) { 371cb0ef41Sopenharmony_ci const position = rawHeaders.indexOf(name); 381cb0ef41Sopenharmony_ci assert.notStrictEqual(position, -1); 391cb0ef41Sopenharmony_ci assert.strictEqual(rawHeaders[position + 1], value); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci request.url = '/one'; 431cb0ef41Sopenharmony_ci assert.strictEqual(request.url, '/one'); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci // Third-party plugins for packages like express use query params to 461cb0ef41Sopenharmony_ci // change the request method 471cb0ef41Sopenharmony_ci request.method = 'POST'; 481cb0ef41Sopenharmony_ci assert.strictEqual(request.method, 'POST'); 491cb0ef41Sopenharmony_ci assert.throws( 501cb0ef41Sopenharmony_ci () => request.method = ' ', 511cb0ef41Sopenharmony_ci { 521cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 531cb0ef41Sopenharmony_ci name: 'TypeError', 541cb0ef41Sopenharmony_ci message: "The argument 'method' is invalid. Received ' '" 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci ); 571cb0ef41Sopenharmony_ci assert.throws( 581cb0ef41Sopenharmony_ci () => request.method = true, 591cb0ef41Sopenharmony_ci { 601cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 611cb0ef41Sopenharmony_ci name: 'TypeError', 621cb0ef41Sopenharmony_ci message: 'The "method" argument must be of type string. ' + 631cb0ef41Sopenharmony_ci 'Received type boolean (true)' 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci ); 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci response.on('finish', common.mustCall(function() { 681cb0ef41Sopenharmony_ci server.close(); 691cb0ef41Sopenharmony_ci })); 701cb0ef41Sopenharmony_ci response.end(); 711cb0ef41Sopenharmony_ci })); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci const url = `http://localhost:${port}`; 741cb0ef41Sopenharmony_ci const client = h2.connect(url, common.mustCall(function() { 751cb0ef41Sopenharmony_ci const headers = { 761cb0ef41Sopenharmony_ci ':path': '/foobar', 771cb0ef41Sopenharmony_ci ':method': 'GET', 781cb0ef41Sopenharmony_ci ':scheme': 'http', 791cb0ef41Sopenharmony_ci ':authority': `localhost:${port}`, 801cb0ef41Sopenharmony_ci 'foo-bar': 'abc123' 811cb0ef41Sopenharmony_ci }; 821cb0ef41Sopenharmony_ci const request = client.request(headers); 831cb0ef41Sopenharmony_ci request.on('end', common.mustCall(function() { 841cb0ef41Sopenharmony_ci client.close(); 851cb0ef41Sopenharmony_ci })); 861cb0ef41Sopenharmony_ci request.end(); 871cb0ef41Sopenharmony_ci request.resume(); 881cb0ef41Sopenharmony_ci })); 891cb0ef41Sopenharmony_ci })); 901cb0ef41Sopenharmony_ci} 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci{ 931cb0ef41Sopenharmony_ci // Http2ServerRequest should keep pseudo-headers order and after them, 941cb0ef41Sopenharmony_ci // in the same order, the others headers 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci const server = h2.createServer(); 971cb0ef41Sopenharmony_ci server.listen(0, common.mustCall(function() { 981cb0ef41Sopenharmony_ci const port = server.address().port; 991cb0ef41Sopenharmony_ci server.once('request', common.mustCall(function(request, response) { 1001cb0ef41Sopenharmony_ci const expected = { 1011cb0ef41Sopenharmony_ci ':path': '/foobar', 1021cb0ef41Sopenharmony_ci ':method': 'GET', 1031cb0ef41Sopenharmony_ci ':scheme': 'http', 1041cb0ef41Sopenharmony_ci ':authority': `localhost:${port}`, 1051cb0ef41Sopenharmony_ci 'foo1': 'abc1', 1061cb0ef41Sopenharmony_ci 'foo2': 'abc2' 1071cb0ef41Sopenharmony_ci }; 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ci assert.strictEqual(request.path, undefined); 1101cb0ef41Sopenharmony_ci assert.strictEqual(request.method, expected[':method']); 1111cb0ef41Sopenharmony_ci assert.strictEqual(request.scheme, expected[':scheme']); 1121cb0ef41Sopenharmony_ci assert.strictEqual(request.url, expected[':path']); 1131cb0ef41Sopenharmony_ci assert.strictEqual(request.authority, expected[':authority']); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci const headers = request.headers; 1161cb0ef41Sopenharmony_ci for (const [name, value] of Object.entries(expected)) { 1171cb0ef41Sopenharmony_ci assert.strictEqual(headers[name], value); 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci const rawHeaders = request.rawHeaders; 1211cb0ef41Sopenharmony_ci let expectedPosition = 0; 1221cb0ef41Sopenharmony_ci for (const [name, value] of Object.entries(expected)) { 1231cb0ef41Sopenharmony_ci const position = rawHeaders.indexOf(name); 1241cb0ef41Sopenharmony_ci assert.strictEqual(position / 2, expectedPosition); 1251cb0ef41Sopenharmony_ci assert.strictEqual(rawHeaders[position + 1], value); 1261cb0ef41Sopenharmony_ci expectedPosition++; 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci response.on('finish', common.mustCall(function() { 1301cb0ef41Sopenharmony_ci server.close(); 1311cb0ef41Sopenharmony_ci })); 1321cb0ef41Sopenharmony_ci response.end(); 1331cb0ef41Sopenharmony_ci })); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci const url = `http://localhost:${port}`; 1361cb0ef41Sopenharmony_ci const client = h2.connect(url, common.mustCall(function() { 1371cb0ef41Sopenharmony_ci const headers = { 1381cb0ef41Sopenharmony_ci ':path': '/foobar', 1391cb0ef41Sopenharmony_ci ':method': 'GET', 1401cb0ef41Sopenharmony_ci 'foo1': 'abc1', 1411cb0ef41Sopenharmony_ci ':scheme': 'http', 1421cb0ef41Sopenharmony_ci 'foo2': 'abc2', 1431cb0ef41Sopenharmony_ci ':authority': `localhost:${port}` 1441cb0ef41Sopenharmony_ci }; 1451cb0ef41Sopenharmony_ci const request = client.request(headers); 1461cb0ef41Sopenharmony_ci request.on('end', common.mustCall(function() { 1471cb0ef41Sopenharmony_ci client.close(); 1481cb0ef41Sopenharmony_ci })); 1491cb0ef41Sopenharmony_ci request.end(); 1501cb0ef41Sopenharmony_ci request.resume(); 1511cb0ef41Sopenharmony_ci })); 1521cb0ef41Sopenharmony_ci })); 1531cb0ef41Sopenharmony_ci} 154