11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/11788.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst http = require('http');
81cb0ef41Sopenharmony_ciconst net = require('net');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifor (const enc of ['utf8', 'utf16le', 'latin1', 'UTF-8']) {
111cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
121cb0ef41Sopenharmony_ci    res.setHeader('content-type', `text/plain; charset=${enc}`);
131cb0ef41Sopenharmony_ci    res.write('helloworld', enc);
141cb0ef41Sopenharmony_ci    res.end();
151cb0ef41Sopenharmony_ci  })).listen(0);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  server.on('listening', common.mustCall(() => {
181cb0ef41Sopenharmony_ci    const buffers = [];
191cb0ef41Sopenharmony_ci    const socket = net.connect(server.address().port);
201cb0ef41Sopenharmony_ci    socket.write('GET / HTTP/1.0\r\n\r\n');
211cb0ef41Sopenharmony_ci    socket.on('data', (data) => buffers.push(data));
221cb0ef41Sopenharmony_ci    socket.on('end', common.mustCall(() => {
231cb0ef41Sopenharmony_ci      const received = Buffer.concat(buffers);
241cb0ef41Sopenharmony_ci      const headerEnd = received.indexOf('\r\n\r\n', 'utf8');
251cb0ef41Sopenharmony_ci      assert.notStrictEqual(headerEnd, -1);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci      const header = received.toString('utf8', 0, headerEnd).split('\r\n');
281cb0ef41Sopenharmony_ci      const body = received.toString(enc, headerEnd + 4);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci      assert.strictEqual(header[0], 'HTTP/1.1 200 OK');
311cb0ef41Sopenharmony_ci      assert.strictEqual(header[1], `content-type: text/plain; charset=${enc}`);
321cb0ef41Sopenharmony_ci      assert.strictEqual(body, 'helloworld');
331cb0ef41Sopenharmony_ci      server.close();
341cb0ef41Sopenharmony_ci    }));
351cb0ef41Sopenharmony_ci  }));
361cb0ef41Sopenharmony_ci}
37