summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-compat-serverresponse-write.js
blob: af3029835eaecbdc606be2e1b763b5a1d370b1ea (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
'use strict';

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

const server = createServer();
server.listen(0, mustCall(() => {
  const port = server.address().port;
  const url = `http://localhost:${port}`;
  const client = connect(url, mustCall(() => {
    const request = client.request();
    request.resume();
    request.on('end', mustCall());
    request.on('close', mustCall(() => {
      client.close();
    }));
  }));

  server.once('request', mustCall((request, response) => {
    // response.write() returns true
    assert(response.write('muahaha', 'utf8', mustCall()));

    response.stream.close(0, mustCall(() => {
      response.on('error', mustNotCall());

      // response.write() without cb returns error
      expectsError(
        () => { response.write('muahaha'); },
        {
          type: Error,
          code: 'ERR_HTTP2_INVALID_STREAM',
          message: 'The stream has been destroyed'
        }
      );

      // response.write() with cb returns falsy value
      assert(!response.write('muahaha', mustCall()));

      client.destroy();
      server.close();
    }));
  }));
}));