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