ajax.test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. require("./chai.helper");
  2. var domHelper = require("./dom.helper");
  3. var HttpFake = require("./http-fake.helper");
  4. var XMLHttpRequest = require("./http-fake-xmlhttprequest.helper");
  5. // NB sinon has a fake XMLHTTPRequest object, but it doesn't
  6. // work properly with node.js;
  7. // see https://groups.google.com/forum/#!topic/sinonjs/yD8Q9OjtFvc
  8. describe("ajax", function () {
  9. var originalXmlHttpRequest;
  10. var server = HttpFake.createServer();
  11. // we need to refer to this from the tests, but we allow
  12. // the server to randomly get a port before setting it
  13. var host;
  14. before(function (done) {
  15. domHelper(
  16. "<div id=\"foo\">" +
  17. "</div>"
  18. );
  19. // replace jsdom's XMLHttpRequest with
  20. // node-xmlhttprequest, to support POST requests
  21. originalXmlHttpRequest = window.XMLHttpRequest;
  22. window.XMLHttpRequest = XMLHttpRequest;
  23. server.start(function () {
  24. host = "localhost:" + server.port;
  25. done.apply(null, arguments);
  26. });
  27. });
  28. afterEach(function () {
  29. server.clearFakes();
  30. });
  31. after(function (done) {
  32. window.XMLHttpRequest = originalXmlHttpRequest;
  33. server.stop(done);
  34. });
  35. it("should do a GET request to a specified url", function (done) {
  36. // this is what we expect the request to look like
  37. var requestMatcher = {
  38. method: "GET",
  39. path: "/",
  40. query: {a: "1", b: "2"},
  41. host: "localhost",
  42. headers: {
  43. "x-bloop": "glong"
  44. }
  45. };
  46. // this is what we send back if we get the expected request
  47. var response = { data: "hello world" };
  48. server.registerFake(response, requestMatcher);
  49. $.ajax({
  50. type: "GET",
  51. url: "http://" + host + "/",
  52. data: "a=1&b=2",
  53. dataType: "text",
  54. headers: {
  55. "X-bloop": "glong"
  56. },
  57. success: function (data) {
  58. data.should.equal("hello world");
  59. done();
  60. },
  61. error: function (xhr) {
  62. done(new Error(xhr.responseText));
  63. }
  64. });
  65. });
  66. it("should do a POST request to a specified url", function (done) {
  67. var postData = {a: 1, b: 2};
  68. // this is what we expect the request to look like
  69. var requestMatcher = {
  70. method: "POST",
  71. path: "/save",
  72. host: "localhost",
  73. headers: {
  74. "content-type": "application/x-www-form-urlencoded"
  75. },
  76. body: { a: "1", b: "2" }
  77. };
  78. // this is what we send back if we get the expected request
  79. var response = { data: "OK" };
  80. server.registerFake(response, requestMatcher);
  81. $.ajax({
  82. type: "POST",
  83. url: "http://" + host + "/save",
  84. contentType: "application/x-www-form-urlencoded",
  85. data: postData,
  86. success: function (responseText) {
  87. responseText.should.equal("OK");
  88. done();
  89. },
  90. error: function (xhr) {
  91. done(new Error(xhr.responseText));
  92. }
  93. });
  94. });
  95. it("should do a GET request for XML to a specified url", function (done) {
  96. var requestMatcher = {
  97. method: "GET",
  98. path: "/",
  99. query: { x: "10", y: "20", z: "30" },
  100. host: "localhost"
  101. };
  102. var xml = "<?xml version=\"1.0\"><top/>";
  103. var response = { data: xml };
  104. server.registerFake(response, requestMatcher);
  105. $.ajax({
  106. type: "GET",
  107. url: "http://" + host + "/?x=10",
  108. data: "y=20&z=30",
  109. dataType: "xml",
  110. success: function (data) {
  111. data.should.equal(xml);
  112. done();
  113. },
  114. error: function (xhr) {
  115. done(new Error(xhr.responseXML));
  116. }
  117. });
  118. });
  119. it("should do a json request and parse the response", function (done) {
  120. var requestMatcher = {
  121. method: "GET",
  122. path: "/",
  123. host: "localhost"
  124. };
  125. var responseData = {hi: "world", nice: "to see you"};
  126. var response = { data: JSON.stringify(responseData) };
  127. server.registerFake(response, requestMatcher);
  128. $.ajax({
  129. type: "GET",
  130. url: "http://" + host + "/",
  131. dataType: "json",
  132. success: function (data) {
  133. data.should.eql(responseData);
  134. done();
  135. },
  136. error: function (xhr) {
  137. done(new Error(xhr.responseText));
  138. }
  139. });
  140. });
  141. it("should call error callback if request fails", function (done) {
  142. var requestMatcher = {
  143. method: "GET",
  144. path: "/",
  145. host: "localhost"
  146. };
  147. var response = { status: 503 };
  148. server.registerFake(response, requestMatcher);
  149. $.ajax({
  150. type: "GET",
  151. url: "http://" + host + "/",
  152. success: function (data) {
  153. done(new Error("success cb should not be called"));
  154. },
  155. error: function (xhr, message) {
  156. xhr.status.should.equal(503);
  157. message.should.equal("error");
  158. done();
  159. }
  160. });
  161. });
  162. it("should call error callback if request times out", function (done) {
  163. var requestMatcher = {
  164. method: "GET",
  165. path: "/",
  166. host: "localhost"
  167. };
  168. // response should still error because it times out,
  169. // even though the status code is 200
  170. var response = { status: 200, pause: 2000, data: "OK" };
  171. server.registerFake(response, requestMatcher);
  172. $.ajax({
  173. type: "GET",
  174. url: "http://" + host + "/",
  175. timeout: 100,
  176. success: function (data) {
  177. done(new Error("success cb should not be called"));
  178. },
  179. error: function (xhr, message) {
  180. message.should.equal("timeout");
  181. done();
  182. }
  183. });
  184. });
  185. });