@@ -3,6 +3,7 @@ require('../common');
3
3
const assert = require ( 'assert' ) ;
4
4
const http = require ( 'http' ) ;
5
5
const net = require ( 'net' ) ;
6
+ const Countdown = require ( '../common/countdown' ) ;
6
7
7
8
const SERVER_RESPONSES = [
8
9
'HTTP/1.0 200 ok\r\nContent-Length: 0\r\n\r\n' ,
@@ -20,34 +21,27 @@ const SHOULD_KEEP_ALIVE = [
20
21
true , // HTTP/1.1, Connection: keep-alive
21
22
false // HTTP/1.1, Connection: close
22
23
] ;
23
- let requests = 0 ;
24
- let responses = 0 ;
25
24
http . globalAgent . maxSockets = 5 ;
26
25
26
+ const countdown = new Countdown ( SHOULD_KEEP_ALIVE . length , ( ) => server . close ( ) ) ;
27
+
28
+ const getCountdownIndex = ( ) => SERVER_RESPONSES . length - countdown . remaining ;
29
+
27
30
const server = net . createServer ( function ( socket ) {
28
- socket . write ( SERVER_RESPONSES [ requests ] ) ;
29
- ++ requests ;
31
+ socket . write ( SERVER_RESPONSES [ getCountdownIndex ( ) ] ) ;
30
32
} ) . listen ( 0 , function ( ) {
31
33
function makeRequest ( ) {
32
34
const req = http . get ( { port : server . address ( ) . port } , function ( res ) {
33
35
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 ) {
39
41
makeRequest ( ) ;
40
- } else {
41
- server . close ( ) ;
42
42
}
43
43
res . resume ( ) ;
44
44
} ) ;
45
45
}
46
-
47
46
makeRequest ( ) ;
48
47
} ) ;
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