summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-aborted.js
blob: e22e7ca4cc330fc8bfef59bd745dd130bcc8cefc (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
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';

const common = require('../common');
const http = require('http');
const assert = require('assert');

{
  const server = http.createServer(common.mustCall(function(req, res) {
    req.on('aborted', common.mustCall(function() {
      assert.strictEqual(this.aborted, true);
    }));
    req.on('error', common.mustCall(function(err) {
      assert.strictEqual(err.code, 'ECONNRESET');
      assert.strictEqual(err.message, 'aborted');
      server.close();
    }));
    assert.strictEqual(req.aborted, false);
    res.write('hello');
  }));

  server.listen(0, common.mustCall(() => {
    const req = http.get({
      port: server.address().port,
      headers: { connection: 'keep-alive' }
    }, common.mustCall((res) => {
      res.on('aborted', common.mustCall(() => {
        assert.strictEqual(res.aborted, true);
      }));
      res.on('error', common.expectsError({
        code: 'ECONNRESET',
        message: 'aborted'
      }));
      req.abort();
    }));
  }));
}

{
  // Don't crash if no 'error' handler on server request.

  const server = http.createServer(common.mustCall(function(req, res) {
    req.on('aborted', common.mustCall(function() {
      assert.strictEqual(this.aborted, true);
      server.close();
    }));
    assert.strictEqual(req.aborted, false);
    res.write('hello');
  }));

  server.listen(0, common.mustCall(() => {
    const req = http.get({
      port: server.address().port,
      headers: { connection: 'keep-alive' }
    }, common.mustCall((res) => {
      res.on('aborted', common.mustCall(() => {
        assert.strictEqual(res.aborted, true);
      }));
      req.abort();
    }));
  }));
}