11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci// Tests the internal utility functions that are used to prepare headers 51cb0ef41Sopenharmony_ci// to pass to the internal binding layer and to build a header object. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst common = require('../common'); 81cb0ef41Sopenharmony_ciif (!common.hasCrypto) 91cb0ef41Sopenharmony_ci common.skip('missing crypto'); 101cb0ef41Sopenharmony_ciconst assert = require('assert'); 111cb0ef41Sopenharmony_ciconst { 121cb0ef41Sopenharmony_ci getAuthority, 131cb0ef41Sopenharmony_ci mapToHeaders, 141cb0ef41Sopenharmony_ci toHeaderObject 151cb0ef41Sopenharmony_ci} = require('internal/http2/util'); 161cb0ef41Sopenharmony_ciconst { sensitiveHeaders } = require('http2'); 171cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding'); 181cb0ef41Sopenharmony_ciconst { 191cb0ef41Sopenharmony_ci HTTP2_HEADER_STATUS, 201cb0ef41Sopenharmony_ci HTTP2_HEADER_METHOD, 211cb0ef41Sopenharmony_ci HTTP2_HEADER_AUTHORITY, 221cb0ef41Sopenharmony_ci HTTP2_HEADER_SCHEME, 231cb0ef41Sopenharmony_ci HTTP2_HEADER_PATH, 241cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS, 251cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_MAX_AGE, 261cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_REQUEST_METHOD, 271cb0ef41Sopenharmony_ci HTTP2_HEADER_AGE, 281cb0ef41Sopenharmony_ci HTTP2_HEADER_AUTHORIZATION, 291cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_ENCODING, 301cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_LANGUAGE, 311cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_LENGTH, 321cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_LOCATION, 331cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_MD5, 341cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_RANGE, 351cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_TYPE, 361cb0ef41Sopenharmony_ci HTTP2_HEADER_DATE, 371cb0ef41Sopenharmony_ci HTTP2_HEADER_DNT, 381cb0ef41Sopenharmony_ci HTTP2_HEADER_ETAG, 391cb0ef41Sopenharmony_ci HTTP2_HEADER_EXPIRES, 401cb0ef41Sopenharmony_ci HTTP2_HEADER_FROM, 411cb0ef41Sopenharmony_ci HTTP2_HEADER_HOST, 421cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_MATCH, 431cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_MODIFIED_SINCE, 441cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_NONE_MATCH, 451cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_RANGE, 461cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_UNMODIFIED_SINCE, 471cb0ef41Sopenharmony_ci HTTP2_HEADER_LAST_MODIFIED, 481cb0ef41Sopenharmony_ci HTTP2_HEADER_LOCATION, 491cb0ef41Sopenharmony_ci HTTP2_HEADER_MAX_FORWARDS, 501cb0ef41Sopenharmony_ci HTTP2_HEADER_PROXY_AUTHORIZATION, 511cb0ef41Sopenharmony_ci HTTP2_HEADER_RANGE, 521cb0ef41Sopenharmony_ci HTTP2_HEADER_REFERER, 531cb0ef41Sopenharmony_ci HTTP2_HEADER_RETRY_AFTER, 541cb0ef41Sopenharmony_ci HTTP2_HEADER_TK, 551cb0ef41Sopenharmony_ci HTTP2_HEADER_UPGRADE_INSECURE_REQUESTS, 561cb0ef41Sopenharmony_ci HTTP2_HEADER_USER_AGENT, 571cb0ef41Sopenharmony_ci HTTP2_HEADER_X_CONTENT_TYPE_OPTIONS, 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_CHARSET, 601cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_ENCODING, 611cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_LANGUAGE, 621cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_RANGES, 631cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT, 641cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_HEADERS, 651cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_METHODS, 661cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, 671cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS, 681cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_REQUEST_HEADERS, 691cb0ef41Sopenharmony_ci HTTP2_HEADER_ALLOW, 701cb0ef41Sopenharmony_ci HTTP2_HEADER_CACHE_CONTROL, 711cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_DISPOSITION, 721cb0ef41Sopenharmony_ci HTTP2_HEADER_COOKIE, 731cb0ef41Sopenharmony_ci HTTP2_HEADER_EXPECT, 741cb0ef41Sopenharmony_ci HTTP2_HEADER_FORWARDED, 751cb0ef41Sopenharmony_ci HTTP2_HEADER_LINK, 761cb0ef41Sopenharmony_ci HTTP2_HEADER_PREFER, 771cb0ef41Sopenharmony_ci HTTP2_HEADER_PROXY_AUTHENTICATE, 781cb0ef41Sopenharmony_ci HTTP2_HEADER_REFRESH, 791cb0ef41Sopenharmony_ci HTTP2_HEADER_SERVER, 801cb0ef41Sopenharmony_ci HTTP2_HEADER_SET_COOKIE, 811cb0ef41Sopenharmony_ci HTTP2_HEADER_STRICT_TRANSPORT_SECURITY, 821cb0ef41Sopenharmony_ci HTTP2_HEADER_TRAILER, 831cb0ef41Sopenharmony_ci HTTP2_HEADER_VARY, 841cb0ef41Sopenharmony_ci HTTP2_HEADER_VIA, 851cb0ef41Sopenharmony_ci HTTP2_HEADER_WARNING, 861cb0ef41Sopenharmony_ci HTTP2_HEADER_WWW_AUTHENTICATE, 871cb0ef41Sopenharmony_ci HTTP2_HEADER_X_FRAME_OPTIONS, 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci HTTP2_HEADER_CONNECTION, 901cb0ef41Sopenharmony_ci HTTP2_HEADER_UPGRADE, 911cb0ef41Sopenharmony_ci HTTP2_HEADER_HTTP2_SETTINGS, 921cb0ef41Sopenharmony_ci HTTP2_HEADER_TE, 931cb0ef41Sopenharmony_ci HTTP2_HEADER_TRANSFER_ENCODING, 941cb0ef41Sopenharmony_ci HTTP2_HEADER_KEEP_ALIVE, 951cb0ef41Sopenharmony_ci HTTP2_HEADER_PROXY_CONNECTION 961cb0ef41Sopenharmony_ci} = internalBinding('http2').constants; 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci{ 991cb0ef41Sopenharmony_ci const headers = { 1001cb0ef41Sopenharmony_ci 'abc': 1, 1011cb0ef41Sopenharmony_ci ':path': 'abc', 1021cb0ef41Sopenharmony_ci ':status': 200, 1031cb0ef41Sopenharmony_ci 'xyz': [1, '2', { toString() { return '3'; } }, 4], 1041cb0ef41Sopenharmony_ci 'foo': [], 1051cb0ef41Sopenharmony_ci 'BAR': [1] 1061cb0ef41Sopenharmony_ci }; 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1091cb0ef41Sopenharmony_ci mapToHeaders(headers), 1101cb0ef41Sopenharmony_ci [ [ ':path', 'abc\0', ':status', '200\0', 'abc', '1\0', 'xyz', '1\0', 1111cb0ef41Sopenharmony_ci 'xyz', '2\0', 'xyz', '3\0', 'xyz', '4\0', 'bar', '1\0', '' ].join('\0'), 1121cb0ef41Sopenharmony_ci 8 ] 1131cb0ef41Sopenharmony_ci ); 1141cb0ef41Sopenharmony_ci} 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci{ 1171cb0ef41Sopenharmony_ci const headers = { 1181cb0ef41Sopenharmony_ci 'abc': 1, 1191cb0ef41Sopenharmony_ci ':status': [200], 1201cb0ef41Sopenharmony_ci ':path': 'abc', 1211cb0ef41Sopenharmony_ci ':authority': [], 1221cb0ef41Sopenharmony_ci 'xyz': [1, 2, 3, 4] 1231cb0ef41Sopenharmony_ci }; 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1261cb0ef41Sopenharmony_ci mapToHeaders(headers), 1271cb0ef41Sopenharmony_ci [ [ ':status', '200\0', ':path', 'abc\0', 'abc', '1\0', 'xyz', '1\0', 1281cb0ef41Sopenharmony_ci 'xyz', '2\0', 'xyz', '3\0', 'xyz', '4\0', '' ].join('\0'), 7 ] 1291cb0ef41Sopenharmony_ci ); 1301cb0ef41Sopenharmony_ci} 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci{ 1331cb0ef41Sopenharmony_ci const headers = { 1341cb0ef41Sopenharmony_ci 'abc': 1, 1351cb0ef41Sopenharmony_ci ':status': 200, 1361cb0ef41Sopenharmony_ci 'xyz': [1, 2, 3, 4], 1371cb0ef41Sopenharmony_ci '': 1, 1381cb0ef41Sopenharmony_ci ':path': 'abc', 1391cb0ef41Sopenharmony_ci [Symbol('test')]: 1 // Symbol keys are ignored 1401cb0ef41Sopenharmony_ci }; 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1431cb0ef41Sopenharmony_ci mapToHeaders(headers), 1441cb0ef41Sopenharmony_ci [ [ ':status', '200\0', ':path', 'abc\0', 'abc', '1\0', 'xyz', '1\0', 1451cb0ef41Sopenharmony_ci 'xyz', '2\0', 'xyz', '3\0', 'xyz', '4\0', '' ].join('\0'), 7 ] 1461cb0ef41Sopenharmony_ci ); 1471cb0ef41Sopenharmony_ci} 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci{ 1501cb0ef41Sopenharmony_ci // Only own properties are used 1511cb0ef41Sopenharmony_ci const base = { 'abc': 1 }; 1521cb0ef41Sopenharmony_ci const headers = Object.create(base); 1531cb0ef41Sopenharmony_ci headers[':status'] = 200; 1541cb0ef41Sopenharmony_ci headers.xyz = [1, 2, 3, 4]; 1551cb0ef41Sopenharmony_ci headers.foo = []; 1561cb0ef41Sopenharmony_ci headers[':path'] = 'abc'; 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1591cb0ef41Sopenharmony_ci mapToHeaders(headers), 1601cb0ef41Sopenharmony_ci [ [ ':status', '200\0', ':path', 'abc\0', 'xyz', '1\0', 'xyz', '2\0', 1611cb0ef41Sopenharmony_ci 'xyz', '3\0', 'xyz', '4\0', '' ].join('\0'), 6 ] 1621cb0ef41Sopenharmony_ci ); 1631cb0ef41Sopenharmony_ci} 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci{ 1661cb0ef41Sopenharmony_ci // Arrays containing a single set-cookie value are handled correctly 1671cb0ef41Sopenharmony_ci // (https://github.com/nodejs/node/issues/16452) 1681cb0ef41Sopenharmony_ci const headers = { 1691cb0ef41Sopenharmony_ci 'set-cookie': ['foo=bar'] 1701cb0ef41Sopenharmony_ci }; 1711cb0ef41Sopenharmony_ci assert.deepStrictEqual( 1721cb0ef41Sopenharmony_ci mapToHeaders(headers), 1731cb0ef41Sopenharmony_ci [ [ 'set-cookie', 'foo=bar\0', '' ].join('\0'), 1 ] 1741cb0ef41Sopenharmony_ci ); 1751cb0ef41Sopenharmony_ci} 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci{ 1781cb0ef41Sopenharmony_ci // pseudo-headers are only allowed a single value 1791cb0ef41Sopenharmony_ci const headers = { 1801cb0ef41Sopenharmony_ci ':status': 200, 1811cb0ef41Sopenharmony_ci ':statuS': 204, 1821cb0ef41Sopenharmony_ci }; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci assert.throws(() => mapToHeaders(headers), { 1851cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_HEADER_SINGLE_VALUE', 1861cb0ef41Sopenharmony_ci name: 'TypeError', 1871cb0ef41Sopenharmony_ci message: 'Header field ":status" must only have a single value' 1881cb0ef41Sopenharmony_ci }); 1891cb0ef41Sopenharmony_ci} 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci{ 1921cb0ef41Sopenharmony_ci const headers = { 1931cb0ef41Sopenharmony_ci 'abc': 1, 1941cb0ef41Sopenharmony_ci ':status': [200], 1951cb0ef41Sopenharmony_ci ':path': 'abc', 1961cb0ef41Sopenharmony_ci ':authority': [], 1971cb0ef41Sopenharmony_ci 'xyz': [1, 2, 3, 4], 1981cb0ef41Sopenharmony_ci [sensitiveHeaders]: ['xyz'] 1991cb0ef41Sopenharmony_ci }; 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci assert.deepStrictEqual( 2021cb0ef41Sopenharmony_ci mapToHeaders(headers), 2031cb0ef41Sopenharmony_ci [ ':status\x00200\x00\x00:path\x00abc\x00\x00abc\x001\x00\x00' + 2041cb0ef41Sopenharmony_ci 'xyz\x001\x00\x01xyz\x002\x00\x01xyz\x003\x00\x01xyz\x004\x00\x01', 7 ] 2051cb0ef41Sopenharmony_ci ); 2061cb0ef41Sopenharmony_ci} 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci// The following are not allowed to have multiple values 2091cb0ef41Sopenharmony_ci[ 2101cb0ef41Sopenharmony_ci HTTP2_HEADER_STATUS, 2111cb0ef41Sopenharmony_ci HTTP2_HEADER_METHOD, 2121cb0ef41Sopenharmony_ci HTTP2_HEADER_AUTHORITY, 2131cb0ef41Sopenharmony_ci HTTP2_HEADER_SCHEME, 2141cb0ef41Sopenharmony_ci HTTP2_HEADER_PATH, 2151cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS, 2161cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_MAX_AGE, 2171cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_REQUEST_METHOD, 2181cb0ef41Sopenharmony_ci HTTP2_HEADER_AGE, 2191cb0ef41Sopenharmony_ci HTTP2_HEADER_AUTHORIZATION, 2201cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_ENCODING, 2211cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_LANGUAGE, 2221cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_LENGTH, 2231cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_LOCATION, 2241cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_MD5, 2251cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_RANGE, 2261cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_TYPE, 2271cb0ef41Sopenharmony_ci HTTP2_HEADER_DATE, 2281cb0ef41Sopenharmony_ci HTTP2_HEADER_DNT, 2291cb0ef41Sopenharmony_ci HTTP2_HEADER_ETAG, 2301cb0ef41Sopenharmony_ci HTTP2_HEADER_EXPIRES, 2311cb0ef41Sopenharmony_ci HTTP2_HEADER_FROM, 2321cb0ef41Sopenharmony_ci HTTP2_HEADER_HOST, 2331cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_MATCH, 2341cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_MODIFIED_SINCE, 2351cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_NONE_MATCH, 2361cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_RANGE, 2371cb0ef41Sopenharmony_ci HTTP2_HEADER_IF_UNMODIFIED_SINCE, 2381cb0ef41Sopenharmony_ci HTTP2_HEADER_LAST_MODIFIED, 2391cb0ef41Sopenharmony_ci HTTP2_HEADER_LOCATION, 2401cb0ef41Sopenharmony_ci HTTP2_HEADER_MAX_FORWARDS, 2411cb0ef41Sopenharmony_ci HTTP2_HEADER_PROXY_AUTHORIZATION, 2421cb0ef41Sopenharmony_ci HTTP2_HEADER_RANGE, 2431cb0ef41Sopenharmony_ci HTTP2_HEADER_REFERER, 2441cb0ef41Sopenharmony_ci HTTP2_HEADER_RETRY_AFTER, 2451cb0ef41Sopenharmony_ci HTTP2_HEADER_TK, 2461cb0ef41Sopenharmony_ci HTTP2_HEADER_UPGRADE_INSECURE_REQUESTS, 2471cb0ef41Sopenharmony_ci HTTP2_HEADER_USER_AGENT, 2481cb0ef41Sopenharmony_ci HTTP2_HEADER_X_CONTENT_TYPE_OPTIONS, 2491cb0ef41Sopenharmony_ci].forEach((name) => { 2501cb0ef41Sopenharmony_ci const msg = `Header field "${name}" must only have a single value`; 2511cb0ef41Sopenharmony_ci assert.throws(() => mapToHeaders({ [name]: [1, 2, 3] }), { 2521cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_HEADER_SINGLE_VALUE', 2531cb0ef41Sopenharmony_ci message: msg 2541cb0ef41Sopenharmony_ci }); 2551cb0ef41Sopenharmony_ci}); 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci[ 2581cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_CHARSET, 2591cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_ENCODING, 2601cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_LANGUAGE, 2611cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT_RANGES, 2621cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCEPT, 2631cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_HEADERS, 2641cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_METHODS, 2651cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, 2661cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS, 2671cb0ef41Sopenharmony_ci HTTP2_HEADER_ACCESS_CONTROL_REQUEST_HEADERS, 2681cb0ef41Sopenharmony_ci HTTP2_HEADER_ALLOW, 2691cb0ef41Sopenharmony_ci HTTP2_HEADER_CACHE_CONTROL, 2701cb0ef41Sopenharmony_ci HTTP2_HEADER_CONTENT_DISPOSITION, 2711cb0ef41Sopenharmony_ci HTTP2_HEADER_COOKIE, 2721cb0ef41Sopenharmony_ci HTTP2_HEADER_EXPECT, 2731cb0ef41Sopenharmony_ci HTTP2_HEADER_FORWARDED, 2741cb0ef41Sopenharmony_ci HTTP2_HEADER_LINK, 2751cb0ef41Sopenharmony_ci HTTP2_HEADER_PREFER, 2761cb0ef41Sopenharmony_ci HTTP2_HEADER_PROXY_AUTHENTICATE, 2771cb0ef41Sopenharmony_ci HTTP2_HEADER_REFRESH, 2781cb0ef41Sopenharmony_ci HTTP2_HEADER_SERVER, 2791cb0ef41Sopenharmony_ci HTTP2_HEADER_SET_COOKIE, 2801cb0ef41Sopenharmony_ci HTTP2_HEADER_STRICT_TRANSPORT_SECURITY, 2811cb0ef41Sopenharmony_ci HTTP2_HEADER_TRAILER, 2821cb0ef41Sopenharmony_ci HTTP2_HEADER_VARY, 2831cb0ef41Sopenharmony_ci HTTP2_HEADER_VIA, 2841cb0ef41Sopenharmony_ci HTTP2_HEADER_WARNING, 2851cb0ef41Sopenharmony_ci HTTP2_HEADER_WWW_AUTHENTICATE, 2861cb0ef41Sopenharmony_ci HTTP2_HEADER_X_FRAME_OPTIONS, 2871cb0ef41Sopenharmony_ci].forEach((name) => { 2881cb0ef41Sopenharmony_ci assert(!(mapToHeaders({ [name]: [1, 2, 3] }) instanceof Error), name); 2891cb0ef41Sopenharmony_ci}); 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci[ 2921cb0ef41Sopenharmony_ci HTTP2_HEADER_CONNECTION, 2931cb0ef41Sopenharmony_ci HTTP2_HEADER_UPGRADE, 2941cb0ef41Sopenharmony_ci HTTP2_HEADER_HTTP2_SETTINGS, 2951cb0ef41Sopenharmony_ci HTTP2_HEADER_TE, 2961cb0ef41Sopenharmony_ci HTTP2_HEADER_TRANSFER_ENCODING, 2971cb0ef41Sopenharmony_ci HTTP2_HEADER_PROXY_CONNECTION, 2981cb0ef41Sopenharmony_ci HTTP2_HEADER_KEEP_ALIVE, 2991cb0ef41Sopenharmony_ci 'Connection', 3001cb0ef41Sopenharmony_ci 'Upgrade', 3011cb0ef41Sopenharmony_ci 'HTTP2-Settings', 3021cb0ef41Sopenharmony_ci 'TE', 3031cb0ef41Sopenharmony_ci 'Transfer-Encoding', 3041cb0ef41Sopenharmony_ci 'Proxy-Connection', 3051cb0ef41Sopenharmony_ci 'Keep-Alive', 3061cb0ef41Sopenharmony_ci].forEach((name) => { 3071cb0ef41Sopenharmony_ci assert.throws(() => mapToHeaders({ [name]: 'abc' }), { 3081cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', 3091cb0ef41Sopenharmony_ci name: 'TypeError', 3101cb0ef41Sopenharmony_ci message: 'HTTP/1 Connection specific headers are forbidden: ' + 3111cb0ef41Sopenharmony_ci `"${name.toLowerCase()}"` 3121cb0ef41Sopenharmony_ci }); 3131cb0ef41Sopenharmony_ci}); 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ciassert.throws(() => mapToHeaders({ [HTTP2_HEADER_TE]: ['abc'] }), { 3161cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', 3171cb0ef41Sopenharmony_ci name: 'TypeError', 3181cb0ef41Sopenharmony_ci message: 'HTTP/1 Connection specific headers are forbidden: ' + 3191cb0ef41Sopenharmony_ci `"${HTTP2_HEADER_TE}"` 3201cb0ef41Sopenharmony_ci}); 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ciassert.throws( 3231cb0ef41Sopenharmony_ci () => mapToHeaders({ [HTTP2_HEADER_TE]: ['abc', 'trailers'] }), { 3241cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_INVALID_CONNECTION_HEADERS', 3251cb0ef41Sopenharmony_ci name: 'TypeError', 3261cb0ef41Sopenharmony_ci message: 'HTTP/1 Connection specific headers are forbidden: ' + 3271cb0ef41Sopenharmony_ci `"${HTTP2_HEADER_TE}"` 3281cb0ef41Sopenharmony_ci }); 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci// These should not throw 3311cb0ef41Sopenharmony_cimapToHeaders({ te: 'trailers' }); 3321cb0ef41Sopenharmony_cimapToHeaders({ te: ['trailers'] }); 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ci// HTTP/2 encourages use of Host instead of :authority when converting 3351cb0ef41Sopenharmony_ci// from HTTP/1 to HTTP/2, so we no longer disallow it. 3361cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/29858 3371cb0ef41Sopenharmony_cimapToHeaders({ [HTTP2_HEADER_HOST]: 'abc' }); 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ci// If both are present, the latter has priority 3401cb0ef41Sopenharmony_ciassert.strictEqual(getAuthority({ 3411cb0ef41Sopenharmony_ci [HTTP2_HEADER_AUTHORITY]: 'abc', 3421cb0ef41Sopenharmony_ci [HTTP2_HEADER_HOST]: 'def' 3431cb0ef41Sopenharmony_ci}), 'abc'); 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci{ 3471cb0ef41Sopenharmony_ci const rawHeaders = [ 3481cb0ef41Sopenharmony_ci ':status', '200', 3491cb0ef41Sopenharmony_ci 'cookie', 'foo', 3501cb0ef41Sopenharmony_ci 'set-cookie', 'sc1', 3511cb0ef41Sopenharmony_ci 'age', '10', 3521cb0ef41Sopenharmony_ci 'x-multi', 'first', 3531cb0ef41Sopenharmony_ci ]; 3541cb0ef41Sopenharmony_ci const headers = toHeaderObject(rawHeaders); 3551cb0ef41Sopenharmony_ci assert.strictEqual(headers[':status'], 200); 3561cb0ef41Sopenharmony_ci assert.strictEqual(headers.cookie, 'foo'); 3571cb0ef41Sopenharmony_ci assert.deepStrictEqual(headers['set-cookie'], ['sc1']); 3581cb0ef41Sopenharmony_ci assert.strictEqual(headers.age, '10'); 3591cb0ef41Sopenharmony_ci assert.strictEqual(headers['x-multi'], 'first'); 3601cb0ef41Sopenharmony_ci} 3611cb0ef41Sopenharmony_ci 3621cb0ef41Sopenharmony_ci{ 3631cb0ef41Sopenharmony_ci const rawHeaders = [ 3641cb0ef41Sopenharmony_ci ':status', '200', 3651cb0ef41Sopenharmony_ci ':status', '400', 3661cb0ef41Sopenharmony_ci 'cookie', 'foo', 3671cb0ef41Sopenharmony_ci 'cookie', 'bar', 3681cb0ef41Sopenharmony_ci 'set-cookie', 'sc1', 3691cb0ef41Sopenharmony_ci 'set-cookie', 'sc2', 3701cb0ef41Sopenharmony_ci 'age', '10', 3711cb0ef41Sopenharmony_ci 'age', '20', 3721cb0ef41Sopenharmony_ci 'x-multi', 'first', 3731cb0ef41Sopenharmony_ci 'x-multi', 'second', 3741cb0ef41Sopenharmony_ci ]; 3751cb0ef41Sopenharmony_ci const headers = toHeaderObject(rawHeaders); 3761cb0ef41Sopenharmony_ci assert.strictEqual(headers[':status'], 200); 3771cb0ef41Sopenharmony_ci assert.strictEqual(headers.cookie, 'foo; bar'); 3781cb0ef41Sopenharmony_ci assert.deepStrictEqual(headers['set-cookie'], ['sc1', 'sc2']); 3791cb0ef41Sopenharmony_ci assert.strictEqual(headers.age, '10'); 3801cb0ef41Sopenharmony_ci assert.strictEqual(headers['x-multi'], 'first, second'); 3811cb0ef41Sopenharmony_ci} 382