Skip to content

Commit

Permalink
test: refactor test-http-parser.js
Browse files Browse the repository at this point in the history
* eliminate CRLF identifier, reducing string concatenation
* adjust indentation for stricter ESLint 4.x indentation linting

PR-URL: #14431
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
Trott authored and addaleax committed Jul 27, 2017
1 parent e90af29 commit c285389
Showing 1 changed file with 117 additions and 106 deletions.
223 changes: 117 additions & 106 deletions test/parallel/test-http-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const binding = process.binding('http_parser');
const methods = binding.methods;
const HTTPParser = binding.HTTPParser;

const CRLF = '\r\n';
const REQUEST = HTTPParser.REQUEST;
const RESPONSE = HTTPParser.RESPONSE;

Expand Down Expand Up @@ -92,7 +91,7 @@ function expectBody(expected) {
// Simple request test.
//
{
const request = Buffer.from(`GET /hello HTTP/1.1${CRLF}${CRLF}`);
const request = Buffer.from('GET /hello HTTP/1.1\r\n\r\n');

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand Down Expand Up @@ -129,11 +128,12 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'HTTP/1.1 200 OK' + CRLF +
'Content-Type: text/plain' + CRLF +
'Content-Length: 4' + CRLF +
CRLF +
'pong');
'HTTP/1.1 200 OK\r\n' +
'Content-Type: text/plain\r\n' +
'Content-Length: 4\r\n' +
'\r\n' +
'pong'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand Down Expand Up @@ -162,7 +162,7 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
`HTTP/1.0 200 Connection established${CRLF}${CRLF}`);
'HTTP/1.0 200 Connection established\r\n\r\n');

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand All @@ -186,15 +186,16 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'POST /it HTTP/1.1' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
'4' + CRLF +
'ping' + CRLF +
'0' + CRLF +
'Vary: *' + CRLF +
'Content-Type: text/plain' + CRLF +
CRLF);
'POST /it HTTP/1.1\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n' +
'4\r\n' +
'ping\r\n' +
'0\r\n' +
'Vary: *\r\n' +
'Content-Type: text/plain\r\n' +
'\r\n'
);

let seen_body = false;

Expand Down Expand Up @@ -233,11 +234,12 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'GET / HTTP/1.0' + CRLF +
'X-Filler: 1337' + CRLF +
'X-Filler: 42' + CRLF +
'X-Filler2: 42' + CRLF +
CRLF);
'GET / HTTP/1.0\r\n' +
'X-Filler: 1337\r\n' +
'X-Filler: 42\r\n' +
'X-Filler2: 42\r\n' +
'\r\n'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand All @@ -246,8 +248,8 @@ function expectBody(expected) {
assert.strictEqual(versionMajor, 1);
assert.strictEqual(versionMinor, 0);
assert.deepStrictEqual(
headers || parser.headers,
['X-Filler', '1337', 'X-Filler', '42', 'X-Filler2', '42']);
headers || parser.headers,
['X-Filler', '1337', 'X-Filler', '42', 'X-Filler2', '42']);
};

const parser = newParser(REQUEST);
Expand All @@ -261,12 +263,13 @@ function expectBody(expected) {
//
{
// 256 X-Filler headers
const lots_of_headers = `X-Filler: 42${CRLF}`.repeat(256);
const lots_of_headers = 'X-Filler: 42\r\n'.repeat(256);

const request = Buffer.from(
'GET /foo/bar/baz?quux=42#1337 HTTP/1.0' + CRLF +
lots_of_headers +
CRLF);
'GET /foo/bar/baz?quux=42#1337 HTTP/1.0\r\n' +
lots_of_headers +
'\r\n'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand Down Expand Up @@ -296,11 +299,12 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'POST /it HTTP/1.1' + CRLF +
'Content-Type: application/x-www-form-urlencoded' + CRLF +
'Content-Length: 15' + CRLF +
CRLF +
'foo=42&bar=1337');
'POST /it HTTP/1.1\r\n' +
'Content-Type: application/x-www-form-urlencoded\r\n' +
'Content-Length: 15\r\n' +
'\r\n' +
'foo=42&bar=1337'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand Down Expand Up @@ -328,17 +332,18 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'POST /it HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
'3' + CRLF +
'123' + CRLF +
'6' + CRLF +
'123456' + CRLF +
'A' + CRLF +
'1234567890' + CRLF +
'0' + CRLF);
'POST /it HTTP/1.1\r\n' +
'Content-Type: text/plain\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n' +
'3\r\n' +
'123\r\n' +
'6\r\n' +
'123456\r\n' +
'A\r\n' +
'1234567890\r\n' +
'0\r\n'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand Down Expand Up @@ -369,14 +374,15 @@ function expectBody(expected) {
//
{
let request = Buffer.from(
'POST /it HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
'3' + CRLF +
'123' + CRLF +
'6' + CRLF +
'123456' + CRLF);
'POST /it HTTP/1.1\r\n' +
'Content-Type: text/plain\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n' +
'3\r\n' +
'123\r\n' +
'6\r\n' +
'123456\r\n'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand All @@ -402,13 +408,14 @@ function expectBody(expected) {
parser.execute(request, 0, request.length);

request = Buffer.from(
'9' + CRLF +
'123456789' + CRLF +
'C' + CRLF +
'123456789ABC' + CRLF +
'F' + CRLF +
'123456789ABCDEF' + CRLF +
'0' + CRLF);
'9\r\n' +
'123456789\r\n' +
'C\r\n' +
'123456789ABC\r\n' +
'F\r\n' +
'123456789ABCDEF\r\n' +
'0\r\n'
);

parser.execute(request, 0, request.length);
}
Expand All @@ -419,21 +426,22 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'POST /helpme HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
'3' + CRLF +
'123' + CRLF +
'6' + CRLF +
'123456' + CRLF +
'9' + CRLF +
'123456789' + CRLF +
'C' + CRLF +
'123456789ABC' + CRLF +
'F' + CRLF +
'123456789ABCDEF' + CRLF +
'0' + CRLF);
'POST /helpme HTTP/1.1\r\n' +
'Content-Type: text/plain\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n' +
'3\r\n' +
'123\r\n' +
'6\r\n' +
'123456\r\n' +
'9\r\n' +
'123456789\r\n' +
'C\r\n' +
'123456789ABC\r\n' +
'F\r\n' +
'123456789ABCDEF\r\n' +
'0\r\n'
);

function test(a, b) {
const onHeadersComplete = (versionMajor, versionMinor, headers,
Expand Down Expand Up @@ -477,21 +485,22 @@ function expectBody(expected) {
//
{
const request = Buffer.from(
'POST /it HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
'3' + CRLF +
'123' + CRLF +
'6' + CRLF +
'123456' + CRLF +
'9' + CRLF +
'123456789' + CRLF +
'C' + CRLF +
'123456789ABC' + CRLF +
'F' + CRLF +
'123456789ABCDEF' + CRLF +
'0' + CRLF);
'POST /it HTTP/1.1\r\n' +
'Content-Type: text/plain\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n' +
'3\r\n' +
'123\r\n' +
'6\r\n' +
'123456\r\n' +
'9\r\n' +
'123456789\r\n' +
'C\r\n' +
'123456789ABC\r\n' +
'F\r\n' +
'123456789ABCDEF\r\n' +
'0\r\n'
);

const onHeadersComplete = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand All @@ -501,8 +510,8 @@ function expectBody(expected) {
assert.strictEqual(versionMajor, 1);
assert.strictEqual(versionMinor, 1);
assert.deepStrictEqual(
headers || parser.headers,
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
headers || parser.headers,
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
};

let expected_body = '123123456123456789123456789ABC123456789ABCDEF';
Expand Down Expand Up @@ -530,20 +539,22 @@ function expectBody(expected) {
//
{
const req1 = Buffer.from(
'PUT /this HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
'4' + CRLF +
'ping' + CRLF +
'0' + CRLF);
'PUT /this HTTP/1.1\r\n' +
'Content-Type: text/plain\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n' +
'4\r\n' +
'ping\r\n' +
'0\r\n'
);

const req2 = Buffer.from(
'POST /that HTTP/1.0' + CRLF +
'Content-Type: text/plain' + CRLF +
'Content-Length: 4' + CRLF +
CRLF +
'pong');
'POST /that HTTP/1.0\r\n' +
'Content-Type: text/plain\r\n' +
'Content-Length: 4\r\n' +
'\r\n' +
'pong'
);

const onHeadersComplete1 = (versionMajor, versionMinor, headers,
method, url, statusCode, statusMessage,
Expand All @@ -553,8 +564,8 @@ function expectBody(expected) {
assert.strictEqual(versionMajor, 1);
assert.strictEqual(versionMinor, 1);
assert.deepStrictEqual(
headers,
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
headers,
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
};

const onHeadersComplete2 = (versionMajor, versionMinor, headers,
Expand Down Expand Up @@ -584,7 +595,7 @@ function expectBody(expected) {
// Test parser 'this' safety
// https://github.com/joyent/node/issues/6690
assert.throws(function() {
const request = Buffer.from(`GET /hello HTTP/1.1${CRLF}${CRLF}`);
const request = Buffer.from('GET /hello HTTP/1.1\r\n\r\n');

const parser = newParser(REQUEST);
const notparser = { execute: parser.execute };
Expand Down

0 comments on commit c285389

Please sign in to comment.