• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'use strict';
23const common = require('../common');
24const assert = require('assert');
25const http = require('http');
26const net = require('net');
27
28const webPort = common.PORT;
29const tcpPort = webPort + 1;
30const bufferSize = 5 * 1024 * 1024;
31
32let listenCount = 0;
33let gotThanks = false;
34let tcpLengthSeen = 0;
35
36
37/*
38 * 5MB of random buffer.
39 */
40const buffer = Buffer.allocUnsafe(bufferSize);
41for (let i = 0; i < buffer.length; i++) {
42  buffer[i] = parseInt(Math.random() * 10000) % 256;
43}
44
45
46const web = http.Server(common.mustCall((req, res) => {
47  web.close();
48
49  const socket = net.Stream();
50  socket.connect(tcpPort);
51
52  socket.on('connect', common.mustCall());
53
54  req.pipe(socket);
55
56  req.on('end', common.mustCall(() => {
57    res.writeHead(200);
58    res.write('thanks');
59    res.end();
60  }));
61
62  req.connection.on('error', (e) => {
63    assert.ifError(e);
64  });
65}));
66
67web.listen(webPort, startClient);
68
69
70const tcp = net.Server(common.mustCall((s) => {
71  tcp.close();
72
73  let i = 0;
74
75  s.on('data', (d) => {
76    tcpLengthSeen += d.length;
77    for (let j = 0; j < d.length; j++) {
78      assert.strictEqual(d[j], buffer[i]);
79      i++;
80    }
81  });
82
83  s.on('end', common.mustCall(() => {
84    s.end();
85  }));
86
87  s.on('error', (e) => {
88    assert.ifError(e);
89  });
90}));
91
92tcp.listen(tcpPort, startClient);
93
94function startClient() {
95  listenCount++;
96  if (listenCount < 2) return;
97
98  const req = http.request({
99    port: common.PORT,
100    method: 'GET',
101    path: '/',
102    headers: { 'content-length': buffer.length }
103  }, common.mustCall((res) => {
104    res.setEncoding('utf8');
105    res.on('data', common.mustCall((string) => {
106      assert.strictEqual(string, 'thanks');
107      gotThanks = true;
108    }));
109  }));
110  req.write(buffer);
111  req.end();
112}
113
114process.on('exit', () => {
115  assert.ok(gotThanks);
116  assert.strictEqual(tcpLengthSeen, bufferSize);
117});
118