• 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 domain = require('domain');
25const http = require('http');
26const assert = require('assert');
27const debug = require('util').debuglog('test');
28
29process.on('warning', common.mustNotCall());
30
31const objects = { foo: 'bar', baz: {}, num: 42, arr: [1, 2, 3] };
32objects.baz.asdf = objects;
33
34let serverCaught = 0;
35let clientCaught = 0;
36
37const server = http.createServer(function(req, res) {
38  const dom = domain.create();
39  req.resume();
40  dom.add(req);
41  dom.add(res);
42
43  dom.on('error', function(er) {
44    serverCaught++;
45    debug('horray! got a server error', er);
46    // Try to send a 500.  If that fails, oh well.
47    res.writeHead(500, { 'content-type': 'text/plain' });
48    res.end(er.stack || er.message || 'Unknown error');
49  });
50
51  dom.run(function() {
52    // Now, an action that has the potential to fail!
53    // if you request 'baz', then it'll throw a JSON circular ref error.
54    const data = JSON.stringify(objects[req.url.replace(/[^a-z]/g, '')]);
55
56    // This line will throw if you pick an unknown key
57    assert.notStrictEqual(data, undefined);
58
59    res.writeHead(200);
60    res.end(data);
61  });
62});
63
64server.listen(0, next);
65
66function next() {
67  const port = this.address().port;
68  debug(`listening on localhost:${port}`);
69
70  let requests = 0;
71  let responses = 0;
72
73  makeReq('/');
74  makeReq('/foo');
75  makeReq('/arr');
76  makeReq('/baz');
77  makeReq('/num');
78
79  function makeReq(p) {
80    requests++;
81
82    const dom = domain.create();
83    dom.on('error', function(er) {
84      clientCaught++;
85      debug('client error', er);
86      req.socket.destroy();
87    });
88
89    const req = http.get({ host: 'localhost', port: port, path: p });
90    dom.add(req);
91    req.on('response', function(res) {
92      responses++;
93      debug(`requests=${requests} responses=${responses}`);
94      if (responses === requests) {
95        debug('done, closing server');
96        // no more coming.
97        server.close();
98      }
99
100      dom.add(res);
101      let d = '';
102      res.on('data', function(c) {
103        d += c;
104      });
105      res.on('end', function() {
106        debug('trying to parse json', d);
107        d = JSON.parse(d);
108        debug('json!', d);
109      });
110    });
111  }
112}
113
114process.on('exit', function() {
115  assert.strictEqual(serverCaught, 2);
116  assert.strictEqual(clientCaught, 2);
117  debug('ok');
118});
119