node/test/sequential/test-pipe.js

112 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
2010-10-11 11:18:47 +08:00
var common = require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
2010-10-11 13:05:49 +08:00
2010-12-05 06:45:52 +08:00
var webPort = common.PORT;
2010-10-11 13:05:49 +08:00
var tcpPort = webPort + 1;
2010-10-11 11:18:47 +08:00
var listenCount = 0;
var gotThanks = false;
2010-10-11 13:05:49 +08:00
var tcpLengthSeen = 0;
var bufferSize = 5 * 1024 * 1024;
2010-10-11 11:18:47 +08:00
/*
* 5MB of random buffer.
2010-10-11 08:21:36 +08:00
*/
2010-10-11 13:05:49 +08:00
var buffer = Buffer(bufferSize);
2010-10-11 11:18:47 +08:00
for (var i = 0; i < buffer.length; i++) {
2010-12-05 06:45:52 +08:00
buffer[i] = parseInt(Math.random() * 10000) % 256;
2010-10-11 11:18:47 +08:00
}
2010-10-11 08:21:36 +08:00
2010-12-05 06:45:52 +08:00
var web = http.Server(function(req, res) {
2010-10-11 11:18:47 +08:00
web.close();
2010-10-11 08:21:36 +08:00
console.log(req.headers);
2010-10-11 11:18:47 +08:00
var socket = net.Stream();
socket.connect(tcpPort);
2010-12-05 06:45:52 +08:00
socket.on('connect', function() {
console.log('socket connected');
2010-10-11 13:05:49 +08:00
});
2010-10-11 11:18:47 +08:00
req.pipe(socket);
2010-10-11 08:21:36 +08:00
2010-12-05 06:45:52 +08:00
req.on('end', function() {
2010-10-11 08:21:36 +08:00
res.writeHead(200);
2010-12-05 06:45:52 +08:00
res.write('thanks');
2010-10-11 08:21:36 +08:00
res.end();
2010-12-05 06:45:52 +08:00
console.log('response with \'thanks\'');
2010-10-11 13:05:49 +08:00
});
2010-12-05 06:45:52 +08:00
req.connection.on('error', function(e) {
console.log('http server-side error: ' + e.message);
2010-10-11 13:05:49 +08:00
process.exit(1);
2010-10-11 08:21:36 +08:00
});
});
2010-10-11 11:18:47 +08:00
web.listen(webPort, startClient);
2010-12-05 06:45:52 +08:00
var tcp = net.Server(function(s) {
2010-10-11 11:18:47 +08:00
tcp.close();
2010-12-05 06:45:52 +08:00
console.log('tcp server connection');
2010-10-11 13:05:49 +08:00
2010-10-11 11:18:47 +08:00
var i = 0;
2010-12-05 06:45:52 +08:00
s.on('data', function(d) {
process.stdout.write('.');
2010-10-11 13:05:49 +08:00
tcpLengthSeen += d.length;
2010-10-11 11:18:47 +08:00
for (var j = 0; j < d.length; j++) {
2010-10-12 07:08:19 +08:00
assert.equal(buffer[i], d[j]);
2010-10-11 11:18:47 +08:00
i++;
}
});
2010-12-05 06:45:52 +08:00
s.on('end', function() {
console.log('tcp socket disconnect');
2010-10-11 13:05:49 +08:00
s.end();
});
2010-12-05 06:45:52 +08:00
s.on('error', function(e) {
console.log('tcp server-side error: ' + e.message);
2010-10-11 13:05:49 +08:00
process.exit(1);
2010-10-11 11:18:47 +08:00
});
});
tcp.listen(tcpPort, startClient);
2010-12-05 06:45:52 +08:00
function startClient() {
2010-10-11 11:18:47 +08:00
listenCount++;
2010-10-11 13:05:49 +08:00
if (listenCount < 2) return;
2010-10-11 11:18:47 +08:00
2010-12-05 06:45:52 +08:00
console.log('Making request');
2010-10-11 11:18:47 +08:00
var req = http.request({
port: common.PORT,
method: 'GET',
path: '/',
headers: { 'content-length': buffer.length }
}, function(res) {
2010-10-11 13:05:49 +08:00
console.log('Got response');
2010-10-11 11:18:47 +08:00
res.setEncoding('utf8');
2010-12-05 06:45:52 +08:00
res.on('data', function(string) {
assert.equal('thanks', string);
2010-10-11 11:18:47 +08:00
gotThanks = true;
});
});
req.write(buffer);
req.end();
console.error('ended request', req);
2010-10-11 11:18:47 +08:00
}
2010-12-05 06:45:52 +08:00
process.on('exit', function() {
2010-10-11 11:18:47 +08:00
assert.ok(gotThanks);
2010-10-11 13:05:49 +08:00
assert.equal(bufferSize, tcpLengthSeen);
2010-10-11 11:18:47 +08:00
});
2010-10-11 08:21:36 +08:00