11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst domain = require('domain');
251cb0ef41Sopenharmony_ciconst http = require('http');
261cb0ef41Sopenharmony_ciconst assert = require('assert');
271cb0ef41Sopenharmony_ciconst debug = require('util').debuglog('test');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciprocess.on('warning', common.mustNotCall());
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst objects = { foo: 'bar', baz: {}, num: 42, arr: [1, 2, 3] };
321cb0ef41Sopenharmony_ciobjects.baz.asdf = objects;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cilet serverCaught = 0;
351cb0ef41Sopenharmony_cilet clientCaught = 0;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciconst server = http.createServer(function(req, res) {
381cb0ef41Sopenharmony_ci  const dom = domain.create();
391cb0ef41Sopenharmony_ci  req.resume();
401cb0ef41Sopenharmony_ci  dom.add(req);
411cb0ef41Sopenharmony_ci  dom.add(res);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  dom.on('error', function(er) {
441cb0ef41Sopenharmony_ci    serverCaught++;
451cb0ef41Sopenharmony_ci    debug('horray! got a server error', er);
461cb0ef41Sopenharmony_ci    // Try to send a 500.  If that fails, oh well.
471cb0ef41Sopenharmony_ci    res.writeHead(500, { 'content-type': 'text/plain' });
481cb0ef41Sopenharmony_ci    res.end(er.stack || er.message || 'Unknown error');
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  dom.run(function() {
521cb0ef41Sopenharmony_ci    // Now, an action that has the potential to fail!
531cb0ef41Sopenharmony_ci    // if you request 'baz', then it'll throw a JSON circular ref error.
541cb0ef41Sopenharmony_ci    const data = JSON.stringify(objects[req.url.replace(/[^a-z]/g, '')]);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    // This line will throw if you pick an unknown key
571cb0ef41Sopenharmony_ci    assert.notStrictEqual(data, undefined);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    res.writeHead(200);
601cb0ef41Sopenharmony_ci    res.end(data);
611cb0ef41Sopenharmony_ci  });
621cb0ef41Sopenharmony_ci});
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciserver.listen(0, next);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cifunction next() {
671cb0ef41Sopenharmony_ci  const port = this.address().port;
681cb0ef41Sopenharmony_ci  debug(`listening on localhost:${port}`);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  let requests = 0;
711cb0ef41Sopenharmony_ci  let responses = 0;
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  makeReq('/');
741cb0ef41Sopenharmony_ci  makeReq('/foo');
751cb0ef41Sopenharmony_ci  makeReq('/arr');
761cb0ef41Sopenharmony_ci  makeReq('/baz');
771cb0ef41Sopenharmony_ci  makeReq('/num');
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  function makeReq(p) {
801cb0ef41Sopenharmony_ci    requests++;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    const dom = domain.create();
831cb0ef41Sopenharmony_ci    dom.on('error', function(er) {
841cb0ef41Sopenharmony_ci      clientCaught++;
851cb0ef41Sopenharmony_ci      debug('client error', er);
861cb0ef41Sopenharmony_ci      req.socket.destroy();
871cb0ef41Sopenharmony_ci    });
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    const req = http.get({ host: 'localhost', port: port, path: p });
901cb0ef41Sopenharmony_ci    dom.add(req);
911cb0ef41Sopenharmony_ci    req.on('response', function(res) {
921cb0ef41Sopenharmony_ci      responses++;
931cb0ef41Sopenharmony_ci      debug(`requests=${requests} responses=${responses}`);
941cb0ef41Sopenharmony_ci      if (responses === requests) {
951cb0ef41Sopenharmony_ci        debug('done, closing server');
961cb0ef41Sopenharmony_ci        // no more coming.
971cb0ef41Sopenharmony_ci        server.close();
981cb0ef41Sopenharmony_ci      }
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci      dom.add(res);
1011cb0ef41Sopenharmony_ci      let d = '';
1021cb0ef41Sopenharmony_ci      res.on('data', function(c) {
1031cb0ef41Sopenharmony_ci        d += c;
1041cb0ef41Sopenharmony_ci      });
1051cb0ef41Sopenharmony_ci      res.on('end', function() {
1061cb0ef41Sopenharmony_ci        debug('trying to parse json', d);
1071cb0ef41Sopenharmony_ci        d = JSON.parse(d);
1081cb0ef41Sopenharmony_ci        debug('json!', d);
1091cb0ef41Sopenharmony_ci      });
1101cb0ef41Sopenharmony_ci    });
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciprocess.on('exit', function() {
1151cb0ef41Sopenharmony_ci  assert.strictEqual(serverCaught, 2);
1161cb0ef41Sopenharmony_ci  assert.strictEqual(clientCaught, 2);
1171cb0ef41Sopenharmony_ci  debug('ok');
1181cb0ef41Sopenharmony_ci});
119