11'use strict' ;
22
3- const common = require ( '../common' ) ;
4- const http = require ( 'http' ) ;
5- const assert = require ( 'assert' ) ;
6- const fs = require ( 'fs' ) ;
3+ const { mustCall } = require ( '../common' ) ;
74
8- let resEnd = null ;
5+ const fs = require ( 'fs' ) ;
6+ const http = require ( 'http' ) ;
7+ const { strictEqual } = require ( 'assert' ) ;
98
10- const server = http . createServer ( common . mustCall ( function ( req , res ) {
9+ const server = http . createServer ( mustCall ( function ( req , res ) {
10+ strictEqual ( req . socket . listenerCount ( 'data' ) , 1 ) ;
11+ req . socket . once ( 'data' , mustCall ( function ( ) {
12+ // Ensure that a chunk of data is received before calling `res.end()`.
13+ res . end ( 'hello world' ) ;
14+ } ) ) ;
1115 // This checks if the request gets dumped
1216 // resume will be triggered by res.end().
13- req . on ( 'resume' , common . mustCall ( function ( ) {
17+ req . on ( 'resume' , mustCall ( function ( ) {
1418 // There is no 'data' event handler anymore
1519 // it gets automatically removed when dumping the request.
16- assert . strictEqual ( req . listenerCount ( 'data' ) , 0 ) ;
17-
18- req . on ( 'data' , common . mustCallAtLeast ( function ( d ) {
19- // Leaving the console.log explicitly, so that
20- // we can know how many chunks we have received.
21- console . log ( 'data' , d ) ;
22- } , 1 ) ) ;
20+ strictEqual ( req . listenerCount ( 'data' ) , 0 ) ;
21+ req . on ( 'data' , mustCall ( ) ) ;
2322 } ) ) ;
2423
2524 // We explicitly pause the stream
@@ -30,15 +29,9 @@ const server = http.createServer(common.mustCall(function(req, res) {
3029
3130 // Start sending the response.
3231 res . flushHeaders ( ) ;
33-
34- resEnd = function ( ) {
35- setImmediate ( function ( ) {
36- res . end ( 'hello world' ) ;
37- } ) ;
38- } ;
3932} ) ) ;
4033
41- server . listen ( 0 , common . mustCall ( function ( ) {
34+ server . listen ( 0 , mustCall ( function ( ) {
4235 const req = http . request ( {
4336 method : 'POST' ,
4437 port : server . address ( ) . port
@@ -48,13 +41,10 @@ server.listen(0, common.mustCall(function() {
4841 // for the body.
4942 req . flushHeaders ( ) ;
5043
51- req . on ( 'response' , common . mustCall ( function ( res ) {
44+ req . on ( 'response' , mustCall ( function ( res ) {
5245 // Pipe the body as soon as we get the headers of the
5346 // response back.
54- const readFileStream = fs . createReadStream ( __filename ) ;
55- readFileStream . on ( 'end' , resEnd ) ;
56-
57- readFileStream . pipe ( req ) ;
47+ fs . createReadStream ( __filename ) . pipe ( req ) ;
5848
5949 res . resume ( ) ;
6050
0 commit comments