9f1dae885b03d4fd1be0b68044ebb7ad6a198968
[platform/upstream/nodejs.git] / test / sequential / test-pipe.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22
23
24
25 var common = require('../common');
26 var assert = require('assert');
27 var http = require('http');
28 var net = require('net');
29
30 var webPort = common.PORT;
31 var tcpPort = webPort + 1;
32
33 var listenCount = 0;
34 var gotThanks = false;
35 var tcpLengthSeen = 0;
36 var bufferSize = 5 * 1024 * 1024;
37
38
39 /*
40  * 5MB of random buffer.
41  */
42 var buffer = Buffer(bufferSize);
43 for (var i = 0; i < buffer.length; i++) {
44   buffer[i] = parseInt(Math.random() * 10000) % 256;
45 }
46
47
48 var web = http.Server(function(req, res) {
49   web.close();
50
51   console.log(req.headers);
52
53   var socket = net.Stream();
54   socket.connect(tcpPort);
55
56   socket.on('connect', function() {
57     console.log('socket connected');
58   });
59
60   req.pipe(socket);
61
62   req.on('end', function() {
63     res.writeHead(200);
64     res.write('thanks');
65     res.end();
66     console.log('response with \'thanks\'');
67   });
68
69   req.connection.on('error', function(e) {
70     console.log('http server-side error: ' + e.message);
71     process.exit(1);
72   });
73 });
74 web.listen(webPort, startClient);
75
76
77
78 var tcp = net.Server(function(s) {
79   tcp.close();
80
81   console.log('tcp server connection');
82
83   var i = 0;
84
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]);
90       i++;
91     }
92   });
93
94   s.on('end', function() {
95     console.log('tcp socket disconnect');
96     s.end();
97   });
98
99   s.on('error', function(e) {
100     console.log('tcp server-side error: ' + e.message);
101     process.exit(1);
102   });
103 });
104 tcp.listen(tcpPort, startClient);
105
106
107 function startClient() {
108   listenCount++;
109   if (listenCount < 2) return;
110
111   console.log('Making request');
112
113   var req = http.request({
114     port: common.PORT,
115     method: 'GET',
116     path: '/',
117     headers: { 'content-length': buffer.length }
118   }, function(res) {
119     console.log('Got response');
120     res.setEncoding('utf8');
121     res.on('data', function(string) {
122       assert.equal('thanks', string);
123       gotThanks = true;
124     });
125   });
126   req.write(buffer);
127   req.end();
128   console.error('ended request', req);
129 }
130
131 process.on('exit', function() {
132   assert.ok(gotThanks);
133   assert.equal(bufferSize, tcpLengthSeen);
134 });
135