Code coverage report for api/CORSNode.js

Statements: 93.15% (68 / 73)      Branches: 88.57% (31 / 35)      Functions: 80% (8 / 10)      Lines: 93.15% (68 / 73)      Ignored: none     

All files » api/ » CORSNode.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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 1611 1 1 1   1         1 1       1   1 10       10 10         1 82         82 1 1           1     81       81   81 81 73 73   81   81 81 22 22   22   22 31 31 31             22 22 22     22 7 7       22 22 3       22           22         81 8   81   81   81     81                       81 36       81 59     81 1 1     81 81 81 36 36   45   81             81     1 1    
var Q = require("q");
var http = require("http");
var https = require("https");
var url = require("url");
 
var CORS = require("./CORS").CORS;
 
/*
 * Helper function which handles IE
 */
CORS.buildXhr = function() {};
CORS.setHeader = function(xhr, key, value) {
  xhr.setHeader(key, value);
};
 
var COOKIE_JAR = {};
 
CORS.clearCookies = function(hostname) {
  Iif (!hostname) {
    return;
  }
 
  this.warn("clearing cookies on " + hostname);
  delete COOKIE_JAR[hostname];
};
/*
 * Functions for well formed CORS requests
 */
CORS.makeCORSRequest = function(options) {
  var self = this,
    deferred = Q.defer(),
    data = "",
    xhr;
 
  if (!options || !options.url) {
    Q.nextTick(function() {
      deferred.reject({
        status: 400,
        details: options,
        userFriendlyErrors: ["Url must be defined"]
      });
    });
    return deferred.promise;
  }
 
  this.preprocess(options, deferred);
 
  //forcing production server
  // options.url = options.url.replace("corpusdev", "corpus");
  var urlObject = url.parse(options.url);
 
  var httpOrHttps = http;
  if (urlObject.protocol === "https:") {
    httpOrHttps = https;
    urlObject.port = urlObject.port || 443;
  }
  delete urlObject.protocol;
 
  urlObject.method = options.method || "GET";
  xhr = httpOrHttps.request(urlObject, function(res) {
    var output = "";
    res.setEncoding("utf8");
 
    self.debug(options.url + " " + options.complete + ":  requesting ");
 
    res.on("data", function(chunk) {
      output += chunk;
      self.debug(options.url + " " + options.complete + ":  ondata ");
      self.onprogress.apply(self, [options, {
        lengthComputable: true,
        loaded: output.length - chunk.length,
        total: output.length
      }, deferred]);
    });
 
    res.on("end", function() {
      xhr.responseText = output;
      xhr.status = res.statusCode;
 
      // Save cookies
      if (res.headers && res.headers["set-cookie"]) {
        COOKIE_JAR[urlObject.host] = res.headers["set-cookie"];
        self.debug(options.url + " " + options.complete + ": cookies", COOKIE_JAR);
      }
 
      // Remove cookies
      self.debug(options.url + " " + options.complete + ":  response headers " + options.url, res.headers);
      if (options.method === "DELETE" && urlObject.path === "/_session") {
        self.clearCookies(urlObject.host);
      }
 
      // Final progress event
      self.onprogress.apply(self, [options, {
        lengthComputable: true,
        loaded: output.length,
        total: output.length
      }, deferred]);
 
      self.onload.apply(self, [options, {}, deferred]);
    });
  });
 
  // Include cookies
  if (COOKIE_JAR[urlObject.host]) {
    xhr.setHeader("Cookie", COOKIE_JAR[urlObject.host].join(";"));
  }
  self.debug(options.url + " " + options.complete + ": request cookies " + xhr.getHeader("cookie"));
 
  xhr.setHeader("Content-type", "application/json");
 
  xhr.setTimeout(options.timeout || this.timeout);
 
  // If it contains files, make it into a mulitpart upload
  Iif (options && options.data && options.data.files) {
    // console.log("converting to formdata ", options.data);
 
    // data = new FormData();
    // for (var part in options.data) {
    //   if (options.data.hasOwnProperty(part)) {
    //     data.append(part, options.data[part]);
    //   }
    // }
    // data = data;
    // xhr.setHeader("Content-Type", "multipart/form-data");
  } else {
    if (options.data) {
      data = JSON.stringify(options.data);
    }
  }
 
  xhr.on("error", function(err) {
    self.onerror.apply(self, [options, err, deferred]);
  });
 
  xhr.on("timeout", function(err) {
    self.ontimeout.apply(self, [options, err, deferred]);
    xhr.abort();
  });
 
  options.xhr = xhr;
  try {
    if (data) {
      self.debug(options.url + " " + options.complete + ": sending ", data);
      xhr.write(data);
    } else {
      xhr.write("");
    }
    xhr.end();
  } catch (e) {
    self.warn("Caught an exception when calling send on xhr", e.stack);
    e.details = options;
    deferred.reject(e);
  }
 
  return deferred.promise;
};
 
Eif (exports) {
  exports.CORS = CORS;
}