diff options
author | Adrian Estrada <edsadr@gmail.com> | 2016-12-26 09:43:06 -0500 |
---|---|---|
committer | Luigi Pinca <luigipinca@gmail.com> | 2016-12-30 12:56:02 +0100 |
commit | 72a8488e8e0108e92422443364752b788d58ea56 (patch) | |
tree | 13de774da73c85c42d144ac1937c5248c0334e64 /test/parallel/test-http-connect.js | |
parent | ca82f72b31dda754693d4e008ed7bafda7dcea31 (diff) | |
download | android-node-v8-72a8488e8e0108e92422443364752b788d58ea56.tar.gz android-node-v8-72a8488e8e0108e92422443364752b788d58ea56.tar.bz2 android-node-v8-72a8488e8e0108e92422443364752b788d58ea56.zip |
test: refactor the code in test-http-connect
* use common.mustCall to control the functions execution automatically
* use let and const instead of var
* use assert.strictEqual instead of assert.equal
* use arrow functions
* remove console.error and unnecessary variables
PR-URL: https://github.com/nodejs/node/pull/10397
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'test/parallel/test-http-connect.js')
-rw-r--r-- | test/parallel/test-http-connect.js | 78 |
1 files changed, 33 insertions, 45 deletions
diff --git a/test/parallel/test-http-connect.js b/test/parallel/test-http-connect.js index 9da199b8ee..fa2d8aaa4f 100644 --- a/test/parallel/test-http-connect.js +++ b/test/parallel/test-http-connect.js @@ -1,85 +1,73 @@ 'use strict'; const common = require('../common'); -var assert = require('assert'); -var http = require('http'); +const assert = require('assert'); +const http = require('http'); -var serverGotConnect = false; -var clientGotConnect = false; +const server = http.createServer(common.fail); -var server = http.createServer(common.fail); -server.on('connect', function(req, socket, firstBodyChunk) { - assert.equal(req.method, 'CONNECT'); - assert.equal(req.url, 'google.com:443'); - console.error('Server got CONNECT request'); - serverGotConnect = true; +server.on('connect', common.mustCall((req, socket, firstBodyChunk) => { + assert.strictEqual(req.method, 'CONNECT'); + assert.strictEqual(req.url, 'google.com:443'); socket.write('HTTP/1.1 200 Connection established\r\n\r\n'); - var data = firstBodyChunk.toString(); - socket.on('data', function(buf) { + let data = firstBodyChunk.toString(); + socket.on('data', (buf) => { data += buf.toString(); }); - socket.on('end', function() { + + socket.on('end', common.mustCall(() => { socket.end(data); - }); -}); -server.listen(0, function() { - var req = http.request({ + })); +})); + +server.listen(0, common.mustCall(function() { + const req = http.request({ port: this.address().port, method: 'CONNECT', path: 'google.com:443' }, common.fail); - var clientRequestClosed = false; - req.on('close', function() { - clientRequestClosed = true; - }); - - req.on('connect', function(res, socket, firstBodyChunk) { - console.error('Client got CONNECT request'); - clientGotConnect = true; + req.on('close', common.mustCall(() => {})); + req.on('connect', common.mustCall((res, socket, firstBodyChunk) => { // Make sure this request got removed from the pool. - var name = 'localhost:' + server.address().port; + const name = 'localhost:' + server.address().port; assert(!http.globalAgent.sockets.hasOwnProperty(name)); assert(!http.globalAgent.requests.hasOwnProperty(name)); // Make sure this socket has detached. assert(!socket.ondata); assert(!socket.onend); - assert.equal(socket.listeners('connect').length, 0); - assert.equal(socket.listeners('data').length, 0); + assert.strictEqual(socket.listeners('connect').length, 0); + assert.strictEqual(socket.listeners('data').length, 0); // the stream.Duplex onend listener // allow 0 here, so that i can run the same test on streams1 impl assert(socket.listeners('end').length <= 1); - assert.equal(socket.listeners('free').length, 0); - assert.equal(socket.listeners('close').length, 0); - assert.equal(socket.listeners('error').length, 0); - assert.equal(socket.listeners('agentRemove').length, 0); + assert.strictEqual(socket.listeners('free').length, 0); + assert.strictEqual(socket.listeners('close').length, 0); + assert.strictEqual(socket.listeners('error').length, 0); + assert.strictEqual(socket.listeners('agentRemove').length, 0); - var data = firstBodyChunk.toString(); - socket.on('data', function(buf) { + let data = firstBodyChunk.toString(); + socket.on('data', (buf) => { data += buf.toString(); }); - socket.on('end', function() { - assert.equal(data, 'HeadBody'); - assert(clientRequestClosed); + + socket.on('end', common.mustCall(() => { + assert.strictEqual(data, 'HeadBody'); server.close(); - }); + })); + socket.write('Body'); socket.end(); - }); + })); // It is legal for the client to send some data intended for the server // before the "200 Connection established" (or any other success or // error code) is received. req.write('Head'); req.end(); -}); - -process.on('exit', function() { - assert.ok(serverGotConnect); - assert.ok(clientGotConnect); -}); +})); |