Repository: ambari Updated Branches: refs/heads/trunk 62b9f9551 -> 5601b1404
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/5601b140 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/5601b140 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/5601b140 Branch: refs/heads/trunk Commit: 5601b1404ee116e8994102525227817f88176706 Parents: 62b9f95 Author: Andrii Tkach <atk...@hortonworks.com> Authored: Mon Oct 12 19:17:40 2015 +0300 Committer: Andrii Tkach <atk...@hortonworks.com> Committed: Mon Oct 12 19:17:40 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/5601b140/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 9e5e8d2..70c6b10 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/5601b140/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\"}}" + }); + }); + }); });