ajax_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.set('apiPrefix', '/api/v1');
  23. App.set('clusterName', 'tdk');
  24. });
  25. describe('#send', function() {
  26. beforeEach(function() {
  27. sinon.spy($, 'ajax');
  28. });
  29. afterEach(function() {
  30. $.ajax.restore();
  31. });
  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. beforeEach(function () {
  100. sinon.stub(App, 'get').withArgs('testMode').returns(false);
  101. });
  102. afterEach(function () {
  103. App.get.restore();
  104. });
  105. var tests = [
  106. {
  107. urlObj: {
  108. real: '/real_url',
  109. format: function() {
  110. return {
  111. type: 'PUT'
  112. }
  113. }
  114. },
  115. data: {},
  116. m: '',
  117. e: {type: 'PUT', url: '/api/v1/real_url'}
  118. }
  119. ];
  120. tests.forEach(function(test) {
  121. it(test.m, function() {
  122. var r = App.ajax.fakeFormatRequest(test.urlObj, test.data);
  123. expect(r.type).to.equal(test.e.type);
  124. expect(r.url).to.equal(test.e.url);
  125. });
  126. });
  127. });
  128. describe("#doGetAsPost()", function () {
  129. beforeEach(function () {
  130. sinon.stub(App, 'dateTime').returns(1);
  131. });
  132. afterEach(function () {
  133. App.dateTime.restore();
  134. });
  135. it("url does not have '?'", function () {
  136. var opt = {
  137. type: 'GET',
  138. url: '',
  139. headers: {}
  140. };
  141. expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
  142. type: 'POST',
  143. url: '?_=1',
  144. headers: {"X-Http-Method-Override": "GET"}
  145. });
  146. });
  147. it("url has '?'", function () {
  148. var opt = {
  149. type: 'GET',
  150. url: 'root?params',
  151. headers: {}
  152. };
  153. expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
  154. type: 'POST',
  155. url: 'root?_=1',
  156. headers: {"X-Http-Method-Override": "GET"},
  157. data: "{\"RequestInfo\":{\"query\":\"params\"}}"
  158. });
  159. });
  160. });
  161. });