@@ -539,3 +539,100 @@ test('request should include statusText in response', async t => {
539539 await body . dump ( )
540540 t . ok ( 'request completed' )
541541} )
542+
543+ describe ( 'connection header per RFC 7230' , ( ) => {
544+ test ( 'should allow close' , async ( t ) => {
545+ t = tspl ( t , { plan : 1 } )
546+
547+ const server = createServer ( ( req , res ) => {
548+ res . end ( 'ok' )
549+ } )
550+
551+ after ( ( ) => server . close ( ) )
552+ await new Promise ( ( resolve ) => server . listen ( 0 , resolve ) )
553+
554+ const { statusCode, body } = await request ( {
555+ method : 'GET' ,
556+ origin : `http://localhost:${ server . address ( ) . port } ` ,
557+ headers : { connection : 'close' }
558+ } )
559+ await body . dump ( )
560+ t . strictEqual ( statusCode , 200 )
561+ } )
562+
563+ test ( 'should allow keep-alive' , async ( t ) => {
564+ t = tspl ( t , { plan : 1 } )
565+
566+ const server = createServer ( ( req , res ) => {
567+ res . end ( 'ok' )
568+ } )
569+
570+ after ( ( ) => server . close ( ) )
571+ await new Promise ( ( resolve ) => server . listen ( 0 , resolve ) )
572+
573+ const { statusCode, body } = await request ( {
574+ method : 'GET' ,
575+ origin : `http://localhost:${ server . address ( ) . port } ` ,
576+ headers : { connection : 'keep-alive' }
577+ } )
578+ await body . dump ( )
579+ t . strictEqual ( statusCode , 200 )
580+ } )
581+
582+ test ( 'should allow custom header name as connection option' , async ( t ) => {
583+ t = tspl ( t , { plan : 1 } )
584+
585+ const server = createServer ( ( req , res ) => {
586+ res . end ( 'ok' )
587+ } )
588+
589+ after ( ( ) => server . close ( ) )
590+ await new Promise ( ( resolve ) => server . listen ( 0 , resolve ) )
591+
592+ const { statusCode, body } = await request ( {
593+ method : 'GET' ,
594+ origin : `http://localhost:${ server . address ( ) . port } ` ,
595+ headers : {
596+ 'x-custom-header' : 'value' ,
597+ connection : 'x-custom-header'
598+ }
599+ } )
600+ await body . dump ( )
601+ t . strictEqual ( statusCode , 200 )
602+ } )
603+
604+ test ( 'should allow comma-separated list of connection options' , async ( t ) => {
605+ t = tspl ( t , { plan : 1 } )
606+
607+ const server = createServer ( ( req , res ) => {
608+ res . end ( 'ok' )
609+ } )
610+
611+ after ( ( ) => server . close ( ) )
612+ await new Promise ( ( resolve ) => server . listen ( 0 , resolve ) )
613+
614+ const { statusCode, body } = await request ( {
615+ method : 'GET' ,
616+ origin : `http://localhost:${ server . address ( ) . port } ` ,
617+ headers : {
618+ 'x-custom-header' : 'value' ,
619+ connection : 'close, x-custom-header'
620+ }
621+ } )
622+ await body . dump ( )
623+ t . strictEqual ( statusCode , 200 )
624+ } )
625+
626+ test ( 'should reject invalid tokens in connection header' , async ( t ) => {
627+ t = tspl ( t , { plan : 2 } )
628+
629+ await request ( {
630+ method : 'GET' ,
631+ origin : 'http://localhost:1234' ,
632+ headers : { connection : 'invalid header with spaces' }
633+ } ) . catch ( ( err ) => {
634+ t . ok ( err instanceof errors . InvalidArgumentError )
635+ t . strictEqual ( err . message , 'invalid connection header' )
636+ } )
637+ } )
638+ } )
0 commit comments