@@ -4,6 +4,7 @@ const net = require('net');
4
4
const http = require ( 'http' ) ;
5
5
const assert = require ( 'assert' ) ;
6
6
const common = require ( '../common' ) ;
7
+ const { mustCall } = require ( '../common' ) ;
7
8
8
9
const bodySent = 'This is my request' ;
9
10
@@ -50,65 +51,63 @@ const server = http.createServer(function (req, res) {
50
51
} )
51
52
} )
52
53
54
+ function initialRequests ( socket , numberOfRequests , cb ) {
55
+ let buffer = '' ;
56
+
57
+ writeRequest ( socket )
58
+
59
+ socket . on ( 'data' , ( data ) => {
60
+ buffer += data ;
61
+
62
+ if ( buffer . endsWith ( '\r\n\r\n' ) ) {
63
+ if ( -- numberOfRequests === 0 ) {
64
+ socket . removeAllListeners ( 'data' ) ;
65
+ cb ( ) ;
66
+ } else {
67
+ const [ headers , body ] = buffer . trim ( ) . split ( '\r\n\r\n' ) ;
68
+ assertResponse ( headers , body )
69
+ buffer = '' ;
70
+ writeRequest ( socket , true )
71
+ }
72
+ }
73
+ } ) ;
74
+ }
75
+
76
+
53
77
server . maxRequestsPerSocket = 3 ;
54
78
server . listen ( 0 , common . mustCall ( ( res ) => {
55
- const socket = net . createConnection (
56
- { port : server . address ( ) . port } ,
57
- common . mustCall ( ( ) => {
58
- writeRequest ( socket )
59
- writeRequest ( socket )
60
-
61
- const anotherSocket = net . createConnection (
62
- { port : server . address ( ) . port } ,
63
- common . mustCall ( ( ) => {
64
- writeRequest ( anotherSocket )
65
-
66
- let anotherBuffer = ''
67
- let lastWritten = false ;
68
- anotherSocket . setEncoding ( 'utf8' ) ;
69
- anotherSocket . on ( 'data' , ( data ) => {
70
- anotherBuffer += data ;
71
-
72
- if ( anotherBuffer . endsWith ( '\r\n\r\n' ) ) {
73
- if ( lastWritten ) {
74
- anotherSocket . end ( )
75
- } else {
76
- writeRequest ( anotherSocket ) ;
77
- lastWritten = true ;
78
- }
79
- }
80
- } ) ;
81
-
82
- anotherSocket . on ( 'end' , common . mustCall ( ( ) => {
83
- const anoterResponses = anotherBuffer . trim ( ) . split ( '\r\n\r\n' ) ;
84
-
85
- assertResponse ( anoterResponses [ 0 ] , anoterResponses [ 1 ] , false )
86
- assertResponse ( anoterResponses [ 2 ] , anoterResponses [ 3 ] , false )
87
-
88
- // Add two additional requests to two previous on the first socket
89
- writeRequest ( socket , true )
90
- writeRequest ( socket , true )
91
-
92
- let buffer = '' ;
93
- socket . setEncoding ( 'utf8' ) ;
94
- socket . on ( 'data' , ( data ) => {
95
- buffer += data ;
96
- } ) ;
97
-
98
- socket . on ( 'end' , common . mustCall ( ( ) => {
99
- const responses = buffer . trim ( ) . split ( '\r\n\r\n' ) ;
100
- // We sent more requests than allowed per socket,
101
- // but we get only the allowed number of responses & headers
102
- assert ( responses . length === server . maxRequestsPerSocket * 2 ) ;
103
-
104
- assertResponse ( responses [ 0 ] , responses [ 1 ] , false )
105
- assertResponse ( responses [ 2 ] , responses [ 3 ] , false )
106
- assertResponse ( responses [ 4 ] , responses [ 5 ] , true )
107
-
108
- server . close ( ) ;
109
- } ) ) ;
110
- } ) ) ;
111
- } ) ) ;
112
- } )
113
- ) ;
79
+ const socket = new net . Socket ( ) ;
80
+ const anotherSocket = new net . Socket ( ) ;
81
+
82
+ socket . on ( 'end' , mustCall ( ( ) => {
83
+ server . close ( ) ;
84
+ } ) ) ;
85
+
86
+ socket . on ( 'ready' , common . mustCall ( ( ) => {
87
+ // Do two of 3 requests and ensure they still alive
88
+ initialRequests ( socket , 2 , common . mustCall ( ( ) => {
89
+ anotherSocket . connect ( { port : server . address ( ) . port } ) ;
90
+ } ) )
91
+ } ) ) ;
92
+
93
+ anotherSocket . on ( 'ready' , common . mustCall ( ( ) => {
94
+ // Do another 2 requests with another socket, enusre that this will not affect the first socket
95
+ initialRequests ( anotherSocket , 2 , common . mustCall ( ( ) => {
96
+ let buffer = '' ;
97
+
98
+ // Send the rest of the calls to the first socket and see connection is closed
99
+ socket . on ( 'data' , common . mustCall ( ( data ) => {
100
+ buffer += data ;
101
+
102
+ if ( buffer . endsWith ( '\r\n\r\n' ) ) {
103
+ const [ headers , body ] = buffer . trim ( ) . split ( '\r\n\r\n' ) ;
104
+ assertResponse ( headers , body , true ) ;
105
+ }
106
+ } ) ) ;
107
+
108
+ writeRequest ( socket , true ) ;
109
+ } ) ) ;
110
+ } ) ) ;
111
+
112
+ socket . connect ( { port : server . address ( ) . port } ) ;
114
113
} ) ) ;
0 commit comments