• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// Regression test for https://github.com/nodejs/node/issues/11788.
5
6const assert = require('assert');
7const http = require('http');
8const net = require('net');
9
10for (const enc of ['utf8', 'utf16le', 'latin1', 'UTF-8']) {
11  const server = http.createServer(common.mustCall((req, res) => {
12    res.setHeader('content-type', `text/plain; charset=${enc}`);
13    res.write('helloworld', enc);
14    res.end();
15  })).listen(0);
16
17  server.on('listening', common.mustCall(() => {
18    const buffers = [];
19    const socket = net.connect(server.address().port);
20    socket.write('GET / HTTP/1.0\r\n\r\n');
21    socket.on('data', (data) => buffers.push(data));
22    socket.on('end', common.mustCall(() => {
23      const received = Buffer.concat(buffers);
24      const headerEnd = received.indexOf('\r\n\r\n', 'utf8');
25      assert.notStrictEqual(headerEnd, -1);
26
27      const header = received.toString('utf8', 0, headerEnd).split('\r\n');
28      const body = received.toString(enc, headerEnd + 4);
29
30      assert.strictEqual(header[0], 'HTTP/1.1 200 OK');
31      assert.strictEqual(header[1], `content-type: text/plain; charset=${enc}`);
32      assert.strictEqual(body, 'helloworld');
33      server.close();
34    }));
35  }));
36}
37