ajax_test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.spy(App.ajax, 'send'); // no sense to test stubbed function, so going to spy on it
  24. App.set('apiPrefix', '/api/v1');
  25. App.set('clusterName', 'tdk');
  26. });
  27. describe('#send', function() {
  28. it('Without sender', function() {
  29. expect(App.ajax.send({})).to.equal(null);
  30. expect($.ajax.called).to.equal(false);
  31. });
  32. it('Invalid config.name', function() {
  33. expect(App.ajax.send({name:'fake_name', sender: this})).to.equal(null);
  34. expect($.ajax.called).to.equal(false);
  35. });
  36. it('With proper data', function() {
  37. App.ajax.send({name: 'router.logoff', sender: this});
  38. expect($.ajax.calledOnce).to.equal(true);
  39. });
  40. });
  41. describe('#formatUrl', function() {
  42. var tests = [
  43. {
  44. url: null,
  45. data: {},
  46. e: null,
  47. m: 'url is null'
  48. },
  49. {
  50. url: 'site/{param}',
  51. data: null,
  52. e: 'site/',
  53. m: 'url with one param, but data is null'
  54. },
  55. {
  56. url: 'clean_url',
  57. data: {},
  58. e: 'clean_url',
  59. m: 'url without placeholders'
  60. },
  61. {
  62. url: 'site/{param}',
  63. data: {},
  64. e: 'site/',
  65. m: 'url with param, but there is no such param in the data'
  66. },
  67. {
  68. url: 'site/{param}/{param}',
  69. data: {param: 123},
  70. e: 'site/123/123',
  71. m: 'url with param which appears two times'
  72. }
  73. ];
  74. tests.forEach(function(test) {
  75. it(test.m, function() {
  76. var r = App.ajax.fakeFormatUrl(test.url, test.data);
  77. expect(r).to.equal(test.e);
  78. });
  79. });
  80. });
  81. describe('Check "real" property for each url object', function() {
  82. var names = App.ajax.fakeGetUrlNames();
  83. names.forEach(function(name) {
  84. it('`' + name + '`', function() {
  85. var url = App.ajax.fakeGetUrl(name);
  86. expect(url.real).to.be.a('string');
  87. });
  88. it('`' + name + '` should not contain spaces', function () {
  89. var url = App.ajax.fakeGetUrl(name);
  90. expect(url.real.contains(' ')).to.be.false;
  91. });
  92. });
  93. });
  94. describe('#formatRequest', function() {
  95. var tests = [
  96. {
  97. urlObj: {
  98. real: '/real_url',
  99. format: function() {
  100. return {
  101. type: 'PUT'
  102. }
  103. }
  104. },
  105. data: {},
  106. m: '',
  107. e: {type: 'PUT', url: '/api/v1/real_url'}
  108. }
  109. ];
  110. tests.forEach(function(test) {
  111. it(test.m, function() {
  112. var r = App.ajax.fakeFormatRequest(test.urlObj, test.data);
  113. expect(r.type).to.equal(test.e.type);
  114. expect(r.url).to.equal(test.e.url);
  115. });
  116. });
  117. });
  118. describe("#doGetAsPost()", function () {
  119. beforeEach(function () {
  120. sinon.stub(App, 'dateTime').returns(1);
  121. });
  122. afterEach(function () {
  123. App.dateTime.restore();
  124. });
  125. it("url does not have '?'", function () {
  126. var opt = {
  127. type: 'GET',
  128. url: '',
  129. headers: {}
  130. };
  131. expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
  132. type: 'POST',
  133. url: '?_=1',
  134. headers: {"X-Http-Method-Override": "GET"}
  135. });
  136. });
  137. it("url has '?'", function () {
  138. var opt = {
  139. type: 'GET',
  140. url: 'root?params',
  141. headers: {}
  142. };
  143. expect(App.ajax.fakeDoGetAsPost({}, opt)).to.eql({
  144. type: 'POST',
  145. url: 'root?_=1',
  146. headers: {"X-Http-Method-Override": "GET"},
  147. data: "{\"RequestInfo\":{\"query\":\"params\"}}"
  148. });
  149. });
  150. });
  151. });