Skip to content

Commit c285389

Browse files
Trottaddaleax
authored andcommitted
test: refactor test-http-parser.js
* eliminate CRLF identifier, reducing string concatenation * adjust indentation for stricter ESLint 4.x indentation linting PR-URL: #14431 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent e90af29 commit c285389

File tree

1 file changed

+117
-106
lines changed

1 file changed

+117
-106
lines changed

test/parallel/test-http-parser.js

+117-106
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const binding = process.binding('http_parser');
2727
const methods = binding.methods;
2828
const HTTPParser = binding.HTTPParser;
2929

30-
const CRLF = '\r\n';
3130
const REQUEST = HTTPParser.REQUEST;
3231
const RESPONSE = HTTPParser.RESPONSE;
3332

@@ -92,7 +91,7 @@ function expectBody(expected) {
9291
// Simple request test.
9392
//
9493
{
95-
const request = Buffer.from(`GET /hello HTTP/1.1${CRLF}${CRLF}`);
94+
const request = Buffer.from('GET /hello HTTP/1.1\r\n\r\n');
9695

9796
const onHeadersComplete = (versionMajor, versionMinor, headers,
9897
method, url, statusCode, statusMessage,
@@ -129,11 +128,12 @@ function expectBody(expected) {
129128
//
130129
{
131130
const request = Buffer.from(
132-
'HTTP/1.1 200 OK' + CRLF +
133-
'Content-Type: text/plain' + CRLF +
134-
'Content-Length: 4' + CRLF +
135-
CRLF +
136-
'pong');
131+
'HTTP/1.1 200 OK\r\n' +
132+
'Content-Type: text/plain\r\n' +
133+
'Content-Length: 4\r\n' +
134+
'\r\n' +
135+
'pong'
136+
);
137137

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

167167
const onHeadersComplete = (versionMajor, versionMinor, headers,
168168
method, url, statusCode, statusMessage,
@@ -186,15 +186,16 @@ function expectBody(expected) {
186186
//
187187
{
188188
const request = Buffer.from(
189-
'POST /it HTTP/1.1' + CRLF +
190-
'Transfer-Encoding: chunked' + CRLF +
191-
CRLF +
192-
'4' + CRLF +
193-
'ping' + CRLF +
194-
'0' + CRLF +
195-
'Vary: *' + CRLF +
196-
'Content-Type: text/plain' + CRLF +
197-
CRLF);
189+
'POST /it HTTP/1.1\r\n' +
190+
'Transfer-Encoding: chunked\r\n' +
191+
'\r\n' +
192+
'4\r\n' +
193+
'ping\r\n' +
194+
'0\r\n' +
195+
'Vary: *\r\n' +
196+
'Content-Type: text/plain\r\n' +
197+
'\r\n'
198+
);
198199

199200
let seen_body = false;
200201

@@ -233,11 +234,12 @@ function expectBody(expected) {
233234
//
234235
{
235236
const request = Buffer.from(
236-
'GET / HTTP/1.0' + CRLF +
237-
'X-Filler: 1337' + CRLF +
238-
'X-Filler: 42' + CRLF +
239-
'X-Filler2: 42' + CRLF +
240-
CRLF);
237+
'GET / HTTP/1.0\r\n' +
238+
'X-Filler: 1337\r\n' +
239+
'X-Filler: 42\r\n' +
240+
'X-Filler2: 42\r\n' +
241+
'\r\n'
242+
);
241243

242244
const onHeadersComplete = (versionMajor, versionMinor, headers,
243245
method, url, statusCode, statusMessage,
@@ -246,8 +248,8 @@ function expectBody(expected) {
246248
assert.strictEqual(versionMajor, 1);
247249
assert.strictEqual(versionMinor, 0);
248250
assert.deepStrictEqual(
249-
headers || parser.headers,
250-
['X-Filler', '1337', 'X-Filler', '42', 'X-Filler2', '42']);
251+
headers || parser.headers,
252+
['X-Filler', '1337', 'X-Filler', '42', 'X-Filler2', '42']);
251253
};
252254

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

266268
const request = Buffer.from(
267-
'GET /foo/bar/baz?quux=42#1337 HTTP/1.0' + CRLF +
268-
lots_of_headers +
269-
CRLF);
269+
'GET /foo/bar/baz?quux=42#1337 HTTP/1.0\r\n' +
270+
lots_of_headers +
271+
'\r\n'
272+
);
270273

271274
const onHeadersComplete = (versionMajor, versionMinor, headers,
272275
method, url, statusCode, statusMessage,
@@ -296,11 +299,12 @@ function expectBody(expected) {
296299
//
297300
{
298301
const request = Buffer.from(
299-
'POST /it HTTP/1.1' + CRLF +
300-
'Content-Type: application/x-www-form-urlencoded' + CRLF +
301-
'Content-Length: 15' + CRLF +
302-
CRLF +
303-
'foo=42&bar=1337');
302+
'POST /it HTTP/1.1\r\n' +
303+
'Content-Type: application/x-www-form-urlencoded\r\n' +
304+
'Content-Length: 15\r\n' +
305+
'\r\n' +
306+
'foo=42&bar=1337'
307+
);
304308

305309
const onHeadersComplete = (versionMajor, versionMinor, headers,
306310
method, url, statusCode, statusMessage,
@@ -328,17 +332,18 @@ function expectBody(expected) {
328332
//
329333
{
330334
const request = Buffer.from(
331-
'POST /it HTTP/1.1' + CRLF +
332-
'Content-Type: text/plain' + CRLF +
333-
'Transfer-Encoding: chunked' + CRLF +
334-
CRLF +
335-
'3' + CRLF +
336-
'123' + CRLF +
337-
'6' + CRLF +
338-
'123456' + CRLF +
339-
'A' + CRLF +
340-
'1234567890' + CRLF +
341-
'0' + CRLF);
335+
'POST /it HTTP/1.1\r\n' +
336+
'Content-Type: text/plain\r\n' +
337+
'Transfer-Encoding: chunked\r\n' +
338+
'\r\n' +
339+
'3\r\n' +
340+
'123\r\n' +
341+
'6\r\n' +
342+
'123456\r\n' +
343+
'A\r\n' +
344+
'1234567890\r\n' +
345+
'0\r\n'
346+
);
342347

343348
const onHeadersComplete = (versionMajor, versionMinor, headers,
344349
method, url, statusCode, statusMessage,
@@ -369,14 +374,15 @@ function expectBody(expected) {
369374
//
370375
{
371376
let request = Buffer.from(
372-
'POST /it HTTP/1.1' + CRLF +
373-
'Content-Type: text/plain' + CRLF +
374-
'Transfer-Encoding: chunked' + CRLF +
375-
CRLF +
376-
'3' + CRLF +
377-
'123' + CRLF +
378-
'6' + CRLF +
379-
'123456' + CRLF);
377+
'POST /it HTTP/1.1\r\n' +
378+
'Content-Type: text/plain\r\n' +
379+
'Transfer-Encoding: chunked\r\n' +
380+
'\r\n' +
381+
'3\r\n' +
382+
'123\r\n' +
383+
'6\r\n' +
384+
'123456\r\n'
385+
);
380386

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

404410
request = Buffer.from(
405-
'9' + CRLF +
406-
'123456789' + CRLF +
407-
'C' + CRLF +
408-
'123456789ABC' + CRLF +
409-
'F' + CRLF +
410-
'123456789ABCDEF' + CRLF +
411-
'0' + CRLF);
411+
'9\r\n' +
412+
'123456789\r\n' +
413+
'C\r\n' +
414+
'123456789ABC\r\n' +
415+
'F\r\n' +
416+
'123456789ABCDEF\r\n' +
417+
'0\r\n'
418+
);
412419

413420
parser.execute(request, 0, request.length);
414421
}
@@ -419,21 +426,22 @@ function expectBody(expected) {
419426
//
420427
{
421428
const request = Buffer.from(
422-
'POST /helpme HTTP/1.1' + CRLF +
423-
'Content-Type: text/plain' + CRLF +
424-
'Transfer-Encoding: chunked' + CRLF +
425-
CRLF +
426-
'3' + CRLF +
427-
'123' + CRLF +
428-
'6' + CRLF +
429-
'123456' + CRLF +
430-
'9' + CRLF +
431-
'123456789' + CRLF +
432-
'C' + CRLF +
433-
'123456789ABC' + CRLF +
434-
'F' + CRLF +
435-
'123456789ABCDEF' + CRLF +
436-
'0' + CRLF);
429+
'POST /helpme HTTP/1.1\r\n' +
430+
'Content-Type: text/plain\r\n' +
431+
'Transfer-Encoding: chunked\r\n' +
432+
'\r\n' +
433+
'3\r\n' +
434+
'123\r\n' +
435+
'6\r\n' +
436+
'123456\r\n' +
437+
'9\r\n' +
438+
'123456789\r\n' +
439+
'C\r\n' +
440+
'123456789ABC\r\n' +
441+
'F\r\n' +
442+
'123456789ABCDEF\r\n' +
443+
'0\r\n'
444+
);
437445

438446
function test(a, b) {
439447
const onHeadersComplete = (versionMajor, versionMinor, headers,
@@ -477,21 +485,22 @@ function expectBody(expected) {
477485
//
478486
{
479487
const request = Buffer.from(
480-
'POST /it HTTP/1.1' + CRLF +
481-
'Content-Type: text/plain' + CRLF +
482-
'Transfer-Encoding: chunked' + CRLF +
483-
CRLF +
484-
'3' + CRLF +
485-
'123' + CRLF +
486-
'6' + CRLF +
487-
'123456' + CRLF +
488-
'9' + CRLF +
489-
'123456789' + CRLF +
490-
'C' + CRLF +
491-
'123456789ABC' + CRLF +
492-
'F' + CRLF +
493-
'123456789ABCDEF' + CRLF +
494-
'0' + CRLF);
488+
'POST /it HTTP/1.1\r\n' +
489+
'Content-Type: text/plain\r\n' +
490+
'Transfer-Encoding: chunked\r\n' +
491+
'\r\n' +
492+
'3\r\n' +
493+
'123\r\n' +
494+
'6\r\n' +
495+
'123456\r\n' +
496+
'9\r\n' +
497+
'123456789\r\n' +
498+
'C\r\n' +
499+
'123456789ABC\r\n' +
500+
'F\r\n' +
501+
'123456789ABCDEF\r\n' +
502+
'0\r\n'
503+
);
495504

496505
const onHeadersComplete = (versionMajor, versionMinor, headers,
497506
method, url, statusCode, statusMessage,
@@ -501,8 +510,8 @@ function expectBody(expected) {
501510
assert.strictEqual(versionMajor, 1);
502511
assert.strictEqual(versionMinor, 1);
503512
assert.deepStrictEqual(
504-
headers || parser.headers,
505-
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
513+
headers || parser.headers,
514+
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
506515
};
507516

508517
let expected_body = '123123456123456789123456789ABC123456789ABCDEF';
@@ -530,20 +539,22 @@ function expectBody(expected) {
530539
//
531540
{
532541
const req1 = Buffer.from(
533-
'PUT /this HTTP/1.1' + CRLF +
534-
'Content-Type: text/plain' + CRLF +
535-
'Transfer-Encoding: chunked' + CRLF +
536-
CRLF +
537-
'4' + CRLF +
538-
'ping' + CRLF +
539-
'0' + CRLF);
542+
'PUT /this HTTP/1.1\r\n' +
543+
'Content-Type: text/plain\r\n' +
544+
'Transfer-Encoding: chunked\r\n' +
545+
'\r\n' +
546+
'4\r\n' +
547+
'ping\r\n' +
548+
'0\r\n'
549+
);
540550

541551
const req2 = Buffer.from(
542-
'POST /that HTTP/1.0' + CRLF +
543-
'Content-Type: text/plain' + CRLF +
544-
'Content-Length: 4' + CRLF +
545-
CRLF +
546-
'pong');
552+
'POST /that HTTP/1.0\r\n' +
553+
'Content-Type: text/plain\r\n' +
554+
'Content-Length: 4\r\n' +
555+
'\r\n' +
556+
'pong'
557+
);
547558

548559
const onHeadersComplete1 = (versionMajor, versionMinor, headers,
549560
method, url, statusCode, statusMessage,
@@ -553,8 +564,8 @@ function expectBody(expected) {
553564
assert.strictEqual(versionMajor, 1);
554565
assert.strictEqual(versionMinor, 1);
555566
assert.deepStrictEqual(
556-
headers,
557-
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
567+
headers,
568+
['Content-Type', 'text/plain', 'Transfer-Encoding', 'chunked']);
558569
};
559570

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

589600
const parser = newParser(REQUEST);
590601
const notparser = { execute: parser.execute };

0 commit comments

Comments
 (0)