@@ -14,20 +14,40 @@ const isError = (error) => error instanceof Error;
1414
1515dc . subscribe ( 'http.client.request.start' , common . mustCall ( ( { request } ) => {
1616 assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
17+ } , 4 ) ) ;
18+
19+ dc . subscribe ( 'http.client.request.bodyChunkSent' , common . mustCall ( ( { request, chunk, encoding } ) => {
20+ assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
21+ assert . ok ( typeof chunk === 'string' || chunk instanceof Uint8Array ) ;
22+ assert . strictEqual ( typeof encoding === 'string' || encoding == null , true ) ;
23+ } , 3 ) ) ;
24+
25+ dc . subscribe ( 'http.client.request.bodySent' , common . mustCall ( ( { request } ) => {
26+ assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
1727} , 2 ) ) ;
1828
1929dc . subscribe ( 'http.client.request.error' , common . mustCall ( ( { request, error } ) => {
2030 assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
2131 assert . strictEqual ( isError ( error ) , true ) ;
2232} ) ) ;
2333
34+ dc . subscribe ( 'http.client.response.bodyChunkReceived' , common . mustCall ( ( {
35+ request,
36+ response,
37+ chunk,
38+ } ) => {
39+ assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
40+ assert . strictEqual ( isIncomingMessage ( response ) , true ) ;
41+ assert . ok ( chunk instanceof Uint8Array ) ;
42+ } , 3 ) ) ;
43+
2444dc . subscribe ( 'http.client.response.finish' , common . mustCall ( ( {
2545 request,
2646 response
2747} ) => {
2848 assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
2949 assert . strictEqual ( isIncomingMessage ( response ) , true ) ;
30- } ) ) ;
50+ } , 3 ) ) ;
3151
3252dc . subscribe ( 'http.server.request.start' , common . mustCall ( ( {
3353 request,
@@ -39,7 +59,7 @@ dc.subscribe('http.server.request.start', common.mustCall(({
3959 assert . strictEqual ( isOutgoingMessage ( response ) , true ) ;
4060 assert . strictEqual ( isNetSocket ( socket ) , true ) ;
4161 assert . strictEqual ( isHTTPServer ( server ) , true ) ;
42- } ) ) ;
62+ } , 3 ) ) ;
4363
4464dc . subscribe ( 'http.server.response.finish' , common . mustCall ( ( {
4565 request,
@@ -51,24 +71,37 @@ dc.subscribe('http.server.response.finish', common.mustCall(({
5171 assert . strictEqual ( isOutgoingMessage ( response ) , true ) ;
5272 assert . strictEqual ( isNetSocket ( socket ) , true ) ;
5373 assert . strictEqual ( isHTTPServer ( server ) , true ) ;
54- } ) ) ;
74+ } , 3 ) ) ;
5575
5676dc . subscribe ( 'http.server.response.created' , common . mustCall ( ( {
5777 request,
5878 response,
5979} ) => {
6080 assert . strictEqual ( isIncomingMessage ( request ) , true ) ;
6181 assert . strictEqual ( isOutgoingMessage ( response ) , true ) ;
62- } ) ) ;
82+ } , 3 ) ) ;
6383
6484dc . subscribe ( 'http.client.request.created' , common . mustCall ( ( { request } ) => {
6585 assert . strictEqual ( isOutgoingMessage ( request ) , true ) ;
6686 assert . strictEqual ( isHTTPServer ( server ) , true ) ;
67- } , 2 ) ) ;
87+ } , 4 ) ) ;
6888
6989const server = http . createServer ( common . mustCall ( ( req , res ) => {
70- res . end ( 'done' ) ;
71- } ) ) ;
90+ const chunks = [ ] ;
91+ req . on ( 'data' , ( chunk ) => chunks . push ( chunk ) ) ;
92+ req . on ( 'end' , common . mustCall ( ( ) => {
93+ if ( req . method === 'POST' && req . url === '/string-body' ) {
94+ assert . strictEqual ( Buffer . concat ( chunks ) . toString ( ) , 'foobar' ) ;
95+ } else if ( req . method === 'POST' && req . url === '/binary-body' ) {
96+ assert . deepStrictEqual ( Buffer . concat ( chunks ) , Buffer . from ( [ 0 , 1 , 2 , 3 ] ) ) ;
97+ } else {
98+ assert . strictEqual ( req . method , 'GET' ) ;
99+ assert . strictEqual ( req . url , '/' ) ;
100+ assert . strictEqual ( Buffer . concat ( chunks ) . byteLength , 0 ) ;
101+ }
102+ res . end ( 'done' ) ;
103+ } ) ) ;
104+ } , 3 ) ) ;
72105
73106server . listen ( async ( ) => {
74107 const { port } = server . address ( ) ;
@@ -78,10 +111,33 @@ server.listen(async () => {
78111 await new Promise ( ( resolve ) => {
79112 invalidRequest . on ( 'error' , resolve ) ;
80113 } ) ;
81- http . get ( `http://localhost:${ port } ` , ( res ) => {
82- res . resume ( ) ;
83- res . on ( 'end' , ( ) => {
84- server . close ( ) ;
114+ await new Promise ( ( resolve , reject ) => {
115+ http . get ( `http://localhost:${ port } ` , ( res ) => {
116+ res . setEncoding ( 'utf8' ) ;
117+ res . resume ( ) ;
118+ res . on ( 'end' , resolve ) ;
119+ } ) . on ( 'error' , reject ) ;
120+ } ) ;
121+ await new Promise ( ( resolve , reject ) => {
122+ const req = http . request ( `http://localhost:${ port } /string-body` , {
123+ method : 'POST' ,
124+ } , ( res ) => {
125+ res . resume ( ) ;
126+ res . on ( 'end' , resolve ) ;
127+ } ) ;
128+ req . on ( 'error' , reject ) ;
129+ req . write ( 'foo' ) ;
130+ req . end ( 'bar' ) ;
131+ } ) ;
132+ await new Promise ( ( resolve , reject ) => {
133+ const req = http . request ( `http://localhost:${ port } /binary-body` , {
134+ method : 'POST' ,
135+ } , ( res ) => {
136+ res . resume ( ) ;
137+ res . on ( 'end' , resolve ) ;
85138 } ) ;
139+ req . on ( 'error' , reject ) ;
140+ req . end ( Buffer . from ( [ 0 , 1 , 2 , 3 ] ) ) ;
86141 } ) ;
142+ server . close ( ) ;
87143} ) ;
0 commit comments