• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Flags: --expose-gc
4
5const common = require('../common');
6if (!common.hasCrypto)
7  common.skip('missing crypto');
8const assert = require('assert');
9const http2 = require('http2');
10
11// Tests that write uses the correct encoding when writing
12// using the helper function createWriteReq
13
14const testString = 'a\u00A1\u0100\uD83D\uDE00';
15
16const encodings = {
17  'buffer': 'utf8',
18  'ascii': 'ascii',
19  'latin1': 'latin1',
20  'binary': 'latin1',
21  'utf8': 'utf8',
22  'utf-8': 'utf8',
23  'ucs2': 'ucs2',
24  'ucs-2': 'ucs2',
25  'utf16le': 'ucs2',
26  'utf-16le': 'ucs2',
27  'UTF8': 'utf8' // Should fall through to Buffer.from
28};
29
30const testsToRun = Object.keys(encodings).length;
31let testsFinished = 0;
32
33const server = http2.createServer(common.mustCall((req, res) => {
34  const testEncoding = encodings[req.url.slice(1)];
35
36  req.on('data', common.mustCall((chunk) => assert.ok(
37    Buffer.from(testString, testEncoding).equals(chunk)
38  )));
39
40  req.on('end', () => res.end());
41}, Object.keys(encodings).length));
42
43server.listen(0, common.mustCall(function() {
44  Object.keys(encodings).forEach((writeEncoding) => {
45    const client = http2.connect(`http://localhost:${this.address().port}`);
46    const req = client.request({
47      ':path': `/${writeEncoding}`,
48      ':method': 'POST'
49    });
50
51    assert.strictEqual(req._writableState.decodeStrings, false);
52    req.write(
53      writeEncoding !== 'buffer' ? testString : Buffer.from(testString),
54      writeEncoding !== 'buffer' ? writeEncoding : undefined
55    );
56    req.resume();
57
58    req.on('end', common.mustCall(function() {
59      client.close();
60      testsFinished++;
61
62      if (testsFinished === testsToRun) {
63        server.close(common.mustCall());
64      }
65    }));
66
67    // Ref: https://github.com/nodejs/node/issues/17840
68    const origDestroy = req.destroy;
69    req.destroy = function(...args) {
70      // Schedule a garbage collection event at the end of the current
71      // MakeCallback() run.
72      process.nextTick(global.gc);
73      return origDestroy.call(this, ...args);
74    };
75
76    req.end();
77  });
78}));
79