summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-createwritereq.js
blob: 3015ad6c642801b5bf9556ad8b9e306f7ca58f2c (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
'use strict';

// Flags: --expose-gc

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');

// Tests that write uses the correct encoding when writing
// using the helper function createWriteReq

const testString = 'a\u00A1\u0100\uD83D\uDE00';

const encodings = {
  'buffer': 'utf8',
  'ascii': 'ascii',
  'latin1': 'latin1',
  'binary': 'latin1',
  'utf8': 'utf8',
  'utf-8': 'utf8',
  'ucs2': 'ucs2',
  'ucs-2': 'ucs2',
  'utf16le': 'ucs2',
  'utf-16le': 'ucs2',
  'UTF8': 'utf8' // Should fall through to Buffer.from
};

const testsToRun = Object.keys(encodings).length;
let testsFinished = 0;

const server = http2.createServer(common.mustCall((req, res) => {
  const testEncoding = encodings[req.url.slice(1)];

  req.on('data', common.mustCall((chunk) => assert.ok(
    Buffer.from(testString, testEncoding).equals(chunk)
  )));

  req.on('end', () => res.end());
}, Object.keys(encodings).length));

server.listen(0, common.mustCall(function() {
  Object.keys(encodings).forEach((writeEncoding) => {
    const client = http2.connect(`http://localhost:${this.address().port}`);
    const req = client.request({
      ':path': `/${writeEncoding}`,
      ':method': 'POST'
    });

    assert.strictEqual(req._writableState.decodeStrings, false);
    req.write(
      writeEncoding !== 'buffer' ? testString : Buffer.from(testString),
      writeEncoding !== 'buffer' ? writeEncoding : undefined
    );
    req.resume();

    req.on('end', common.mustCall(function() {
      client.close();
      testsFinished++;

      if (testsFinished === testsToRun) {
        server.close(common.mustCall());
      }
    }));

    // Ref: https://github.com/nodejs/node/issues/17840
    const origDestroy = req.destroy;
    req.destroy = function(...args) {
      // Schedule a garbage collection event at the end of the current
      // MakeCallback() run.
      process.nextTick(global.gc);
      return origDestroy.call(this, ...args);
    };

    req.end();
  });
}));