summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-early-hints-invalid-argument.js
blob: f776bcafa40ed3dffb44b9586119cd935245f673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const common = require('../common');
const assert = require('node:assert');
const http = require('node:http');
const debug = require('node:util').debuglog('test');

const testResBody = 'response content\n';

{
  const server = http.createServer(common.mustCall((req, res) => {
    debug('Server sending early hints...');
    assert.throws(() => {
      res.writeEarlyHints('bad argument type');
    }, (err) => err.code === 'ERR_INVALID_ARG_TYPE');

    assert.throws(() => {
      res.writeEarlyHints({
        link: '</>; '
      });
    }, (err) => err.code === 'ERR_INVALID_ARG_VALUE');

    assert.throws(() => {
      res.writeEarlyHints({
        link: 'rel=preload; </scripts.js>'
      });
    }, (err) => err.code === 'ERR_INVALID_ARG_VALUE');

    assert.throws(() => {
      res.writeEarlyHints({
        link: 'invalid string'
      });
    }, (err) => err.code === 'ERR_INVALID_ARG_VALUE');

    debug('Server sending full response...');
    res.end(testResBody);
    server.close();
  }));

  server.listen(0, common.mustCall(() => {
    const req = http.request({
      port: server.address().port, path: '/'
    });

    req.end();
    debug('Client sending request...');

    req.on('information', common.mustNotCall());
  }));
}