1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
25 var common = require('../common');
26 var assert = require('assert');
27 var http = require('http');
28 var net = require('net');
30 var webPort = common.PORT;
31 var tcpPort = webPort + 1;
34 var gotThanks = false;
35 var tcpLengthSeen = 0;
36 var bufferSize = 5 * 1024 * 1024;
40 * 5MB of random buffer.
42 var buffer = Buffer(bufferSize);
43 for (var i = 0; i < buffer.length; i++) {
44 buffer[i] = parseInt(Math.random() * 10000) % 256;
48 var web = http.Server(function(req, res) {
51 console.log(req.headers);
53 var socket = net.Stream();
54 socket.connect(tcpPort);
56 socket.on('connect', function() {
57 console.log('socket connected');
62 req.on('end', function() {
66 console.log('response with \'thanks\'');
69 req.connection.on('error', function(e) {
70 console.log('http server-side error: ' + e.message);
74 web.listen(webPort, startClient);
78 var tcp = net.Server(function(s) {
81 console.log('tcp server connection');
85 s.on('data', function(d) {
86 process.stdout.write('.');
87 tcpLengthSeen += d.length;
88 for (var j = 0; j < d.length; j++) {
89 assert.equal(buffer[i], d[j]);
94 s.on('end', function() {
95 console.log('tcp socket disconnect');
99 s.on('error', function(e) {
100 console.log('tcp server-side error: ' + e.message);
104 tcp.listen(tcpPort, startClient);
107 function startClient() {
109 if (listenCount < 2) return;
111 console.log('Making request');
113 var req = http.request({
117 headers: { 'content-length': buffer.length }
119 console.log('Got response');
120 res.setEncoding('utf8');
121 res.on('data', function(string) {
122 assert.equal('thanks', string);
128 console.error('ended request', req);
131 process.on('exit', function() {
132 assert.ok(gotThanks);
133 assert.equal(bufferSize, tcpLengthSeen);