Browse Source

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

Andrii Tkach 10 years ago
parent
commit
99da5c68e7
2 changed files with 76 additions and 1 deletions
  1. 43 1
      ambari-web/app/utils/ajax/ajax.js
  2. 33 0
      ambari-web/test/utils/ajax/ajax_test.js

+ 43 - 1
ambari-web/app/utils/ajax/ajax.js

@@ -2642,7 +2642,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);
@@ -2659,12 +2660,39 @@ var formatRequest = function (data) {
   return opt;
 };
 
+/**
+ * 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
    *
@@ -2704,6 +2732,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
@@ -2852,6 +2884,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);
     }
   });
 }

+ 33 - 0
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\"}}"
+      });
+    });
+  });
 });