@@ -24,6 +24,9 @@ const common = require('../common');
24
24
if ( ! common . hasCrypto )
25
25
common . skip ( 'missing crypto' ) ;
26
26
27
+ // This test ensures that the data received through tls over http tunnel
28
+ // is same as what is sent.
29
+
27
30
const assert = require ( 'assert' ) ;
28
31
const https = require ( 'https' ) ;
29
32
const net = require ( 'net' ) ;
@@ -37,21 +40,21 @@ const cert = fixtures.readKey('agent1-cert.pem');
37
40
38
41
const options = { key, cert } ;
39
42
40
- const server = https . createServer ( options , function ( req , res ) {
43
+ const server = https . createServer ( options , common . mustCall ( ( req , res ) => {
41
44
console . log ( 'SERVER: got request' ) ;
42
45
res . writeHead ( 200 , {
43
46
'content-type' : 'text/plain'
44
47
} ) ;
45
48
console . log ( 'SERVER: sending response' ) ;
46
49
res . end ( 'hello world\n' ) ;
47
- } ) ;
50
+ } ) ) ;
48
51
49
- const proxy = net . createServer ( function ( clientSocket ) {
52
+ const proxy = net . createServer ( ( clientSocket ) => {
50
53
console . log ( 'PROXY: got a client connection' ) ;
51
54
52
55
let serverSocket = null ;
53
56
54
- clientSocket . on ( 'data' , function ( chunk ) {
57
+ clientSocket . on ( 'data' , ( chunk ) => {
55
58
if ( ! serverSocket ) {
56
59
// Verify the CONNECT request
57
60
assert . strictEqual ( `CONNECT localhost:${ server . address ( ) . port } ` +
@@ -65,39 +68,39 @@ const proxy = net.createServer(function(clientSocket) {
65
68
console . log ( 'PROXY: creating a tunnel' ) ;
66
69
67
70
// create the tunnel
68
- serverSocket = net . connect ( server . address ( ) . port , function ( ) {
71
+ serverSocket = net . connect ( server . address ( ) . port , common . mustCall ( ( ) => {
69
72
console . log ( 'PROXY: replying to client CONNECT request' ) ;
70
73
71
74
// Send the response
72
75
clientSocket . write ( 'HTTP/1.1 200 OK\r\nProxy-Connections: keep' +
73
- '-alive\r\nConnections: keep-alive\r\nVia: ' +
74
- `localhost:${ proxy . address ( ) . port } \r\n\r\n` ) ;
75
- } ) ;
76
+ '-alive\r\nConnections: keep-alive\r\nVia: ' +
77
+ `localhost:${ proxy . address ( ) . port } \r\n\r\n` ) ;
78
+ } ) ) ;
76
79
77
- serverSocket . on ( 'data' , function ( chunk ) {
80
+ serverSocket . on ( 'data' , ( chunk ) => {
78
81
clientSocket . write ( chunk ) ;
79
82
} ) ;
80
83
81
- serverSocket . on ( 'end' , function ( ) {
84
+ serverSocket . on ( 'end' , common . mustCall ( ( ) => {
82
85
clientSocket . destroy ( ) ;
83
- } ) ;
86
+ } ) ) ;
84
87
} else {
85
88
serverSocket . write ( chunk ) ;
86
89
}
87
90
} ) ;
88
91
89
- clientSocket . on ( 'end' , function ( ) {
92
+ clientSocket . on ( 'end' , ( ) => {
90
93
serverSocket . destroy ( ) ;
91
94
} ) ;
92
95
} ) ;
93
96
94
97
server . listen ( 0 ) ;
95
98
96
- proxy . listen ( 0 , function ( ) {
99
+ proxy . listen ( 0 , common . mustCall ( ( ) => {
97
100
console . log ( 'CLIENT: Making CONNECT request' ) ;
98
101
99
102
const req = http . request ( {
100
- port : this . address ( ) . port ,
103
+ port : proxy . address ( ) . port ,
101
104
method : 'CONNECT' ,
102
105
path : `localhost:${ server . address ( ) . port } ` ,
103
106
headers : {
@@ -117,7 +120,7 @@ proxy.listen(0, function() {
117
120
118
121
function onUpgrade ( res , socket , head ) {
119
122
// Hacky.
120
- process . nextTick ( function ( ) {
123
+ process . nextTick ( ( ) => {
121
124
onConnect ( res , socket , head ) ;
122
125
} ) ;
123
126
}
@@ -145,29 +148,29 @@ proxy.listen(0, function() {
145
148
socket : socket , // reuse the socket
146
149
agent : false ,
147
150
rejectUnauthorized : false
148
- } , function ( res ) {
151
+ } , ( res ) => {
149
152
assert . strictEqual ( 200 , res . statusCode ) ;
150
153
151
- res . on ( 'data' , function ( chunk ) {
154
+ res . on ( 'data' , common . mustCall ( ( chunk ) => {
152
155
assert . strictEqual ( 'hello world\n' , chunk . toString ( ) ) ;
153
156
console . log ( 'CLIENT: got HTTPS response' ) ;
154
157
gotRequest = true ;
155
- } ) ;
158
+ } ) ) ;
156
159
157
- res . on ( 'end' , function ( ) {
160
+ res . on ( 'end' , common . mustCall ( ( ) => {
158
161
proxy . close ( ) ;
159
162
server . close ( ) ;
160
- } ) ;
161
- } ) . on ( 'error' , function ( er ) {
163
+ } ) ) ;
164
+ } ) . on ( 'error' , ( er ) => {
162
165
// We're ok with getting ECONNRESET in this test, but it's
163
166
// timing-dependent, and thus unreliable. Any other errors
164
167
// are just failures, though.
165
168
if ( er . code !== 'ECONNRESET' )
166
169
throw er ;
167
170
} ) . end ( ) ;
168
171
}
169
- } ) ;
172
+ } ) ) ;
170
173
171
- process . on ( 'exit' , function ( ) {
174
+ process . on ( 'exit' , ( ) => {
172
175
assert . ok ( gotRequest ) ;
173
176
} ) ;
0 commit comments