Code coverage report for node-proxy-lite/main.js

Statements: 95.92% (47 / 49)      Branches: 73.33% (11 / 15)      Functions: 100% (11 / 11)      Lines: 95.92% (47 / 49)      Ignored: none     

All files » node-proxy-lite/ » main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                      7 7         20         3         3               3   3   3   3   3   3       8 8 8 12 12     8 4       4     4 4   2         2         2     2 2 2     2 2 2     2     2   2   2     2     8             2     2   2   2 2                 2     2   2   2 2           3    
#!/usr/bin/env node
/*jslint
  bitwise: true, browser: true,
  indent: 2,
  maxerr: 8,
  node: true, nomen: true,
  regexp: true,
  stupid: true,
  todo: true
*/
// declare module vars
var exports, required, state, stateRestore;
stateRestore = function (state2) {
  /*
    this function is used by testMock to restore the local state var
  */
  'use strict';
  state = state2;
};
 
 
 
(function submoduleMainNodejs() {
  /*
    this nodejs submodule exports the main api
  */
  'use strict';
  var local = {
    _name: 'main.submoduleMainNodejs',
 
    _init: function () {
      /*
        this function inits the submodule
      */
      // init export object
      exports = module.exports = require(__dirname + '/utility2.js');
      // export __dirname
      exports.__dirname = __dirname;
      // export __filename
      exports.__filename = __filename;
      // init this submodule
      exports.initSubmodule(local);
      // init required object
      required = exports.required;
      // init state object
      state = exports.state;
    },
 
    'serverPathHandlerDict_/': function (request, response, next) {
      var finished, modeIo, onEventIo, options, proxyResponse, timerTimeout;
      modeIo = 0;
      onEventIo = function (error) {
        modeIo = error instanceof Error ? -1 : modeIo + 1;
        switch (modeIo) {
        case 1:
          // serve main page
          if (request.urlPathNormalized === '/') {
            exports.serverRespondData(response, 200, 'text/html', exports.textFormat(
              state.fileDict['/public/main.html'].data,
              { stateBrowserJson: state.stateBrowserJson }
            ));
            return;
          }
          // init url options
          options = required.url.parse(request.url.replace((/^\//), ''));
          if (options.protocol) {
            // set timerTimeout
            timerTimeout = exports.onEventTimeout(
              onEventIo,
              state.timeoutDefault,
              'proxy ' + request.url
            );
            (options.protocol === 'https:' ? required.https : required.http)
              .request(options, onEventIo)
              // handle error event
              .on('error', onEventIo)
              .end();
            return;
          }
          // handle invalid url case
          modeIo = -2;
          onEventIo();
          break;
        case 2:
          // pipe proxy response back to request
          proxyResponse = error;
          proxyResponse.pipe(response).on('error', onEventIo);
          break;
        default:
          // ignore error / data if already finished
          Iif (finished) {
            return;
          }
          finished = true;
          // cleanup timerTimeout
          clearTimeout(timerTimeout);
          // cleanup proxyResponse socket
          Iif (proxyResponse) {
            proxyResponse.destroy();
          }
          next(error);
        }
      };
      onEventIo();
    },
 
    '_serverPathHandlerDict_/_default_test': function (onEventError) {
      /*
        this function tests serverPathHandlerDict_/'s default handing behavior
      */
      exports.ajax({
        url: state.localhost + '/' + state.localhost + '/test/hello.json'
      }, function (error, data) {
        exports.testTryCatch(function () {
          // validate no error occurred
          exports.assert(!error, error);
          // validate data
          exports.assert(data === '"hello"', data);
          onEventError();
        }, onEventError);
      });
    },
 
    '_serverPathHandlerDict_/_invalidUrl_test': function (onEventError) {
      /*
        this function tests serverPathHandlerDict_/'s invalid url handing behavior
      */
      exports.ajax({
        url: '/invalid/url'
      }, function (error, data) {
        exports.testTryCatch(function () {
          // validate error occurred
          exports.assert(error instanceof Error, error);
          // validate data
          exports.assert(data === '404 Not Found', data);
          onEventError();
        }, onEventError);
      });
    }
 
  };
  local._init();
}());