@ -72,21 +72,21 @@ function expectBody(expected) {
//
// Simple request test.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'GET /hello HTTP/1.1' + CRLF +
CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
assert . e qual( method , methods . indexOf ( 'GET' ) ) ;
assert . e qual( url || parser . url , '/hello' ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
assert . strictE qual( method , methods . indexOf ( 'GET' ) ) ;
assert . strictE qual( url || parser . url , '/hello' ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser . execute ( request , 0 , request . length ) ;
@ -104,72 +104,72 @@ function expectBody(expected) {
assert . throws ( function ( ) {
parser . execute ( request , 0 , request . length ) ;
} , Error , 'hello world' ) ;
} ) ( ) ;
}
//
// Simple response test.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'HTTP/1.1 200 OK' + CRLF +
'Content-Type: text/plain' + CRLF +
'Content-Length: 4' + CRLF +
CRLF +
'pong' ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , undefined ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
assert . e qual( statusCode , 200 ) ;
assert . e qual( statusMessage , 'OK' ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , undefined ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
assert . strictE qual( statusCode , 200 ) ;
assert . strictE qual( statusMessage , 'OK' ) ;
} ;
var onBody = function ( buf , start , len ) {
var body = '' + buf . slice ( start , start + len ) ;
assert . e qual( body , 'pong' ) ;
const onBody = function ( buf , start , len ) {
const body = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( body , 'pong' ) ;
} ;
var parser = newParser ( RESPONSE ) ;
const parser = newParser ( RESPONSE ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = mustCall ( onBody ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Response with no headers.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'HTTP/1.0 200 Connection established' + CRLF +
CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 0 ) ;
assert . e qual( method , undefined ) ;
assert . e qual( statusCode , 200 ) ;
assert . e qual( statusMessage , 'Connection established' ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 0 ) ;
assert . strictE qual( method , undefined ) ;
assert . strictE qual( statusCode , 200 ) ;
assert . strictE qual( statusMessage , 'Connection established' ) ;
assert . deepStrictEqual ( headers || parser . headers , [ ] ) ;
} ;
var parser = newParser ( RESPONSE ) ;
const parser = newParser ( RESPONSE ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Trailing headers.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'POST /it HTTP/1.1' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
CRLF +
@ -180,139 +180,139 @@ function expectBody(expected) {
'Content-Type: text/plain' + CRLF +
CRLF ) ;
var seen_body = false ;
let seen_body = false ;
var onHeaders = function ( headers , url ) {
const onHeaders = function ( headers , url ) {
assert . ok ( seen_body ) ; // trailers should come after the body
assert . deepStrictEqual ( headers ,
[ 'Vary' , '*' , 'Content-Type' , 'text/plain' ] ) ;
} ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url || parser . url , '/it' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url || parser . url , '/it' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
// expect to see trailing headers now
parser [ kOnHeaders ] = mustCall ( onHeaders ) ;
} ;
var onBody = function ( buf , start , len ) {
var body = '' + buf . slice ( start , start + len ) ;
assert . e qual( body , 'ping' ) ;
const onBody = function ( buf , start , len ) {
const body = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( body , 'ping' ) ;
seen_body = true ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = mustCall ( onBody ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Test header ordering.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'GET / HTTP/1.0' + CRLF +
'X-Filler: 1337' + CRLF +
'X-Filler: 42' + CRLF +
'X-Filler2: 42' + CRLF +
CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'GET' ) ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 0 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'GET' ) ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 0 ) ;
assert . deepStrictEqual (
headers || parser . headers ,
[ 'X-Filler' , '1337' , 'X-Filler' , '42' , 'X-Filler2' , '42' ] ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Test large number of headers
//
( function ( ) {
{
// 256 X-Filler headers
var lots_of_headers = 'X-Filler: 42' + CRLF ;
let lots_of_headers = 'X-Filler: 42' + CRLF ;
lots_of_headers = lots_of_headers . repeat ( 256 ) ;
var request = Buffer . from (
const request = Buffer . from (
'GET /foo/bar/baz?quux=42#1337 HTTP/1.0' + CRLF +
lots_of_headers +
CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'GET' ) ) ;
assert . e qual( url || parser . url , '/foo/bar/baz?quux=42#1337' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 0 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'GET' ) ) ;
assert . strictE qual( url || parser . url , '/foo/bar/baz?quux=42#1337' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 0 ) ;
headers = headers || parser . headers ;
assert . e qual( headers . length , 2 * 256 ) ; // 256 key/value pairs
for ( var i = 0 ; i < headers . length ; i += 2 ) {
assert . e qual( headers [ i ] , 'X-Filler' ) ;
assert . e qual( headers [ i + 1 ] , '42' ) ;
assert . strictE qual( headers . length , 2 * 256 ) ; // 256 key/value pairs
for ( let i = 0 ; i < headers . length ; i += 2 ) {
assert . strictE qual( headers [ i ] , 'X-Filler' ) ;
assert . strictE qual( headers [ i + 1 ] , '42' ) ;
}
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Test request body
//
( function ( ) {
var request = Buffer . from (
{
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' ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url || parser . url , '/it' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url || parser . url , '/it' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
} ;
var onBody = function ( buf , start , len ) {
var body = '' + buf . slice ( start , start + len ) ;
assert . e qual( body , 'foo=42&bar=1337' ) ;
const onBody = function ( buf , start , len ) {
const body = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( body , 'foo=42&bar=1337' ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = mustCall ( onBody ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Test chunked request body
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'POST /it HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
@ -325,35 +325,35 @@ function expectBody(expected) {
'1234567890' + CRLF +
'0' + CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url || parser . url , '/it' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url || parser . url , '/it' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
} ;
let body_part = 0 ;
const body_parts = [ '123' , '123456' , '1234567890' ] ;
var onBody = function ( buf , start , len ) {
var body = '' + buf . slice ( start , start + len ) ;
assert . e qual( body , body_parts [ body_part ++ ] ) ;
const onBody = function ( buf , start , len ) {
const body = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( body , body_parts [ body_part ++ ] ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = mustCall ( onBody , body_parts . length ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Test chunked request body spread over multiple buffers (packets)
//
( function ( ) {
var request = Buffer . from (
{
let request = Buffer . from (
'POST /it HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
@ -363,25 +363,25 @@ function expectBody(expected) {
'6' + CRLF +
'123456' + CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url || parser . url , '/it' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url || parser . url , '/it' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
} ;
let body_part = 0 ;
const body_parts =
[ '123' , '123456' , '123456789' , '123456789ABC' , '123456789ABCDEF' ] ;
var onBody = function ( buf , start , len ) {
var body = '' + buf . slice ( start , start + len ) ;
assert . e qual( body , body_parts [ body_part ++ ] ) ;
const onBody = function ( buf , start , len ) {
const body = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( body , body_parts [ body_part ++ ] ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = mustCall ( onBody , body_parts . length ) ;
parser . execute ( request , 0 , request . length ) ;
@ -396,14 +396,14 @@ function expectBody(expected) {
'0' + CRLF ) ;
parser . execute ( request , 0 , request . length ) ;
} ) ( ) ;
}
//
// Stress test.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'POST /helpme HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
@ -421,30 +421,30 @@ function expectBody(expected) {
'0' + CRLF ) ;
function test ( a , b ) {
var onHeadersComplete = function ( versionMajor , versionMinor , headers ,
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url || parser . url , '/helpme' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url || parser . url , '/helpme' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
} ;
var expected_body = '123123456123456789123456789ABC123456789ABCDEF' ;
let expected_body = '123123456123456789123456789ABC123456789ABCDEF' ;
var onBody = function ( buf , start , len ) {
var chunk = '' + buf . slice ( start , start + len ) ;
assert . e qual( expected_body . indexOf ( chunk ) , 0 ) ;
const onBody = function ( buf , start , len ) {
const chunk = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( expected_body . indexOf ( chunk ) , 0 ) ;
expected_body = expected_body . slice ( chunk . length ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = onBody ;
parser . execute ( a , 0 , a . length ) ;
parser . execute ( b , 0 , b . length ) ;
assert . e qual( expected_body , '' ) ;
assert . strictE qual( expected_body , '' ) ;
}
for ( var i = 1 ; i < request . length - 1 ; ++ i ) {
@ -456,14 +456,14 @@ function expectBody(expected) {
JSON . stringify ( b . toString ( ) ) ) ;
test ( a , b ) ;
}
} ) ( ) ;
}
//
// Byte by byte test.
//
( function ( ) {
var request = Buffer . from (
{
const request = Buffer . from (
'POST /it HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
@ -480,43 +480,43 @@ function expectBody(expected) {
'123456789ABCDEF' + CRLF +
'0' + CRLF ) ;
var onHeadersComplete = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url || parser . url , '/it' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 1 ) ;
const onHeadersComplete = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url || parser . url , '/it' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 1 ) ;
assert . deepStrictEqual (
headers || parser . headers ,
[ 'Content-Type' , 'text/plain' , 'Transfer-Encoding' , 'chunked' ] ) ;
} ;
var expected_body = '123123456123456789123456789ABC123456789ABCDEF' ;
let expected_body = '123123456123456789123456789ABC123456789ABCDEF' ;
var onBody = function ( buf , start , len ) {
var chunk = '' + buf . slice ( start , start + len ) ;
assert . e qual( expected_body . indexOf ( chunk ) , 0 ) ;
const onBody = function ( buf , start , len ) {
const chunk = '' + buf . slice ( start , start + len ) ;
assert . strictE qual( expected_body . indexOf ( chunk ) , 0 ) ;
expected_body = expected_body . slice ( chunk . length ) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = mustCall ( onHeadersComplete ) ;
parser [ kOnBody ] = onBody ;
for ( var i = 0 ; i < request . length ; ++ i ) {
for ( let i = 0 ; i < request . length ; ++ i ) {
parser . execute ( request , i , 1 ) ;
}
assert . e qual( expected_body , '' ) ;
} ) ( ) ;
assert . strictE qual( expected_body , '' ) ;
}
//
// Test parser reinit sequence.
//
( function ( ) {
var req1 = Buffer . from (
{
const req1 = Buffer . from (
'PUT /this HTTP/1.1' + CRLF +
'Content-Type: text/plain' + CRLF +
'Transfer-Encoding: chunked' + CRLF +
@ -525,16 +525,16 @@ function expectBody(expected) {
'ping' + CRLF +
'0' + CRLF ) ;
var req2 = Buffer . from (
const req2 = Buffer . from (
'POST /that HTTP/1.0' + CRLF +
'Content-Type: text/plain' + CRLF +
'Content-Length: 4' + CRLF +
CRLF +
'pong' ) ;
var onHeadersComplete1 = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
const onHeadersComplete1 = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . equal ( method , methods . indexOf ( 'PUT' ) ) ;
assert . equal ( url , '/this' ) ;
assert . equal ( versionMajor , 1 ) ;
@ -544,20 +544,20 @@ function expectBody(expected) {
[ 'Content-Type' , 'text/plain' , 'Transfer-Encoding' , 'chunked' ] ) ;
} ;
var onHeadersComplete2 = function ( versionMajor , versionMinor , headers , method ,
url , statusCode , statusMessage , upgrade ,
shouldKeepAlive ) {
assert . e qual( method , methods . indexOf ( 'POST' ) ) ;
assert . e qual( url , '/that' ) ;
assert . e qual( versionMajor , 1 ) ;
assert . e qual( versionMinor , 0 ) ;
const onHeadersComplete2 = function ( versionMajor , versionMinor , headers ,
method , url , statusCode , statusMessage ,
upgrade , shouldKeepAlive ) {
assert . strictE qual( method , methods . indexOf ( 'POST' ) ) ;
assert . strictE qual( url , '/that' ) ;
assert . strictE qual( versionMajor , 1 ) ;
assert . strictE qual( versionMinor , 0 ) ;
assert . deepStrictEqual (
headers ,
[ 'Content-Type' , 'text/plain' , 'Content-Length' , '4' ]
) ;
} ;
var parser = newParser ( REQUEST ) ;
const parser = newParser ( REQUEST ) ;
parser [ kOnHeadersComplete ] = onHeadersComplete1 ;
parser [ kOnBody ] = expectBody ( 'ping' ) ;
parser . execute ( req1 , 0 , req1 . length ) ;
@ -566,7 +566,7 @@ function expectBody(expected) {
parser [ kOnBody ] = expectBody ( 'pong' ) ;
parser [ kOnHeadersComplete ] = onHeadersComplete2 ;
parser . execute ( req2 , 0 , req2 . length ) ;
} ) ( ) ;
}
// Test parser 'this' safety
// https://github.com/joyent/node/issues/6690