mirror of https://github.com/lukechilds/node.git
Browse Source
Many of the tests use variables to track when callback functions are invoked or events are emitted. These variables are then asserted on process exit. This commit replaces this pattern in straightforward cases with common.mustCall(). This makes the tests easier to reason about, leads to a net reduction in lines of code, and uncovered a few bugs in tests. This commit also replaces some callbacks that should never be called with common.fail(). PR-URL: https://github.com/nodejs/node/pull/7753 Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Minwoo Jung <jmwsoft@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>v7.x
213 changed files with 1101 additions and 2882 deletions
@ -1,17 +1,10 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../../common'); |
const common = require('../../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var binding = require('./build/Release/binding'); |
var binding = require('./build/Release/binding'); |
||||
var called = false; |
|
||||
|
|
||||
process.on('exit', function() { |
binding(5, common.mustCall(function(err, val) { |
||||
assert(called); |
|
||||
}); |
|
||||
|
|
||||
binding(5, function(err, val) { |
|
||||
assert.equal(null, err); |
assert.equal(null, err); |
||||
assert.equal(10, val); |
assert.equal(10, val); |
||||
process.nextTick(function() { |
process.nextTick(common.mustCall(function() {})); |
||||
called = true; |
})); |
||||
}); |
|
||||
}); |
|
||||
|
@ -1,25 +1,14 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
|
||||
var net = require('net'); |
var net = require('net'); |
||||
|
|
||||
var client, killed = false, ended = false; |
var client; |
||||
var TIMEOUT = 10 * 1000; |
var TIMEOUT = 10 * 1000; |
||||
|
|
||||
client = net.createConnection(53, '8.8.8.8', function() { |
client = net.createConnection(53, '8.8.8.8', function() { |
||||
client.unref(); |
client.unref(); |
||||
}); |
}); |
||||
|
|
||||
client.on('close', function() { |
client.on('close', common.fail); |
||||
ended = true; |
|
||||
}); |
|
||||
|
|
||||
setTimeout(function() { |
|
||||
killed = true; |
|
||||
client.end(); |
|
||||
}, TIMEOUT).unref(); |
|
||||
|
|
||||
process.on('exit', function() { |
setTimeout(common.fail, TIMEOUT).unref(); |
||||
assert.strictEqual(killed, false, 'A client should have connected'); |
|
||||
assert.strictEqual(ended, false, 'A client should stay connected'); |
|
||||
}); |
|
||||
|
@ -1,33 +1,22 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var exec = require('child_process').exec; |
var exec = require('child_process').exec; |
||||
var os = require('os'); |
var os = require('os'); |
||||
|
|
||||
var success_count = 0; |
|
||||
|
|
||||
var str = 'hello'; |
var str = 'hello'; |
||||
|
|
||||
// default encoding
|
// default encoding
|
||||
exec('echo ' + str, function(err, stdout, stderr) { |
exec('echo ' + str, common.mustCall(function(err, stdout, stderr) { |
||||
assert.ok('string', typeof stdout, 'Expected stdout to be a string'); |
assert.ok('string', typeof stdout, 'Expected stdout to be a string'); |
||||
assert.ok('string', typeof stderr, 'Expected stderr to be a string'); |
assert.ok('string', typeof stderr, 'Expected stderr to be a string'); |
||||
assert.equal(str + os.EOL, stdout); |
assert.equal(str + os.EOL, stdout); |
||||
|
})); |
||||
success_count++; |
|
||||
}); |
|
||||
|
|
||||
// no encoding (Buffers expected)
|
// no encoding (Buffers expected)
|
||||
exec('echo ' + str, { |
exec('echo ' + str, { |
||||
encoding: null |
encoding: null |
||||
}, function(err, stdout, stderr) { |
}, common.mustCall(function(err, stdout, stderr) { |
||||
assert.ok(stdout instanceof Buffer, 'Expected stdout to be a Buffer'); |
assert.ok(stdout instanceof Buffer, 'Expected stdout to be a Buffer'); |
||||
assert.ok(stderr instanceof Buffer, 'Expected stderr to be a Buffer'); |
assert.ok(stderr instanceof Buffer, 'Expected stderr to be a Buffer'); |
||||
assert.equal(str + os.EOL, stdout.toString()); |
assert.equal(str + os.EOL, stdout.toString()); |
||||
|
})); |
||||
success_count++; |
|
||||
}); |
|
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal(2, success_count); |
|
||||
}); |
|
||||
|
@ -1,20 +1,14 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var ch = require('child_process'); |
var ch = require('child_process'); |
||||
|
|
||||
var SIZE = 100000; |
var SIZE = 100000; |
||||
var childGone = false; |
|
||||
|
|
||||
var cp = ch.spawn('python', ['-c', 'print ' + SIZE + ' * "C"'], { |
var cp = ch.spawn('python', ['-c', 'print ' + SIZE + ' * "C"'], { |
||||
stdio: 'inherit' |
stdio: 'inherit' |
||||
}); |
}); |
||||
|
|
||||
cp.on('exit', function(code) { |
cp.on('exit', common.mustCall(function(code) { |
||||
childGone = true; |
|
||||
assert.equal(0, code); |
assert.equal(0, code); |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.ok(childGone); |
|
||||
}); |
|
||||
|
@ -1,39 +1,26 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var cluster = require('cluster'); |
var cluster = require('cluster'); |
||||
|
|
||||
assert(cluster.isMaster); |
assert(cluster.isMaster); |
||||
|
|
||||
var assertsRun = 0; |
|
||||
|
|
||||
function emitAndCatch(next) { |
function emitAndCatch(next) { |
||||
cluster.once('setup', function(settings) { |
cluster.once('setup', common.mustCall(function(settings) { |
||||
assert.strictEqual(settings.exec, 'new-exec'); |
assert.strictEqual(settings.exec, 'new-exec'); |
||||
console.log('ok "setup" emitted with options set'); |
|
||||
assertsRun += 1; |
|
||||
setImmediate(next); |
setImmediate(next); |
||||
}); |
})); |
||||
cluster.setupMaster({ exec: 'new-exec' }); |
cluster.setupMaster({ exec: 'new-exec' }); |
||||
} |
} |
||||
|
|
||||
function emitAndCatch2(next) { |
function emitAndCatch2(next) { |
||||
cluster.once('setup', function(settings) { |
cluster.once('setup', common.mustCall(function(settings) { |
||||
assert('exec' in settings); |
assert('exec' in settings); |
||||
console.log('ok "setup" emitted without options set'); |
|
||||
assertsRun += 1; |
|
||||
setImmediate(next); |
setImmediate(next); |
||||
}); |
})); |
||||
cluster.setupMaster(); |
cluster.setupMaster(); |
||||
} |
} |
||||
|
|
||||
process.on('exit', function() { |
emitAndCatch(common.mustCall(function() { |
||||
assert.strictEqual(assertsRun, 2); |
emitAndCatch2(common.mustCall(function() {})); |
||||
console.log('ok correct number of assertions'); |
})); |
||||
}); |
|
||||
|
|
||||
emitAndCatch(function() { |
|
||||
emitAndCatch2(function() { |
|
||||
console.log('ok emitted and caught'); |
|
||||
}); |
|
||||
}); |
|
||||
|
@ -1,25 +1,17 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var cluster = require('cluster'); |
var cluster = require('cluster'); |
||||
|
|
||||
if (!cluster.isMaster) { |
if (!cluster.isMaster) { |
||||
process.exit(42); |
process.exit(42); |
||||
} else { |
} else { |
||||
var seenExit = 0; |
|
||||
var seenDeath = 0; |
|
||||
var worker = cluster.fork(); |
var worker = cluster.fork(); |
||||
worker.on('exit', function(exitCode, signalCode) { |
worker.on('exit', common.mustCall(function(exitCode, signalCode) { |
||||
assert.equal(exitCode, 42); |
assert.equal(exitCode, 42); |
||||
assert.equal(signalCode, null); |
assert.equal(signalCode, null); |
||||
seenExit++; |
})); |
||||
}); |
cluster.on('exit', common.mustCall(function(worker_) { |
||||
cluster.on('exit', function(worker_) { |
|
||||
assert.equal(worker_, worker); |
assert.equal(worker_, worker); |
||||
seenDeath++; |
})); |
||||
}); |
|
||||
process.on('exit', function() { |
|
||||
assert.equal(seenExit, 1); |
|
||||
assert.equal(seenDeath, 1); |
|
||||
}); |
|
||||
} |
} |
||||
|
@ -1,21 +1,14 @@ |
|||||
'use strict'; |
'use strict'; |
||||
var assert = require('assert'); |
|
||||
var common = require('../common'); |
var common = require('../common'); |
||||
var dgram = require('dgram'); |
var dgram = require('dgram'); |
||||
|
|
||||
var buf = Buffer.alloc(1024, 42); |
var buf = Buffer.alloc(1024, 42); |
||||
|
|
||||
var socket = dgram.createSocket('udp4'); |
var socket = dgram.createSocket('udp4'); |
||||
var closeEvents = 0; |
|
||||
socket.send(buf, 0, buf.length, common.PORT, 'localhost'); |
socket.send(buf, 0, buf.length, common.PORT, 'localhost'); |
||||
|
|
||||
// if close callback is not function, ignore the argument.
|
// if close callback is not function, ignore the argument.
|
||||
socket.close('bad argument'); |
socket.close('bad argument'); |
||||
|
|
||||
socket.on('close', function() { |
socket.on('close', common.mustCall(function() {})); |
||||
++closeEvents; |
|
||||
}); |
|
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal(closeEvents, 1); |
|
||||
}); |
|
||||
|
@ -1,19 +1,9 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
|
||||
|
|
||||
var dgram = require('dgram'); |
var dgram = require('dgram'); |
||||
var closed = false; |
|
||||
|
|
||||
var s = dgram.createSocket('udp4'); |
var s = dgram.createSocket('udp4'); |
||||
s.bind(); |
s.bind(); |
||||
s.unref(); |
s.unref(); |
||||
|
|
||||
setTimeout(function() { |
setTimeout(common.fail, 1000).unref(); |
||||
closed = true; |
|
||||
s.close(); |
|
||||
}, 1000).unref(); |
|
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.strictEqual(closed, false, 'Unrefd socket should not hold loop open'); |
|
||||
}); |
|
||||
|
@ -1,27 +1,13 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var util = require('util'); |
var util = require('util'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var exec = require('child_process').exec; |
var exec = require('child_process').exec; |
||||
|
|
||||
var success_count = 0; |
|
||||
var error_count = 0; |
|
||||
|
|
||||
var cmd = ['"' + process.execPath + '"', '-e', '"console.error(process.argv)"', |
var cmd = ['"' + process.execPath + '"', '-e', '"console.error(process.argv)"', |
||||
'foo', 'bar'].join(' '); |
'foo', 'bar'].join(' '); |
||||
var expected = util.format([process.execPath, 'foo', 'bar']) + '\n'; |
var expected = util.format([process.execPath, 'foo', 'bar']) + '\n'; |
||||
exec(cmd, function(err, stdout, stderr) { |
exec(cmd, common.mustCall(function(err, stdout, stderr) { |
||||
if (err) { |
assert.ifError(err); |
||||
console.log(err.toString()); |
|
||||
++error_count; |
|
||||
return; |
|
||||
} |
|
||||
assert.equal(stderr, expected); |
assert.equal(stderr, expected); |
||||
++success_count; |
})); |
||||
}); |
|
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal(1, success_count); |
|
||||
assert.equal(0, error_count); |
|
||||
}); |
|
||||
|
|
||||
|
@ -1,108 +1,81 @@ |
|||||
/* eslint-disable strict */ |
/* eslint-disable strict */ |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var fs = require('fs'); |
var fs = require('fs'); |
||||
var got_error = false; |
|
||||
var success_count = 0; |
|
||||
|
|
||||
fs.stat('.', function(err, stats) { |
fs.stat('.', common.mustCall(function(err, stats) { |
||||
if (err) { |
assert.ifError(err); |
||||
got_error = true; |
assert.ok(stats.mtime instanceof Date); |
||||
} else { |
|
||||
console.dir(stats); |
|
||||
assert.ok(stats.mtime instanceof Date); |
|
||||
success_count++; |
|
||||
} |
|
||||
assert(this === global); |
assert(this === global); |
||||
}); |
})); |
||||
|
|
||||
fs.stat('.', function(err, stats) { |
fs.stat('.', common.mustCall(function(err, stats) { |
||||
assert.ok(stats.hasOwnProperty('blksize')); |
assert.ok(stats.hasOwnProperty('blksize')); |
||||
assert.ok(stats.hasOwnProperty('blocks')); |
assert.ok(stats.hasOwnProperty('blocks')); |
||||
}); |
})); |
||||
|
|
||||
fs.lstat('.', function(err, stats) { |
fs.lstat('.', common.mustCall(function(err, stats) { |
||||
if (err) { |
assert.ifError(err); |
||||
got_error = true; |
assert.ok(stats.mtime instanceof Date); |
||||
} else { |
|
||||
console.dir(stats); |
|
||||
assert.ok(stats.mtime instanceof Date); |
|
||||
success_count++; |
|
||||
} |
|
||||
assert(this === global); |
assert(this === global); |
||||
}); |
})); |
||||
|
|
||||
// fstat
|
// fstat
|
||||
fs.open('.', 'r', undefined, function(err, fd) { |
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) { |
||||
assert.ok(!err); |
assert.ok(!err); |
||||
assert.ok(fd); |
assert.ok(fd); |
||||
|
|
||||
fs.fstat(fd, function(err, stats) { |
fs.fstat(fd, common.mustCall(function(err, stats) { |
||||
if (err) { |
assert.ifError(err); |
||||
got_error = true; |
assert.ok(stats.mtime instanceof Date); |
||||
} else { |
fs.close(fd); |
||||
console.dir(stats); |
|
||||
assert.ok(stats.mtime instanceof Date); |
|
||||
success_count++; |
|
||||
fs.close(fd); |
|
||||
} |
|
||||
assert(this === global); |
assert(this === global); |
||||
}); |
})); |
||||
|
|
||||
assert(this === global); |
assert(this === global); |
||||
}); |
})); |
||||
|
|
||||
// fstatSync
|
// fstatSync
|
||||
fs.open('.', 'r', undefined, function(err, fd) { |
fs.open('.', 'r', undefined, common.mustCall(function(err, fd) { |
||||
var stats; |
var stats; |
||||
try { |
try { |
||||
stats = fs.fstatSync(fd); |
stats = fs.fstatSync(fd); |
||||
} catch (err) { |
} catch (err) { |
||||
got_error = true; |
common.fail(err); |
||||
} |
} |
||||
if (stats) { |
if (stats) { |
||||
console.dir(stats); |
console.dir(stats); |
||||
assert.ok(stats.mtime instanceof Date); |
assert.ok(stats.mtime instanceof Date); |
||||
success_count++; |
|
||||
} |
} |
||||
fs.close(fd); |
fs.close(fd); |
||||
}); |
})); |
||||
|
|
||||
console.log(`stating: ${__filename}`); |
console.log(`stating: ${__filename}`); |
||||
fs.stat(__filename, function(err, s) { |
fs.stat(__filename, common.mustCall(function(err, s) { |
||||
if (err) { |
assert.ifError(err); |
||||
got_error = true; |
|
||||
} else { |
|
||||
console.dir(s); |
|
||||
success_count++; |
|
||||
|
|
||||
console.log('isDirectory: ' + JSON.stringify(s.isDirectory())); |
console.dir(s); |
||||
assert.equal(false, s.isDirectory()); |
|
||||
|
|
||||
console.log('isFile: ' + JSON.stringify(s.isFile())); |
console.log('isDirectory: ' + JSON.stringify(s.isDirectory())); |
||||
assert.equal(true, s.isFile()); |
assert.equal(false, s.isDirectory()); |
||||
|
|
||||
console.log('isSocket: ' + JSON.stringify(s.isSocket())); |
console.log('isFile: ' + JSON.stringify(s.isFile())); |
||||
assert.equal(false, s.isSocket()); |
assert.equal(true, s.isFile()); |
||||
|
|
||||
console.log('isBlockDevice: ' + JSON.stringify(s.isBlockDevice())); |
console.log('isSocket: ' + JSON.stringify(s.isSocket())); |
||||
assert.equal(false, s.isBlockDevice()); |
assert.equal(false, s.isSocket()); |
||||
|
|
||||
console.log('isCharacterDevice: ' + JSON.stringify(s.isCharacterDevice())); |
console.log('isBlockDevice: ' + JSON.stringify(s.isBlockDevice())); |
||||
assert.equal(false, s.isCharacterDevice()); |
assert.equal(false, s.isBlockDevice()); |
||||
|
|
||||
console.log('isFIFO: ' + JSON.stringify(s.isFIFO())); |
console.log('isCharacterDevice: ' + JSON.stringify(s.isCharacterDevice())); |
||||
assert.equal(false, s.isFIFO()); |
assert.equal(false, s.isCharacterDevice()); |
||||
|
|
||||
console.log('isSymbolicLink: ' + JSON.stringify(s.isSymbolicLink())); |
console.log('isFIFO: ' + JSON.stringify(s.isFIFO())); |
||||
assert.equal(false, s.isSymbolicLink()); |
assert.equal(false, s.isFIFO()); |
||||
|
|
||||
assert.ok(s.mtime instanceof Date); |
|
||||
} |
|
||||
}); |
|
||||
|
|
||||
process.on('exit', function() { |
console.log('isSymbolicLink: ' + JSON.stringify(s.isSymbolicLink())); |
||||
assert.equal(5, success_count); |
assert.equal(false, s.isSymbolicLink()); |
||||
assert.equal(false, got_error); |
|
||||
}); |
|
||||
|
|
||||
|
assert.ok(s.mtime instanceof Date); |
||||
|
})); |
||||
|
@ -1,26 +1,16 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
|
||||
var http = require('http'); |
var http = require('http'); |
||||
|
|
||||
var request = 0; |
var server = http.createServer(common.mustCall(function(req, res) { |
||||
var response = 0; |
|
||||
process.on('exit', function() { |
|
||||
assert.equal(request, 1, 'http server "request" callback was not called'); |
|
||||
assert.equal(response, 1, 'http request "response" callback was not called'); |
|
||||
}); |
|
||||
|
|
||||
var server = http.createServer(function(req, res) { |
|
||||
request++; |
|
||||
res.end(); |
res.end(); |
||||
}).listen(0, function() { |
})).listen(0, common.mustCall(function() { |
||||
var options = { |
var options = { |
||||
agent: null, |
agent: null, |
||||
port: this.address().port |
port: this.address().port |
||||
}; |
}; |
||||
http.get(options, function(res) { |
http.get(options, common.mustCall(function(res) { |
||||
response++; |
|
||||
res.resume(); |
res.resume(); |
||||
server.close(); |
server.close(); |
||||
}); |
})); |
||||
}); |
})); |
||||
|
@ -1,26 +1,15 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var http = require('http'); |
var http = require('http'); |
||||
|
|
||||
var gotError = false; |
var server1 = http.createServer(common.fail); |
||||
|
server1.listen(0, '127.0.0.1', common.mustCall(function() { |
||||
|
var server2 = http.createServer(common.fail); |
||||
|
server2.listen(this.address().port, '127.0.0.1', common.fail); |
||||
|
|
||||
process.on('exit', function() { |
server2.on('error', common.mustCall(function(e) { |
||||
assert(gotError); |
|
||||
}); |
|
||||
|
|
||||
function dontCall() { |
|
||||
assert(false); |
|
||||
} |
|
||||
|
|
||||
var server1 = http.createServer(dontCall); |
|
||||
server1.listen(0, '127.0.0.1', function() { |
|
||||
var server2 = http.createServer(dontCall); |
|
||||
server2.listen(this.address().port, '127.0.0.1', dontCall); |
|
||||
|
|
||||
server2.on('error', function(e) { |
|
||||
assert.equal(e.code, 'EADDRINUSE'); |
assert.equal(e.code, 'EADDRINUSE'); |
||||
server1.close(); |
server1.close(); |
||||
gotError = true; |
})); |
||||
}); |
})); |
||||
}); |
|
||||
|
@ -1,29 +1,20 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
|
||||
var http = require('http'); |
var http = require('http'); |
||||
var server = http.createServer(function(req, res) { |
var server = http.createServer(function(req, res) { |
||||
res.end(); |
res.end(); |
||||
}); |
}); |
||||
var count = 0; |
|
||||
server.listen(0, function() { |
server.listen(0, common.mustCall(function() { |
||||
var req = http.request({ |
var req = http.request({ |
||||
port: this.address().port |
port: this.address().port |
||||
}, function() { |
}, common.fail); |
||||
assert(false, 'should not receive data'); |
|
||||
}); |
|
||||
|
|
||||
req.on('abort', function() { |
req.on('abort', common.mustCall(function() { |
||||
// should only be emitted once
|
|
||||
count++; |
|
||||
server.close(); |
server.close(); |
||||
}); |
})); |
||||
|
|
||||
req.end(); |
req.end(); |
||||
req.abort(); |
req.abort(); |
||||
req.abort(); |
req.abort(); |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal(count, 1); |
|
||||
}); |
|
||||
|
@ -1,52 +1,44 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var http = require('http'); |
var http = require('http'); |
||||
|
|
||||
var N = 1024; |
var N = 1024; |
||||
var bytesReceived = 0; |
|
||||
var server_req_complete = false; |
|
||||
var client_res_complete = false; |
|
||||
|
|
||||
var server = http.createServer(function(req, res) { |
var server = http.createServer(common.mustCall(function(req, res) { |
||||
assert.equal('POST', req.method); |
assert.equal('POST', req.method); |
||||
|
|
||||
|
var bytesReceived = 0; |
||||
|
|
||||
req.on('data', function(chunk) { |
req.on('data', function(chunk) { |
||||
bytesReceived += chunk.length; |
bytesReceived += chunk.length; |
||||
}); |
}); |
||||
|
|
||||
req.on('end', function() { |
req.on('end', common.mustCall(function() { |
||||
server_req_complete = true; |
assert.strictEqual(N, bytesReceived); |
||||
console.log('request complete from server'); |
console.log('request complete from server'); |
||||
res.writeHead(200, {'Content-Type': 'text/plain'}); |
res.writeHead(200, {'Content-Type': 'text/plain'}); |
||||
res.write('hello\n'); |
res.write('hello\n'); |
||||
res.end(); |
res.end(); |
||||
}); |
})); |
||||
}); |
})); |
||||
server.listen(0); |
server.listen(0); |
||||
|
|
||||
server.on('listening', function() { |
server.on('listening', common.mustCall(function() { |
||||
var req = http.request({ |
var req = http.request({ |
||||
port: this.address().port, |
port: this.address().port, |
||||
method: 'POST', |
method: 'POST', |
||||
path: '/' |
path: '/' |
||||
}, function(res) { |
}, common.mustCall(function(res) { |
||||
res.setEncoding('utf8'); |
res.setEncoding('utf8'); |
||||
res.on('data', function(chunk) { |
res.on('data', function(chunk) { |
||||
console.log(chunk); |
console.log(chunk); |
||||
}); |
}); |
||||
res.on('end', function() { |
res.on('end', common.mustCall(function() { |
||||
client_res_complete = true; |
|
||||
server.close(); |
server.close(); |
||||
}); |
})); |
||||
}); |
})); |
||||
|
|
||||
req.write(Buffer.allocUnsafe(N)); |
req.write(Buffer.allocUnsafe(N)); |
||||
req.end(); |
req.end(); |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal(N, bytesReceived); |
|
||||
assert.equal(true, server_req_complete); |
|
||||
assert.equal(true, client_res_complete); |
|
||||
}); |
|
||||
|
@ -1,55 +1,46 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var http = require('http'); |
var http = require('http'); |
||||
|
|
||||
var sent_body = ''; |
var server = http.createServer(common.mustCall(function(req, res) { |
||||
var server_req_complete = false; |
|
||||
var client_res_complete = false; |
|
||||
|
|
||||
var server = http.createServer(function(req, res) { |
|
||||
assert.equal('POST', req.method); |
assert.equal('POST', req.method); |
||||
req.setEncoding('utf8'); |
req.setEncoding('utf8'); |
||||
|
|
||||
|
var sent_body = ''; |
||||
|
|
||||
req.on('data', function(chunk) { |
req.on('data', function(chunk) { |
||||
console.log('server got: ' + JSON.stringify(chunk)); |
console.log('server got: ' + JSON.stringify(chunk)); |
||||
sent_body += chunk; |
sent_body += chunk; |
||||
}); |
}); |
||||
|
|
||||
req.on('end', function() { |
req.on('end', common.mustCall(function() { |
||||
server_req_complete = true; |
assert.strictEqual('1\n2\n3\n', sent_body); |
||||
console.log('request complete from server'); |
console.log('request complete from server'); |
||||
res.writeHead(200, {'Content-Type': 'text/plain'}); |
res.writeHead(200, {'Content-Type': 'text/plain'}); |
||||
res.write('hello\n'); |
res.write('hello\n'); |
||||
res.end(); |
res.end(); |
||||
}); |
})); |
||||
}); |
})); |
||||
server.listen(0); |
server.listen(0); |
||||
|
|
||||
server.on('listening', function() { |
server.on('listening', common.mustCall(function() { |
||||
var req = http.request({ |
var req = http.request({ |
||||
port: this.address().port, |
port: this.address().port, |
||||
method: 'POST', |
method: 'POST', |
||||
path: '/' |
path: '/' |
||||
}, function(res) { |
}, common.mustCall(function(res) { |
||||
res.setEncoding('utf8'); |
res.setEncoding('utf8'); |
||||
res.on('data', function(chunk) { |
res.on('data', function(chunk) { |
||||
console.log(chunk); |
console.log(chunk); |
||||
}); |
}); |
||||
res.on('end', function() { |
res.on('end', common.mustCall(function() { |
||||
client_res_complete = true; |
|
||||
server.close(); |
server.close(); |
||||
}); |
})); |
||||
}); |
})); |
||||
|
|
||||
req.write('1\n'); |
req.write('1\n'); |
||||
req.write('2\n'); |
req.write('2\n'); |
||||
req.write('3\n'); |
req.write('3\n'); |
||||
req.end(); |
req.end(); |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal('1\n2\n3\n', sent_body); |
|
||||
assert.equal(true, server_req_complete); |
|
||||
assert.equal(true, client_res_complete); |
|
||||
}); |
|
||||
|
@ -1,26 +1,23 @@ |
|||||
'use strict'; |
'use strict'; |
||||
require('../common'); |
const common = require('../common'); |
||||
var assert = require('assert'); |
var assert = require('assert'); |
||||
var net = require('net'); |
var net = require('net'); |
||||
var http = require('http'); |
var http = require('http'); |
||||
|
|
||||
var body = ''; |
|
||||
|
|
||||
var server = net.createServer(function(socket) { |
var server = net.createServer(function(socket) { |
||||
// Neither Content-Length nor Connection
|
// Neither Content-Length nor Connection
|
||||
socket.end('HTTP/1.1 200 ok\r\n\r\nHello'); |
socket.end('HTTP/1.1 200 ok\r\n\r\nHello'); |
||||
}).listen(0, function() { |
}).listen(0, common.mustCall(function() { |
||||
http.get({port: this.address().port}, function(res) { |
http.get({port: this.address().port}, common.mustCall(function(res) { |
||||
|
var body = ''; |
||||
|
|
||||
res.setEncoding('utf8'); |
res.setEncoding('utf8'); |
||||
res.on('data', function(chunk) { |
res.on('data', function(chunk) { |
||||
body += chunk; |
body += chunk; |
||||
}); |
}); |
||||
res.on('end', function() { |
res.on('end', common.mustCall(function() { |
||||
|
assert.strictEqual(body, 'Hello'); |
||||
server.close(); |
server.close(); |
||||
}); |
})); |
||||
}); |
})); |
||||
}); |
})); |
||||
|
|
||||
process.on('exit', function() { |
|
||||
assert.equal(body, 'Hello'); |
|
||||
}); |
|
||||
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue