• 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';
23require('../common');
24const assert = require('assert');
25const http = require('http');
26const url = require('url');
27
28const body1_s = '1111111111111111';
29const body2_s = '22222';
30
31const server = http.createServer(function(req, res) {
32  const body = url.parse(req.url).pathname === '/1' ? body1_s : body2_s;
33  res.writeHead(200, {
34    'Content-Type': 'text/plain',
35    'Content-Length': body.length
36  });
37  res.end(body);
38});
39server.listen(0);
40
41let body1 = '';
42let body2 = '';
43
44server.on('listening', function() {
45  const req1 = http.request({ port: this.address().port, path: '/1' });
46  req1.end();
47  req1.on('response', function(res1) {
48    res1.setEncoding('utf8');
49
50    res1.on('data', function(chunk) {
51      body1 += chunk;
52    });
53
54    res1.on('end', function() {
55      const req2 = http.request({ port: server.address().port, path: '/2' });
56      req2.end();
57      req2.on('response', function(res2) {
58        res2.setEncoding('utf8');
59        res2.on('data', function(chunk) { body2 += chunk; });
60        res2.on('end', function() { server.close(); });
61      });
62    });
63  });
64});
65
66process.on('exit', function() {
67  assert.strictEqual(body1_s, body1);
68  assert.strictEqual(body2_s, body2);
69});
70