summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-error-rawbytes.js
blob: c0ea16a64326989fb30a74130da1be915d1dcaae (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
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

const response = Buffer.from('HTTP/1.1 200 OK\r\n' +
  'Content-Length: 6\r\n' +
  'Transfer-Encoding: Chunked\r\n' +
  '\r\n' +
  '6\r\nfoobar' +
  '0\r\n');

const server = net.createServer(common.mustCall((conn) => {
  conn.write(response);
}));

server.listen(0, common.mustCall(() => {
  const req = http.get(`http://localhost:${server.address().port}/`);
  req.end();
  req.on('error', common.mustCall((err) => {
    const reason = 'Content-Length can\'t be present with Transfer-Encoding';
    assert.strictEqual(err.message, `Parse Error: ${reason}`);
    assert(err.bytesParsed < response.length);
    assert(err.bytesParsed >= response.indexOf('Transfer-Encoding'));
    assert.strictEqual(err.code, 'HPE_UNEXPECTED_CONTENT_LENGTH');
    assert.strictEqual(err.reason, reason);
    assert.deepStrictEqual(err.rawPacket, response);

    server.close();
  }));
}));