ajax_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. require('utils/ajax/ajax');
  20. describe('App.ajax', function() {
  21. beforeEach(function() {
  22. App.ajax.send.restore();
  23. sinon.stub(App.logger, 'setTimer');
  24. sinon.spy(App.ajax, 'send'); // no sense to test stubbed function, so going to spy on it
  25. App.set('apiPrefix', '/api/v1');
  26. App.set('clusterName', 'tdk');
  27. });
  28. afterEach(function() {
  29. App.logger.setTimer.restore();
  30. });
  31. describe('#send', function() {
  32. it('Without sender', function() {
  33. expect(App.ajax.send({})).to.equal(null);
  34. expect($.ajax.called).to.equal(false);
  35. });
  36. it('Invalid config.name', function() {
  37. expect(App.ajax.send({name:'fake_name', sender: this})).to.equal(null);
  38. expect($.ajax.called).to.equal(false);
  39. });
  40. it('With proper data', function() {
  41. App.ajax.send({name: 'router.logoff', sender: this});
  42. expect($.ajax.calledOnce).to.equal(true);
  43. });
  44. });
  45. describe('#formatUrl', function() {
  46. var tests = [
  47. {
  48. url: null,
  49. data: {},
  50. e: null,
  51. m: 'url is null'
  52. },
  53. {
  54. url: 'site/{param}',
  55. data: null,
  56. e: 'site/',
  57. m: 'url with one param, but data is null'
  58. },
  59. {
  60. url: 'clean_url',
  61. data: {},
  62. e: 'clean_url',
  63. m: 'url without placeholders'
  64. },
  65. {
  66. url: 'site/{param}',
  67. data: {},
  68. e: 'site/',
  69. m: 'url with param, but there is no such param in the data'
  70. },
  71. {
  72. url: 'site/{param}/{param}',
  73. data: {param: 123},
  74. e: 'site/123/123',
  75. m: 'url with param which appears two times'
  76. }
  77. ];
  78. tests.forEach(function(test) {
  79. it(test.m, function() {
  80. var r = App.ajax.fakeFormatUrl(test.url, test.data);
  81. expect(r).to.equal(test.e);
  82. });
  83. });
  84. });
  85. describe('Check "real" property for each url object', function() {
  86. var names = App.ajax.fakeGetUrlNames();
  87. names.forEach(function(name) {
  88. it('`' + name + '`', function() {
  89. var url = App.ajax.fakeGetUrl(name);
  90. expect(url.real).to.be.a('string');
  91. });
  92. it('`' + name + '` should not contain spaces', function () {
  93. var url = App.ajax.fakeGetUrl(name);
  94. expect(url.real.contains(' ')).to.be.false;
  95. });
  96. });
  97. });
  98. describe('#formatRequest', function() {
  99. var tests = [
  100. {
  101. urlObj: {
  102. real: '/real_url',
  103. format: function() {
  104. return {
  105. type: 'PUT'
  106. }
  107. }
  108. },
  109. data: {},
  110. m: '',
  111. e: {type: 'PUT', url: '/api/v1/real_url'}
  112. }
  113. ];
  114. tests.forEach(function(test) {
  115. it(test.m, function() {
  116. var r = App.ajax.fakeFormatRequest(test.urlObj, test.data);
  117. expect(r.type).to.equal(test.e.type);
  118. expect(r.url).to.equal(test.e.url);
  119. });
  120. });
  121. });
  122. describe("#doGetAsPost()", function () {
  123. beforeEach(function () {
  124. sinon.stub(App, 'dateTime').returns(1);
  125. });
  126. afterEach(function () {
  127. App.dateTime.restore();
  128. });
  129. it("url does not have '?'", function () {
  130. var opt = {
  131. type: 'GET',
  132. url: '',
  133. headers: {}
  134. };
  135. expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
  136. type: 'POST',
  137. url: '?_=1',
  138. headers: {"X-Http-Method-Override": "GET"}
  139. });
  140. });
  141. it("url has '?'", function () {
  142. var opt = {
  143. type: 'GET',
  144. url: 'root?params',
  145. headers: {}
  146. };
  147. expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
  148. type: 'POST',
  149. url: 'root?_=1',
  150. headers: {"X-Http-Method-Override": "GET"},
  151. data: "{\"RequestInfo\":{\"query\":\"params\"}}"
  152. });
  153. });
  154. });
  155. describe('#abortRequests', function () {
  156. var xhr = {
  157. abort: Em.K
  158. },
  159. requests;
  160. beforeEach(function () {
  161. sinon.spy(xhr, 'abort');
  162. xhr.isForcedAbort = false;
  163. requests = [xhr, xhr];
  164. App.ajax.abortRequests(requests);
  165. });
  166. afterEach(function () {
  167. xhr.abort.restore();
  168. });
  169. it('should abort all requests', function () {
  170. expect(xhr.abort.calledTwice).to.be.true;
  171. });
  172. it('should mark request as aborted', function () {
  173. expect(xhr.isForcedAbort).to.be.true;
  174. });
  175. it('should clear requests array', function () {
  176. expect(requests).to.have.length(0);
  177. });
  178. });
  179. });