0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 22:46:31 +01:00
nodejs/test/simple/test-net-connect-buffer.js

62 lines
1.2 KiB
JavaScript
Raw Normal View History

var common = require('../common');
var assert = require('assert');
var net = require('net');
var tcpPort = common.PORT;
2010-12-16 00:47:02 +01:00
var fooWritten = false;
var connectHappened = false;
2010-12-04 23:45:52 +01:00
var tcp = net.Server(function(s) {
tcp.close();
2010-12-04 23:45:52 +01:00
console.log('tcp server connection');
2010-12-04 23:45:52 +01:00
var buf = '';
s.on('data', function(d) {
buf += d;
});
2010-12-04 23:45:52 +01:00
s.on('end', function() {
assert.equal('foobar', buf);
console.log('tcp socket disconnect');
s.end();
});
2010-12-04 23:45:52 +01:00
s.on('error', function(e) {
console.log('tcp server-side error: ' + e.message);
process.exit(1);
});
});
2010-12-16 00:47:02 +01:00
tcp.listen(common.PORT, function () {
var socket = net.Stream();
2010-12-04 23:45:52 +01:00
console.log('Connecting to socket');
socket.connect(tcpPort);
2010-12-16 00:47:02 +01:00
2010-12-04 23:45:52 +01:00
socket.on('connect', function() {
console.log('socket connected');
2010-12-16 00:47:02 +01:00
connectHappened = true;
});
2010-12-04 23:45:52 +01:00
assert.equal('opening', socket.readyState);
2010-12-16 00:47:02 +01:00
var r = socket.write('foo', function () {
fooWritten = true;
assert.ok(connectHappened);
console.error("foo written");
});
assert.equal(false, r);
2010-12-04 23:45:52 +01:00
socket.end('bar');
2010-12-04 23:45:52 +01:00
assert.equal('opening', socket.readyState);
2010-12-16 00:47:02 +01:00
});
process.on('exit', function () {
assert.ok(connectHappened);
assert.ok(fooWritten);
});