0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-http-server-client-error.js
XadillaX 6c0da34905
http: add rawPacket in err of clientError event
The `rawPacket` is the current buffer that just parsed. Adding this
buffer to the error object of `clientError` event is to make it possible
that developers can log the broken packet.

PR-URL: https://github.com/nodejs/node/pull/17672
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-12-27 19:42:57 +01:00

46 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
const server = http.createServer(common.mustCall(function(req, res) {
res.end();
}));
server.on('clientError', common.mustCall(function(err, socket) {
assert.strictEqual(err instanceof Error, true);
assert.strictEqual(err.code, 'HPE_INVALID_METHOD');
assert.strictEqual(err.bytesParsed, 1);
assert.strictEqual(err.message, 'Parse Error');
assert.strictEqual(err.rawPacket.toString(), 'Oopsie-doopsie\r\n');
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
server.close();
}));
server.listen(0, function() {
function next() {
// Invalid request
const client = net.connect(server.address().port);
client.end('Oopsie-doopsie\r\n');
let chunks = '';
client.on('data', function(chunk) {
chunks += chunk;
});
client.once('end', function() {
assert.strictEqual(chunks, 'HTTP/1.1 400 Bad Request\r\n\r\n');
});
}
// Normal request
http.get({ port: this.address().port, path: '/' }, function(res) {
assert.strictEqual(res.statusCode, 200);
res.resume();
res.once('end', next);
});
});