Code coverage report for app/utils/http_client.js

Statements: 96% (48 / 50)      Branches: 92.86% (26 / 28)      Functions: 100% (8 / 8)      Lines: 96% (48 / 50)      Ignored: none     

All files » app/utils/ » http_client.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    1                                     1         1                   25 25   25 25 6   19                               21 9     21             21 21 12 12   21   21               55 55 55 55 38 21 21     21 21 21   17     38 38 38 38   17                         9 6   9   9 9 9 9 9 9     9 9 2   7                       2      
"use strict";
 
;require.register("utils/http_client", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  /**
   * App.HttpClient perform an ajax request
   */
  App.HttpClient = Em.Object.create({
 
    /**
     *
     * @param jqXHR
     * @param textStatus
     * @param errorThrown
     * @param url api that invoked this callback function
     */
    defaultErrorHandler: function defaultErrorHandler(jqXHR, textStatus, errorThrown, url) {
      try {
        var json = $.parseJSON(jqXHR.responseText);
      } catch (err) {}
      App.ajax.defaultErrorHandler(jqXHR, url);
      if (json) {
        Em.assert("HttpClient:", json);
      } else {
        Iif (!$.mocho) {
          // don't use this assert on tests
          Em.assert("HttpClient:", errorThrown);
        }
      }
    },
 
    /**
     * @param {string} url
     * @param {Object} ajaxOptions
     * @param {App.ServerDataMapper} mapper - json processor
     * @param {boolean} isGetAsPost - if true, do POST-request equal to GET-request but with some params put to body
     * @param {callback} errorHandler
     */
    request: function request(url, ajaxOptions, mapper, errorHandler, isGetAsPost) {
 
      if (!errorHandler) {
        errorHandler = this.defaultErrorHandler;
      }
 
      var xhr = new XMLHttpRequest(),
          curTime = App.dateTime(),
          method = isGetAsPost ? 'POST' : 'GET',
          params = isGetAsPost ? JSON.stringify({
        "RequestInfo": { "query": ajaxOptions.params }
      }) : null;
 
      xhr.open(method, url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
      if (isGetAsPost) {
        xhr.setRequestHeader("X-Http-Method-Override", "GET");
        xhr.setRequestHeader("Content-type", "text/plain");
      }
      xhr.send(params);
 
      this.onReady(xhr, "", ajaxOptions, mapper, errorHandler, url);
    },
 
    /**
     This function checks if we get response from server
     Not using onreadystatechange cuz of possible closure
     */
    onReady: function onReady(xhr, tm, tmp_val, mapper, errorHandler, url) {
      var self = this;
      clearTimeout(tm);
      var timeout = setTimeout(function () {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            var response = $.parseJSON(xhr.responseText);
            Iif (tmp_val.beforeMap) {
              tmp_val.beforeMap.call(self, response);
            }
            mapper.map(response);
            tmp_val.complete.call(self);
            xhr.abort();
          } else {
            errorHandler(xhr, "error", xhr.statusText, url);
          }
 
          tmp_val = null;
          xhr = null;
          clearTimeout(timeout);
          timeout = null;
        } else {
          self.onReady(xhr, timeout, tmp_val, mapper, errorHandler, url);
        }
      }, 10);
    },
 
    /**
     * @param {string} url
     * @param {App.ServerDataMapper} mapper - json processor
     * @param {Object} data - ajax data property
     * @param {callback} errorHandler
     * @param {number} interval - frequency request
     */
    get: function get(url, mapper, data, errorHandler, interval) {
      if (!errorHandler && data.error) {
        errorHandler = data.error;
      }
      var client = this,
          request = function request() {
        var isGetAsPost = Boolean(data.doGetAsPost && !App.get('testMode'));
        client.request(url, data, mapper, errorHandler, isGetAsPost);
        url = null;
        data = null;
        mapper = null;
        errorHandler = null;
      };
 
      interval = "" + interval;
      if (interval.match(/\d+/)) {
        $.periodic({ period: interval }, request);
      } else {
        request();
      }
    },
 
    /**
     * @param {string} url
     * @param {Object} data - ajax data property
     * @param {App.ServerDataMapper} mapper - json processor
     * @param {callback} errorHandler
     * @param {number} interval - frequency request
     */
    post: function post(url, data, mapper, errorHandler, interval) {
      this.get.apply(this, arguments);
    }
  });
});