summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-client-jsstream-destroy.js
blob: 7e44241e985a64c174c4e916fd350f22cd4becd5 (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
'use strict';

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const h2 = require('http2');
const tls = require('tls');
const fixtures = require('../common/fixtures');
const { Duplex } = require('stream');

const server = h2.createSecureServer({
  key: fixtures.readKey('agent1-key.pem'),
  cert: fixtures.readKey('agent1-cert.pem')
});

class JSSocket extends Duplex {
  constructor(socket) {
    super({ emitClose: true });
    socket.on('close', () => this.destroy());
    socket.on('data', (data) => this.push(data));
    this.socket = socket;
  }

  _write(data, encoding, callback) {
    this.socket.write(data, encoding, callback);
  }

  _read(size) {
  }

  _final(cb) {
    cb();
  }
}

server.listen(0, common.mustCall(function() {
  const socket = tls.connect({
    rejectUnauthorized: false,
    host: 'localhost',
    port: this.address().port,
    ALPNProtocols: ['h2']
  }, () => {
    const proxy = new JSSocket(socket);
    const client = h2.connect(`https://localhost:${this.address().port}`, {
      createConnection: () => proxy
    });
    const req = client.request();

    server.on('request', () => {
      socket.destroy();
    });

    req.on('close', common.mustCall(() => {
      client.close();
      server.close();
    }));
  });
}));