11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('node:assert');
41cb0ef41Sopenharmony_ciconst http = require('node:http');
51cb0ef41Sopenharmony_ciconst debug = require('node:util').debuglog('test');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst testResBody = 'response content\n';
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci{
101cb0ef41Sopenharmony_ci  // Happy flow - string argument
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
131cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
141cb0ef41Sopenharmony_ci    res.writeEarlyHints({
151cb0ef41Sopenharmony_ci      link: '</styles.css>; rel=preload; as=style'
161cb0ef41Sopenharmony_ci    });
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci    debug('Server sending full response...');
191cb0ef41Sopenharmony_ci    res.end(testResBody);
201cb0ef41Sopenharmony_ci  }));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
231cb0ef41Sopenharmony_ci    const req = http.request({
241cb0ef41Sopenharmony_ci      port: server.address().port, path: '/'
251cb0ef41Sopenharmony_ci    });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    debug('Client sending request...');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    req.on('information', common.mustCall((res) => {
301cb0ef41Sopenharmony_ci      assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style');
311cb0ef41Sopenharmony_ci    }));
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
341cb0ef41Sopenharmony_ci      let body = '';
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci      res.on('data', (chunk) => {
391cb0ef41Sopenharmony_ci        body += chunk;
401cb0ef41Sopenharmony_ci      });
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
431cb0ef41Sopenharmony_ci        debug('Got full response.');
441cb0ef41Sopenharmony_ci        assert.strictEqual(body, testResBody);
451cb0ef41Sopenharmony_ci        server.close();
461cb0ef41Sopenharmony_ci      }));
471cb0ef41Sopenharmony_ci    }));
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    req.end();
501cb0ef41Sopenharmony_ci  }));
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci{
541cb0ef41Sopenharmony_ci  // Happy flow - array argument
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
571cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
581cb0ef41Sopenharmony_ci    res.writeEarlyHints({
591cb0ef41Sopenharmony_ci      link: [
601cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style',
611cb0ef41Sopenharmony_ci        '</scripts.js>; crossorigin; rel=preload; as=script',
621cb0ef41Sopenharmony_ci        '</scripts.js>; rel=preload; as=script; crossorigin',
631cb0ef41Sopenharmony_ci      ]
641cb0ef41Sopenharmony_ci    });
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    debug('Server sending full response...');
671cb0ef41Sopenharmony_ci    res.end(testResBody);
681cb0ef41Sopenharmony_ci  }));
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
711cb0ef41Sopenharmony_ci    const req = http.request({
721cb0ef41Sopenharmony_ci      port: server.address().port, path: '/'
731cb0ef41Sopenharmony_ci    });
741cb0ef41Sopenharmony_ci    debug('Client sending request...');
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    req.on('information', common.mustCall((res) => {
771cb0ef41Sopenharmony_ci      assert.strictEqual(
781cb0ef41Sopenharmony_ci        res.headers.link,
791cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style, </scripts.js>; crossorigin; ' +
801cb0ef41Sopenharmony_ci        'rel=preload; as=script, </scripts.js>; rel=preload; as=script; crossorigin'
811cb0ef41Sopenharmony_ci      );
821cb0ef41Sopenharmony_ci    }));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
851cb0ef41Sopenharmony_ci      let body = '';
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci      res.on('data', (chunk) => {
901cb0ef41Sopenharmony_ci        body += chunk;
911cb0ef41Sopenharmony_ci      });
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
941cb0ef41Sopenharmony_ci        debug('Got full response.');
951cb0ef41Sopenharmony_ci        assert.strictEqual(body, testResBody);
961cb0ef41Sopenharmony_ci        server.close();
971cb0ef41Sopenharmony_ci      }));
981cb0ef41Sopenharmony_ci    }));
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci    req.end();
1011cb0ef41Sopenharmony_ci  }));
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci{
1051cb0ef41Sopenharmony_ci  // Happy flow - empty array
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
1081cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
1091cb0ef41Sopenharmony_ci    res.writeEarlyHints({
1101cb0ef41Sopenharmony_ci      link: []
1111cb0ef41Sopenharmony_ci    });
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci    debug('Server sending full response...');
1141cb0ef41Sopenharmony_ci    res.end(testResBody);
1151cb0ef41Sopenharmony_ci  }));
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
1181cb0ef41Sopenharmony_ci    const req = http.request({
1191cb0ef41Sopenharmony_ci      port: server.address().port, path: '/'
1201cb0ef41Sopenharmony_ci    });
1211cb0ef41Sopenharmony_ci    debug('Client sending request...');
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci    req.on('information', common.mustNotCall());
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
1261cb0ef41Sopenharmony_ci      let body = '';
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci      res.on('data', (chunk) => {
1311cb0ef41Sopenharmony_ci        body += chunk;
1321cb0ef41Sopenharmony_ci      });
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
1351cb0ef41Sopenharmony_ci        debug('Got full response.');
1361cb0ef41Sopenharmony_ci        assert.strictEqual(body, testResBody);
1371cb0ef41Sopenharmony_ci        server.close();
1381cb0ef41Sopenharmony_ci      }));
1391cb0ef41Sopenharmony_ci    }));
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci    req.end();
1421cb0ef41Sopenharmony_ci  }));
1431cb0ef41Sopenharmony_ci}
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci{
1461cb0ef41Sopenharmony_ci  // Happy flow - object argument with string
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
1491cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
1501cb0ef41Sopenharmony_ci    res.writeEarlyHints({
1511cb0ef41Sopenharmony_ci      'link': '</styles.css>; rel=preload; as=style',
1521cb0ef41Sopenharmony_ci      'x-trace-id': 'id for diagnostics'
1531cb0ef41Sopenharmony_ci    });
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci    debug('Server sending full response...');
1561cb0ef41Sopenharmony_ci    res.end(testResBody);
1571cb0ef41Sopenharmony_ci  }));
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
1601cb0ef41Sopenharmony_ci    const req = http.request({
1611cb0ef41Sopenharmony_ci      port: server.address().port, path: '/'
1621cb0ef41Sopenharmony_ci    });
1631cb0ef41Sopenharmony_ci    debug('Client sending request...');
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci    req.on('information', common.mustCall((res) => {
1661cb0ef41Sopenharmony_ci      assert.strictEqual(
1671cb0ef41Sopenharmony_ci        res.headers.link,
1681cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style'
1691cb0ef41Sopenharmony_ci      );
1701cb0ef41Sopenharmony_ci      assert.strictEqual(res.headers['x-trace-id'], 'id for diagnostics');
1711cb0ef41Sopenharmony_ci    }));
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
1741cb0ef41Sopenharmony_ci      let body = '';
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci      res.on('data', (chunk) => {
1791cb0ef41Sopenharmony_ci        body += chunk;
1801cb0ef41Sopenharmony_ci      });
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
1831cb0ef41Sopenharmony_ci        debug('Got full response.');
1841cb0ef41Sopenharmony_ci        assert.strictEqual(body, testResBody);
1851cb0ef41Sopenharmony_ci        server.close();
1861cb0ef41Sopenharmony_ci      }));
1871cb0ef41Sopenharmony_ci    }));
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci    req.end();
1901cb0ef41Sopenharmony_ci  }));
1911cb0ef41Sopenharmony_ci}
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci{
1941cb0ef41Sopenharmony_ci  // Happy flow - object argument with array of strings
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
1971cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
1981cb0ef41Sopenharmony_ci    res.writeEarlyHints({
1991cb0ef41Sopenharmony_ci      'link': [
2001cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style',
2011cb0ef41Sopenharmony_ci        '</scripts.js>; rel=preload; as=script',
2021cb0ef41Sopenharmony_ci      ],
2031cb0ef41Sopenharmony_ci      'x-trace-id': 'id for diagnostics'
2041cb0ef41Sopenharmony_ci    });
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci    debug('Server sending full response...');
2071cb0ef41Sopenharmony_ci    res.end(testResBody);
2081cb0ef41Sopenharmony_ci  }));
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
2111cb0ef41Sopenharmony_ci    const req = http.request({
2121cb0ef41Sopenharmony_ci      port: server.address().port, path: '/'
2131cb0ef41Sopenharmony_ci    });
2141cb0ef41Sopenharmony_ci    debug('Client sending request...');
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci    req.on('information', common.mustCall((res) => {
2171cb0ef41Sopenharmony_ci      assert.strictEqual(
2181cb0ef41Sopenharmony_ci        res.headers.link,
2191cb0ef41Sopenharmony_ci        '</styles.css>; rel=preload; as=style, </scripts.js>; rel=preload; as=script'
2201cb0ef41Sopenharmony_ci      );
2211cb0ef41Sopenharmony_ci      assert.strictEqual(res.headers['x-trace-id'], 'id for diagnostics');
2221cb0ef41Sopenharmony_ci    }));
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
2251cb0ef41Sopenharmony_ci      let body = '';
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci      res.on('data', (chunk) => {
2301cb0ef41Sopenharmony_ci        body += chunk;
2311cb0ef41Sopenharmony_ci      });
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
2341cb0ef41Sopenharmony_ci        debug('Got full response.');
2351cb0ef41Sopenharmony_ci        assert.strictEqual(body, testResBody);
2361cb0ef41Sopenharmony_ci        server.close();
2371cb0ef41Sopenharmony_ci      }));
2381cb0ef41Sopenharmony_ci    }));
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci    req.end();
2411cb0ef41Sopenharmony_ci  }));
2421cb0ef41Sopenharmony_ci}
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci{
2451cb0ef41Sopenharmony_ci  // Happy flow - empty object
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
2481cb0ef41Sopenharmony_ci    debug('Server sending early hints...');
2491cb0ef41Sopenharmony_ci    res.writeEarlyHints({});
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci    debug('Server sending full response...');
2521cb0ef41Sopenharmony_ci    res.end(testResBody);
2531cb0ef41Sopenharmony_ci  }));
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
2561cb0ef41Sopenharmony_ci    const req = http.request({
2571cb0ef41Sopenharmony_ci      port: server.address().port, path: '/'
2581cb0ef41Sopenharmony_ci    });
2591cb0ef41Sopenharmony_ci    debug('Client sending request...');
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci    req.on('information', common.mustNotCall());
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
2641cb0ef41Sopenharmony_ci      let body = '';
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200, `Final status code was ${res.statusCode}, not 200.`);
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci      res.on('data', (chunk) => {
2691cb0ef41Sopenharmony_ci        body += chunk;
2701cb0ef41Sopenharmony_ci      });
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
2731cb0ef41Sopenharmony_ci        debug('Got full response.');
2741cb0ef41Sopenharmony_ci        assert.strictEqual(body, testResBody);
2751cb0ef41Sopenharmony_ci        server.close();
2761cb0ef41Sopenharmony_ci      }));
2771cb0ef41Sopenharmony_ci    }));
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci    req.end();
2801cb0ef41Sopenharmony_ci  }));
2811cb0ef41Sopenharmony_ci}
282