@@ -3,6 +3,7 @@ require('../common');
33const assert = require ( 'assert' ) ;
44const http = require ( 'http' ) ;
55const net = require ( 'net' ) ;
6+ const Countdown = require ( '../common/countdown' ) ;
67
78const SERVER_RESPONSES = [
89 'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n' ,
@@ -20,34 +21,27 @@ const SHOULD_KEEP_ALIVE = [
2021 true , // HTTP/1.1, Connection: keep-alive
2122 false // HTTP/1.1, Connection: close
2223] ;
23- let requests = 0 ;
24- let responses = 0 ;
2524http . globalAgent . maxSockets = 5 ;
2625
26+ const countdown = new Countdown ( SHOULD_KEEP_ALIVE . length , ( ) => server . close ( ) ) ;
27+
28+ const getCountdownIndex = ( ) => SERVER_RESPONSES . length - countdown . remaining ;
29+
2730const server = net . createServer ( function ( socket ) {
28- socket . write ( SERVER_RESPONSES [ requests ] ) ;
29- ++ requests ;
31+ socket . write ( SERVER_RESPONSES [ getCountdownIndex ( ) ] ) ;
3032} ) . listen ( 0 , function ( ) {
3133 function makeRequest ( ) {
3234 const req = http . get ( { port : server . address ( ) . port } , function ( res ) {
3335 assert . strictEqual (
34- req . shouldKeepAlive , SHOULD_KEEP_ALIVE [ responses ] ,
35- `${ SERVER_RESPONSES [ responses ] } should ${
36- SHOULD_KEEP_ALIVE [ responses ] ? '' : 'not ' } Keep-Alive`) ;
37- ++ responses ;
38- if ( responses < SHOULD_KEEP_ALIVE . length ) {
36+ req . shouldKeepAlive , SHOULD_KEEP_ALIVE [ getCountdownIndex ( ) ] ,
37+ `${ SERVER_RESPONSES [ getCountdownIndex ( ) ] } should ${
38+ SHOULD_KEEP_ALIVE [ getCountdownIndex ( ) ] ? '' : 'not ' } Keep-Alive`) ;
39+ countdown . dec ( ) ;
40+ if ( countdown . remaining ) {
3941 makeRequest ( ) ;
40- } else {
41- server . close ( ) ;
4242 }
4343 res . resume ( ) ;
4444 } ) ;
4545 }
46-
4746 makeRequest ( ) ;
4847} ) ;
49-
50- process . on ( 'exit' , function ( ) {
51- assert . strictEqual ( requests , SERVER_RESPONSES . length ) ;
52- assert . strictEqual ( responses , SHOULD_KEEP_ALIVE . length ) ;
53- } ) ;
0 commit comments