summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-compat-serverresponse-write-no-cb.js
blob: bc864af46df4cc904dce7d6f92ef1a01189c5374 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Flags: --expose-http2
'use strict';

const { mustCall,
        mustNotCall,
        expectsError,
        hasCrypto, skip } = require('../common');
if (!hasCrypto)
  skip('missing crypto');
const { throws } = require('assert');
const { createServer, connect } = require('http2');

// Http2ServerResponse.write does not imply there is a callback

const expectedError = expectsError({
  code: 'ERR_HTTP2_STREAM_CLOSED',
  message: 'The stream is already closed'
}, 2);

{
  const server = createServer();
  server.listen(0, mustCall(() => {
    const port = server.address().port;
    const url = `http://localhost:${port}`;
    const client = connect(url, mustCall(() => {
      const headers = {
        ':path': '/',
        ':method': 'GET',
        ':scheme': 'http',
        ':authority': `localhost:${port}`
      };
      const request = client.request(headers);
      request.end();
      request.resume();
    }));

    server.once('request', mustCall((request, response) => {
      client.destroy();
      response.stream.session.on('close', mustCall(() => {
        response.on('error', mustNotCall());
        throws(
          () => { response.write('muahaha'); },
          expectsError({
            code: 'ERR_HTTP2_STREAM_CLOSED',
            type: Error,
            message: 'The stream is already closed'
          })
        );
        server.close();
      }));
    }));
  }));
}

{
  const server = createServer();
  server.listen(0, mustCall(() => {
    const port = server.address().port;
    const url = `http://localhost:${port}`;
    const client = connect(url, mustCall(() => {
      const headers = {
        ':path': '/',
        ':method': 'get',
        ':scheme': 'http',
        ':authority': `localhost:${port}`
      };
      const request = client.request(headers);
      request.end();
      request.resume();
    }));

    server.once('request', mustCall((request, response) => {
      client.destroy();
      response.stream.session.on('close', mustCall(() => {
        response.write('muahaha', mustCall(expectedError));
        server.close();
      }));
    }));
  }));
}

{
  const server = createServer();
  server.listen(0, mustCall(() => {
    const port = server.address().port;
    const url = `http://localhost:${port}`;
    const client = connect(url, mustCall(() => {
      const headers = {
        ':path': '/',
        ':method': 'get',
        ':scheme': 'http',
        ':authority': `localhost:${port}`
      };
      const request = client.request(headers);
      request.end();
      request.resume();
    }));

    server.once('request', mustCall((request, response) => {
      response.stream.session.on('close', mustCall(() => {
        response.write('muahaha', 'utf8', mustCall(expectedError));
        server.close();
      }));
      client.destroy();
    }));
  }));
}