@@ -27,6 +27,26 @@ const {
27
27
formatHttpResponse
28
28
} = require ( '../' )
29
29
30
+ async function makeARequest ( url , opts ) {
31
+ return new Promise ( ( resolve , reject ) => {
32
+ const clientReq = http . request ( url , opts , function ( clientRes ) {
33
+ const chunks = [ ]
34
+ clientRes . on ( 'data' , function ( chunk ) {
35
+ chunks . push ( chunk )
36
+ } )
37
+ clientRes . on ( 'end' , function ( ) {
38
+ resolve ( {
39
+ statusCode : clientRes . statusCode ,
40
+ headers : clientRes . headers ,
41
+ body : chunks . join ( '' )
42
+ } )
43
+ } )
44
+ } )
45
+ clientReq . on ( 'error' , reject )
46
+ clientReq . end ( )
47
+ } )
48
+ }
49
+
30
50
test ( 'express res/req serialization' , t => {
31
51
const app = express ( )
32
52
let server
@@ -78,26 +98,74 @@ test('express res/req serialization', t => {
78
98
t . equal ( typeof ( rec . client . port ) , 'number' )
79
99
} )
80
100
81
- app . listen ( 0 , '127.0.0.1' , function ( ) {
82
- server = this
83
- const req = http . get (
84
- `http://127.0.0.1:${ server . address ( ) . port } /apath?aquery#ahash` ,
85
- {
86
- headers : {
87
- 'user-agent' : 'cool-agent' ,
88
- connection : 'close' ,
89
- 'x-request-id' : 'arequestid'
101
+ const aRouter = express . Router ( )
102
+ aRouter . get ( '/asubpath' , ( req , res ) => {
103
+ res . end ( 'hi' )
104
+
105
+ const rec = { }
106
+ let rv = formatHttpRequest ( rec , req )
107
+ t . ok ( rv , 'formatHttpRequest processed req' )
108
+ rv = formatHttpResponse ( rec , res )
109
+ t . ok ( rv , 'formatHttpResponse processed res' )
110
+
111
+ const port = server . address ( ) . port
112
+ t . same ( rec , {
113
+ http : {
114
+ version : '1.1' ,
115
+ request : {
116
+ method : 'GET' ,
117
+ headers : {
118
+ host : `127.0.0.1:${ port } ` ,
119
+ connection : 'close'
120
+ }
121
+ } ,
122
+ response : {
123
+ status_code : 200 ,
124
+ headers : {
125
+ 'x-powered-by' : 'Express'
126
+ }
90
127
}
91
128
} ,
92
- function ( res ) {
93
- res . on ( 'data' , function ( ) { } )
94
- res . on ( 'end' , function ( ) {
95
- server . close ( function ( ) {
96
- t . end ( )
97
- } )
98
- } )
129
+ url : {
130
+ full : `http://127.0.0.1:${ port } /arouter/asubpath` ,
131
+ path : '/arouter/asubpath' ,
132
+ domain : '127.0.0.1'
133
+ } ,
134
+ client : {
135
+ address : '127.0.0.1' ,
136
+ ip : '127.0.0.1' ,
137
+ port : req . socket . remotePort
99
138
}
100
- )
101
- req . on ( 'error' , t . error )
139
+ } )
140
+ } )
141
+ app . use ( '/arouter' , aRouter )
142
+
143
+ app . listen ( 0 , '127.0.0.1' , async function ( ) {
144
+ server = this
145
+
146
+ await Promise . all ( [
147
+ makeARequest (
148
+ `http://127.0.0.1:${ server . address ( ) . port } /apath?aquery#ahash` ,
149
+ {
150
+ headers : {
151
+ 'user-agent' : 'cool-agent' ,
152
+ connection : 'close' ,
153
+ 'x-request-id' : 'arequestid'
154
+ }
155
+ }
156
+ ) ,
157
+ makeARequest (
158
+ `http://127.0.0.1:${ server . address ( ) . port } /arouter/asubpath` ,
159
+ {
160
+ headers : {
161
+ connection : 'close'
162
+ }
163
+ }
164
+ )
165
+ ] )
166
+
167
+ server . close ( ( ) => {
168
+ t . end ( )
169
+ } )
102
170
} )
103
171
} )
0 commit comments