@@ -26,91 +26,77 @@ const http = require('http');
26
26
const net = require ( 'net' ) ;
27
27
const util = require ( 'util' ) ;
28
28
29
- let outstanding_reqs = 0 ;
30
-
31
- const server = http . createServer ( function ( req , res ) {
32
- res . writeHead ( 200 , [ [ 'content-type' , 'text/plain' ] ] ) ;
33
- res . addTrailers ( { 'x-foo' : 'bar' } ) ;
34
- res . end ( 'stuff\n' ) ;
35
- } ) ;
36
- server . listen ( 0 ) ;
37
-
38
-
39
29
// First, we test an HTTP/1.0 request.
40
- server . on ( 'listening' , function ( ) {
41
- const c = net . createConnection ( this . address ( ) . port ) ;
42
- let res_buffer = '' ;
30
+ function testHttp10 ( port , callback ) {
31
+ const c = net . createConnection ( port ) ;
43
32
44
33
c . setEncoding ( 'utf8' ) ;
45
34
46
- c . on ( 'connect' , function ( ) {
47
- outstanding_reqs ++ ;
35
+ c . on ( 'connect' , ( ) => {
48
36
c . write ( 'GET / HTTP/1.0\r\n\r\n' ) ;
49
37
} ) ;
50
38
51
- c . on ( 'data' , function ( chunk ) {
39
+ let res_buffer = '' ;
40
+ c . on ( 'data' , ( chunk ) => {
52
41
res_buffer += chunk ;
53
42
} ) ;
54
43
55
44
c . on ( 'end' , function ( ) {
56
45
c . end ( ) ;
57
46
assert . ok (
58
47
! / x - f o o / . test ( res_buffer ) ,
59
- `Trailer in HTTP/1.0 response. Response buffer: ${ res_buffer } `
48
+ `No trailer in HTTP/1.0 response. Response buffer: ${ res_buffer } `
60
49
) ;
61
- outstanding_reqs -- ;
62
- if ( outstanding_reqs === 0 ) {
63
- server . close ( ) ;
64
- }
50
+ callback ( ) ;
65
51
} ) ;
66
- } ) ;
52
+ }
67
53
68
54
// Now, we test an HTTP/1.1 request.
69
- server . on ( 'listening' , function ( ) {
70
- const c = net . createConnection ( this . address ( ) . port ) ;
71
- let res_buffer = '' ;
72
- let tid ;
55
+ function testHttp11 ( port , callback ) {
56
+ const c = net . createConnection ( port ) ;
73
57
74
58
c . setEncoding ( 'utf8' ) ;
75
59
60
+ let tid ;
76
61
c . on ( 'connect' , function ( ) {
77
- outstanding_reqs ++ ;
78
62
c . write ( 'GET / HTTP/1.1\r\n\r\n' ) ;
79
63
tid = setTimeout ( common . mustNotCall ( ) , 2000 , 'Couldn\'t find last chunk.' ) ;
80
64
} ) ;
81
65
66
+ let res_buffer = '' ;
82
67
c . on ( 'data' , function ( chunk ) {
83
68
res_buffer += chunk ;
84
69
if ( / 0 \r \n / . test ( res_buffer ) ) { // got the end.
85
- outstanding_reqs -- ;
86
70
clearTimeout ( tid ) ;
87
71
assert . ok (
88
72
/ 0 \r \n x - f o o : b a r \r \n \r \n $ / . test ( res_buffer ) ,
89
73
`No trailer in HTTP/1.1 response. Response buffer: ${ res_buffer } `
90
74
) ;
91
- if ( outstanding_reqs === 0 ) {
92
- server . close ( ) ;
93
- }
75
+ callback ( ) ;
94
76
}
95
77
} ) ;
96
- } ) ;
78
+ }
97
79
98
80
// Now, see if the client sees the trailers.
99
- server . on ( 'listening' , function ( ) {
100
- http . get ( {
101
- port : this . address ( ) . port ,
102
- path : '/hello' ,
103
- headers : { }
104
- } , function ( res ) {
81
+ function testClientTrailers ( port , callback ) {
82
+ http . get ( { port, path : '/hello' , headers : { } } , ( res ) => {
105
83
res . on ( 'end' , function ( ) {
106
84
assert . ok ( 'x-foo' in res . trailers ,
107
85
`${ util . inspect ( res . trailers ) } misses the 'x-foo' property` ) ;
108
- outstanding_reqs -- ;
109
- if ( outstanding_reqs === 0 ) {
110
- server . close ( ) ;
111
- }
86
+ callback ( ) ;
112
87
} ) ;
113
88
res . resume ( ) ;
114
89
} ) ;
115
- outstanding_reqs ++ ;
90
+ }
91
+
92
+ const server = http . createServer ( ( req , res ) => {
93
+ res . writeHead ( 200 , [ [ 'content-type' , 'text/plain' ] ] ) ;
94
+ res . addTrailers ( { 'x-foo' : 'bar' } ) ;
95
+ res . end ( 'stuff\n' ) ;
96
+ } ) ;
97
+ server . listen ( 0 , ( ) => {
98
+ Promise . all ( [ testHttp10 , testHttp11 , testClientTrailers ]
99
+ . map ( util . promisify )
100
+ . map ( ( f ) => f ( server . address ( ) . port ) ) )
101
+ . then ( ( ) => server . close ( ) ) ;
116
102
} ) ;
0 commit comments