Repository: ambari
Updated Branches:
  refs/heads/branch-2.1 e181ea001 -> a755a3051


AMBARI-13391 Long API query should be made part of request body. (atkach)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/a755a305
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/a755a305
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/a755a305

Branch: refs/heads/branch-2.1
Commit: a755a3051d7687eb604139b8fa4139c7ab4a36b0
Parents: e181ea0
Author: Andrii Tkach <atk...@hortonworks.com>
Authored: Mon Oct 12 19:14:36 2015 +0300
Committer: Andrii Tkach <atk...@hortonworks.com>
Committed: Mon Oct 12 19:14:59 2015 +0300

----------------------------------------------------------------------
 ambari-web/app/utils/ajax/ajax.js       | 44 +++++++++++++++++++++++++++-
 ambari-web/test/utils/ajax/ajax_test.js | 33 +++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/a755a305/ambari-web/app/utils/ajax/ajax.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/utils/ajax/ajax.js 
b/ambari-web/app/utils/ajax/ajax.js
index edbc528..075eb9a 100644
--- a/ambari-web/app/utils/ajax/ajax.js
+++ b/ambari-web/app/utils/ajax/ajax.js
@@ -2689,7 +2689,8 @@ var formatRequest = function (data) {
     type: this.type || 'GET',
     timeout: App.timeout,
     dataType: 'json',
-    statusCode: require('data/statusCodes')
+    statusCode: require('data/statusCodes'),
+    headers: {}
   };
   if (App.get('testMode')) {
     opt.url = formatUrl(this.mock ? this.mock : '', data);
@@ -2707,12 +2708,39 @@ var formatRequest = function (data) {
 };
 
 /**
+ * transform GET to POST call
+ * @param {object} opt
+ * @returns {object} opt
+ */
+var doGetAsPost = function(opt) {
+  var delimiterPos = opt.url.indexOf('?');
+
+  opt.type = "POST";
+  opt.headers["X-Http-Method-Override"] = "GET";
+  if (delimiterPos !== -1) {
+    opt.data = JSON.stringify({
+      "RequestInfo": {"query" : opt.url.substr(delimiterPos + 1, 
opt.url.length)}
+    });
+    opt.url = opt.url.substr(0, delimiterPos);
+  }
+  opt.url += '?_=' + App.dateTime();
+  return opt;
+};
+
+/**
  * Wrapper for all ajax requests
  *
  * @type {Object}
  */
 var ajax = Em.Object.extend({
   /**
+   * max number of symbols in URL of GET call
+   * @const
+   * @type {number}
+   */
+  MAX_GET_URL_LENGTH: 2048,
+
+  /**
    * Send ajax request
    *
    * @param {Object} config
@@ -2751,6 +2779,10 @@ var ajax = Em.Object.extend({
     }
     opt = formatRequest.call(urls[config.name], params);
 
+    if (opt.url && opt.url.length > this.get('MAX_GET_URL_LENGTH')) {
+      opt = doGetAsPost(opt);
+    }
+
     opt.context = this;
 
     // object sender should be provided for processing beforeSend, success and 
error responses
@@ -2905,6 +2937,16 @@ if ($.mocho) {
      */
     fakeFormatRequest: function (urlObj, data) {
       return formatRequest.call(urlObj, data);
+    },
+
+    /**
+     * Don't use it anywhere except tests!
+     * @param urlObj
+     * @param data
+     * @returns {Object}
+     */
+    fakeDoGetAsPost: function (urlObj, data) {
+      return doGetAsPost.call(urlObj, data);
     }
   });
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/a755a305/ambari-web/test/utils/ajax/ajax_test.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/utils/ajax/ajax_test.js 
b/ambari-web/test/utils/ajax/ajax_test.js
index 1c5c5ab..5eaafc7 100644
--- a/ambari-web/test/utils/ajax/ajax_test.js
+++ b/ambari-web/test/utils/ajax/ajax_test.js
@@ -150,4 +150,37 @@ describe('App.ajax', function() {
     });
   });
 
+  describe("#doGetAsPost()", function () {
+    beforeEach(function () {
+      sinon.stub(App, 'dateTime').returns(1);
+    });
+    afterEach(function () {
+      App.dateTime.restore();
+    });
+    it("url does not have '?'", function () {
+      var opt = {
+        type: 'GET',
+        url: '',
+        headers: {}
+      };
+      expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
+        type: 'POST',
+        url: '?_=1',
+        headers: {"X-Http-Method-Override": "GET"}
+      });
+    });
+    it("url has '?'", function () {
+      var opt = {
+        type: 'GET',
+        url: 'root?params',
+        headers: {}
+      };
+      expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
+        type: 'POST',
+        url: 'root?_=1',
+        headers: {"X-Http-Method-Override": "GET"},
+        data: "{\"RequestInfo\":{\"query\":\"params\"}}"
+      });
+    });
+  });
 });

Reply via email to