summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-compat-serverresponse-finished.js
blob: a42d40227ca33bf26622576656b54aee65f6b7a3 (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');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const net = require('net');

// Http2ServerResponse.finished
const server = h2.createServer();
server.listen(0, common.mustCall(() => {
  const port = server.address().port;
  server.once('request', common.mustCall((request, response) => {
    assert.ok(response.socket instanceof net.Socket);
    assert.ok(response.connection instanceof net.Socket);
    assert.strictEqual(response.socket, response.connection);

    response.on('finish', common.mustCall(() => {
      assert.strictEqual(response.socket, undefined);
      assert.strictEqual(response.connection, undefined);
      process.nextTick(common.mustCall(() => {
        assert.ok(response.stream);
        server.close();
      }));
    }));
    assert.strictEqual(response.finished, false);
    assert.strictEqual(response.writableEnded, false);
    response.end();
    assert.strictEqual(response.finished, true);
    assert.strictEqual(response.writableEnded, true);
  }));

  const url = `http://localhost:${port}`;
  const client = h2.connect(url, common.mustCall(() => {
    const headers = {
      ':path': '/',
      ':method': 'GET',
      ':scheme': 'http',
      ':authority': `localhost:${port}`
    };
    const request = client.request(headers);
    request.on('end', common.mustCall(() => {
      client.close();
    }));
    request.end();
    request.resume();
  }));
}));