forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor test-http-after-connect.js
- Replace assert.equal() to assert.strictEqual() - Replace var with const where applicable - Removed firstBodyChunk which is never used - Remove the process.on('exit', ...) and replace its functionality by - Using common.mustCall() where applicable PR-URL: nodejs#10229 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Italo A. Casas <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
- Loading branch information
1 parent
e6bff1e
commit db5d910
Showing
1 changed file
with
13 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,62 @@ | ||
'use strict'; | ||
require('../common'); | ||
var assert = require('assert'); | ||
var http = require('http'); | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
var serverConnected = false; | ||
var serverRequests = 0; | ||
var clientResponses = 0; | ||
|
||
var server = http.createServer(function(req, res) { | ||
const server = http.createServer(common.mustCall(function(req, res) { | ||
console.error('Server got GET request'); | ||
req.resume(); | ||
++serverRequests; | ||
res.writeHead(200); | ||
res.write(''); | ||
setTimeout(function() { | ||
res.end(req.url); | ||
}, 50); | ||
}); | ||
server.on('connect', function(req, socket, firstBodyChunk) { | ||
}, 2)); | ||
server.on('connect', common.mustCall(function(req, socket) { | ||
console.error('Server got CONNECT request'); | ||
serverConnected = true; | ||
socket.write('HTTP/1.1 200 Connection established\r\n\r\n'); | ||
socket.resume(); | ||
socket.on('end', function() { | ||
socket.end(); | ||
}); | ||
}); | ||
})); | ||
server.listen(0, function() { | ||
var req = http.request({ | ||
const req = http.request({ | ||
port: this.address().port, | ||
method: 'CONNECT', | ||
path: 'google.com:80' | ||
}); | ||
req.on('connect', function(res, socket, firstBodyChunk) { | ||
req.on('connect', common.mustCall(function(res, socket) { | ||
console.error('Client got CONNECT response'); | ||
socket.end(); | ||
socket.on('end', function() { | ||
doRequest(0); | ||
doRequest(1); | ||
}); | ||
socket.resume(); | ||
}); | ||
})); | ||
req.end(); | ||
}); | ||
|
||
function doRequest(i) { | ||
http.get({ | ||
port: server.address().port, | ||
path: '/request' + i | ||
}, function(res) { | ||
}, common.mustCall(function(res) { | ||
console.error('Client got GET response'); | ||
var data = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk) { | ||
data += chunk; | ||
}); | ||
res.on('end', function() { | ||
assert.equal(data, '/request' + i); | ||
assert.strictEqual(data, '/request' + i); | ||
++clientResponses; | ||
if (clientResponses === 2) { | ||
server.close(); | ||
} | ||
}); | ||
}); | ||
})); | ||
} | ||
|
||
process.on('exit', function() { | ||
assert(serverConnected); | ||
assert.equal(serverRequests, 2); | ||
assert.equal(clientResponses, 2); | ||
}); |