11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Flags: --expose-gc
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Tests that write uses the correct encoding when writing
121cb0ef41Sopenharmony_ci// using the helper function createWriteReq
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst testString = 'a\u00A1\u0100\uD83D\uDE00';
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst encodings = {
171cb0ef41Sopenharmony_ci  'buffer': 'utf8',
181cb0ef41Sopenharmony_ci  'ascii': 'ascii',
191cb0ef41Sopenharmony_ci  'latin1': 'latin1',
201cb0ef41Sopenharmony_ci  'binary': 'latin1',
211cb0ef41Sopenharmony_ci  'utf8': 'utf8',
221cb0ef41Sopenharmony_ci  'utf-8': 'utf8',
231cb0ef41Sopenharmony_ci  'ucs2': 'ucs2',
241cb0ef41Sopenharmony_ci  'ucs-2': 'ucs2',
251cb0ef41Sopenharmony_ci  'utf16le': 'ucs2',
261cb0ef41Sopenharmony_ci  'utf-16le': 'ucs2',
271cb0ef41Sopenharmony_ci  'UTF8': 'utf8' // Should fall through to Buffer.from
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst testsToRun = Object.keys(encodings).length;
311cb0ef41Sopenharmony_cilet testsFinished = 0;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst server = http2.createServer(common.mustCall((req, res) => {
341cb0ef41Sopenharmony_ci  const testEncoding = encodings[req.url.slice(1)];
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  req.on('data', common.mustCall((chunk) => assert.ok(
371cb0ef41Sopenharmony_ci    Buffer.from(testString, testEncoding).equals(chunk)
381cb0ef41Sopenharmony_ci  )));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  req.on('end', () => res.end());
411cb0ef41Sopenharmony_ci}, Object.keys(encodings).length));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() {
441cb0ef41Sopenharmony_ci  Object.keys(encodings).forEach((writeEncoding) => {
451cb0ef41Sopenharmony_ci    const client = http2.connect(`http://localhost:${this.address().port}`);
461cb0ef41Sopenharmony_ci    const req = client.request({
471cb0ef41Sopenharmony_ci      ':path': `/${writeEncoding}`,
481cb0ef41Sopenharmony_ci      ':method': 'POST'
491cb0ef41Sopenharmony_ci    });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    assert.strictEqual(req._writableState.decodeStrings, false);
521cb0ef41Sopenharmony_ci    req.write(
531cb0ef41Sopenharmony_ci      writeEncoding !== 'buffer' ? testString : Buffer.from(testString),
541cb0ef41Sopenharmony_ci      writeEncoding !== 'buffer' ? writeEncoding : undefined
551cb0ef41Sopenharmony_ci    );
561cb0ef41Sopenharmony_ci    req.resume();
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(function() {
591cb0ef41Sopenharmony_ci      client.close();
601cb0ef41Sopenharmony_ci      testsFinished++;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci      if (testsFinished === testsToRun) {
631cb0ef41Sopenharmony_ci        server.close(common.mustCall());
641cb0ef41Sopenharmony_ci      }
651cb0ef41Sopenharmony_ci    }));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci    // Ref: https://github.com/nodejs/node/issues/17840
681cb0ef41Sopenharmony_ci    const origDestroy = req.destroy;
691cb0ef41Sopenharmony_ci    req.destroy = function(...args) {
701cb0ef41Sopenharmony_ci      // Schedule a garbage collection event at the end of the current
711cb0ef41Sopenharmony_ci      // MakeCallback() run.
721cb0ef41Sopenharmony_ci      process.nextTick(global.gc);
731cb0ef41Sopenharmony_ci      return origDestroy.call(this, ...args);
741cb0ef41Sopenharmony_ci    };
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    req.end();
771cb0ef41Sopenharmony_ci  });
781cb0ef41Sopenharmony_ci}));
79