• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('node:assert');
4const http = require('node:http');
5const debug = require('node:util').debuglog('test');
6
7const testResBody = 'response content\n';
8
9{
10  const server = http.createServer(common.mustCall((req, res) => {
11    debug('Server sending early hints...');
12    assert.throws(() => {
13      res.writeEarlyHints('bad argument type');
14    }, (err) => err.code === 'ERR_INVALID_ARG_TYPE');
15
16    assert.throws(() => {
17      res.writeEarlyHints({
18        link: '</>; '
19      });
20    }, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
21
22    assert.throws(() => {
23      res.writeEarlyHints({
24        link: 'rel=preload; </scripts.js>'
25      });
26    }, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
27
28    assert.throws(() => {
29      res.writeEarlyHints({
30        link: 'invalid string'
31      });
32    }, (err) => err.code === 'ERR_INVALID_ARG_VALUE');
33
34    debug('Server sending full response...');
35    res.end(testResBody);
36    server.close();
37  }));
38
39  server.listen(0, common.mustCall(() => {
40    const req = http.request({
41      port: server.address().port, path: '/'
42    });
43
44    req.end();
45    debug('Client sending request...');
46
47    req.on('information', common.mustNotCall());
48  }));
49}
50