Skip to content

Instantly share code, notes, and snippets.

@brendanashworth
Created June 23, 2015 21:54
Show Gist options
  • Save brendanashworth/e5b06029c98e4f728781 to your computer and use it in GitHub Desktop.
Save brendanashworth/e5b06029c98e4f728781 to your computer and use it in GitHub Desktop.
http parser test
'use strict';
// switch them around to see difference in results
//const HTTPParser = process.binding('http_parser').HTTPParser;
const HTTPParser = require('_http_parser');
var parser = new HTTPParser(HTTPParser.RESPONSE);
var buf;
if (1) {
parser.reinitialize(HTTPParser.RESPONSE);
// actual data -> HTTP/1.1 200 OK...
buf = new Buffer('xxxxxxx1 200 OK\r\nX-Header: data\r\n\r\n', 'ascii');
assert(parser.execute(buf) instanceof Error);
}
if (2) {
parser.reinitialize(HTTPParser.RESPONSE);
// actual data -> HTTP/1.1 200 OK\r\n...
buf = new Buffer('HTTP/1.1 200xOK\r\n\r\n');
assert(parser.execute(buf) instanceof Error);
}
// Note to Brian: the current parser does not seem to error on this case,
// but to me it looks like a malformed input. Is this not the case?
if (3) {
parser.reinitialize(HTTPParser.REQUEST);
// actual data -> GET /url HTTP/1.1\r\n...
buf = new Buffer('GET /url HTTP/1.1\r\nX-Data: haxked\r\n\r\n');
assert(parser.execute(buf) instanceof Error);
}
var i;
function assert(check) {
if (i === undefined) i = 0;
if (check) {
console.log('passed assertion %d', ++i);
} else {
console.log('failed assertion %d', ++i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment