11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto) common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('node:assert');
71cb0ef41Sopenharmony_ciconst http2 = require('node:http2');
81cb0ef41Sopenharmony_ciconst debug = require('node:util').debuglog('test');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst testResBody = 'response content';
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci{
131cb0ef41Sopenharmony_ci  // Happy flow - string argument
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const server = http2.createServer();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  server.on('request', common.mustCall((req, res) => {
181cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
191cb0ef41Sopenharmony_ci    res.writeEarlyHints({
201cb0ef41Sopenharmony_ci      link: '</styles.css>; rel=preload; as=style'
211cb0ef41Sopenharmony_ci    });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    debug('Server sending full response...');
241cb0ef41Sopenharmony_ci    res.end(testResBody);
251cb0ef41Sopenharmony_ci  }));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  server.listen(0);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  server.on('listening', common.mustCall(() => {
301cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${server.address().port}`);
311cb0ef41Sopenharmony_ci    const req = client.request();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    debug('Client sending request...');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    req.on('headers', common.mustCall((headers) => {
361cb0ef41Sopenharmony_ci      assert.notStrictEqual(headers, undefined);
371cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 103);
381cb0ef41Sopenharmony_ci      assert.strictEqual(headers.link, '</styles.css>; rel=preload; as=style');
391cb0ef41Sopenharmony_ci    }));
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((headers) => {
421cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 200);
431cb0ef41Sopenharmony_ci    }));
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    let data = '';
461cb0ef41Sopenharmony_ci    req.on('data', common.mustCallAtLeast((d) => data += d));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
491cb0ef41Sopenharmony_ci      debug('Got full response.');
501cb0ef41Sopenharmony_ci      assert.strictEqual(data, testResBody);
511cb0ef41Sopenharmony_ci      client.close();
521cb0ef41Sopenharmony_ci      server.close();
531cb0ef41Sopenharmony_ci    }));
541cb0ef41Sopenharmony_ci  }));
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  // Happy flow - array argument
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  const server = http2.createServer();
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  server.on('request', common.mustCall((req, res) => {
631cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
641cb0ef41Sopenharmony_ci    res.writeEarlyHints({
651cb0ef41Sopenharmony_ci      link: [
661cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style',
671cb0ef41Sopenharmony_ci        '</scripts.js>; rel=preload; as=script',
681cb0ef41Sopenharmony_ci      ]
691cb0ef41Sopenharmony_ci    });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci    debug('Server sending full response...');
721cb0ef41Sopenharmony_ci    res.end(testResBody);
731cb0ef41Sopenharmony_ci  }));
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  server.listen(0);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  server.on('listening', common.mustCall(() => {
781cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${server.address().port}`);
791cb0ef41Sopenharmony_ci    const req = client.request();
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    debug('Client sending request...');
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    req.on('headers', common.mustCall((headers) => {
841cb0ef41Sopenharmony_ci      assert.notStrictEqual(headers, undefined);
851cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 103);
861cb0ef41Sopenharmony_ci      assert.strictEqual(
871cb0ef41Sopenharmony_ci        headers.link,
881cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style, </scripts.js>; rel=preload; as=script'
891cb0ef41Sopenharmony_ci      );
901cb0ef41Sopenharmony_ci    }));
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((headers) => {
931cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 200);
941cb0ef41Sopenharmony_ci    }));
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    let data = '';
971cb0ef41Sopenharmony_ci    req.on('data', common.mustCallAtLeast((d) => data += d));
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
1001cb0ef41Sopenharmony_ci      debug('Got full response.');
1011cb0ef41Sopenharmony_ci      assert.strictEqual(data, testResBody);
1021cb0ef41Sopenharmony_ci      client.close();
1031cb0ef41Sopenharmony_ci      server.close();
1041cb0ef41Sopenharmony_ci    }));
1051cb0ef41Sopenharmony_ci  }));
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci{
1091cb0ef41Sopenharmony_ci  // Happy flow - empty array
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  const server = http2.createServer();
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  server.on('request', common.mustCall((req, res) => {
1141cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
1151cb0ef41Sopenharmony_ci    res.writeEarlyHints({
1161cb0ef41Sopenharmony_ci      link: []
1171cb0ef41Sopenharmony_ci    });
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci    debug('Server sending full response...');
1201cb0ef41Sopenharmony_ci    res.end(testResBody);
1211cb0ef41Sopenharmony_ci  }));
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  server.listen(0);
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  server.on('listening', common.mustCall(() => {
1261cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${server.address().port}`);
1271cb0ef41Sopenharmony_ci    const req = client.request();
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci    debug('Client sending request...');
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci    req.on('headers', common.mustNotCall());
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((headers) => {
1341cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 200);
1351cb0ef41Sopenharmony_ci    }));
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci    let data = '';
1381cb0ef41Sopenharmony_ci    req.on('data', common.mustCallAtLeast((d) => data += d));
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
1411cb0ef41Sopenharmony_ci      debug('Got full response.');
1421cb0ef41Sopenharmony_ci      assert.strictEqual(data, testResBody);
1431cb0ef41Sopenharmony_ci      client.close();
1441cb0ef41Sopenharmony_ci      server.close();
1451cb0ef41Sopenharmony_ci    }));
1461cb0ef41Sopenharmony_ci  }));
1471cb0ef41Sopenharmony_ci}
148