summaryrefslogtreecommitdiff
path: root/test/abort/test-http-parser-consume.js
blob: 13dc93874a226ff65eca1d55d68eff3f4e7a8bbf (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
'use strict';
const common = require('../common');
const assert = require('assert');
const { createServer, get } = require('http');
const { spawn } = require('child_process');

if (process.argv[2] === 'child') {
  // sub-process
  const server = createServer(common.mustCall((_, res) => res.end('h')));
  server.listen(0, common.mustCall(() => {
    const rr = get({ port: server.address().port }, common.mustCall(() => {
      // This bad input (0) should abort the parser and the process
      rr.parser.consume(0);
      // This line should be unreachable.
      assert.fail('this should be unreachable');
    }));
  }));
} else {
  // super-process
  const child = spawn(process.execPath, [__filename, 'child']);
  child.stdout.on('data', common.mustNotCall());

  let stderr = '';
  child.stderr.on('data', common.mustCallAtLeast((data) => {
    assert(Buffer.isBuffer(data));
    stderr += data.toString('utf8');
  }, 1));
  child.on('exit', common.mustCall((code, signal) => {
    assert(stderr.includes('failed'), `stderr: ${stderr}`);
    const didAbort = common.nodeProcessAborted(code, signal);
    assert(didAbort, `process did not abort, code:${code} signal:${signal}`);
  }));
}