PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/test/Backbone.CrossDomain.js

https://github.com/nethermead/Backbone.CrossDomain
JavaScript | 256 lines | 209 code | 39 blank | 8 comment | 2 complexity | 61a8e280b1a101701cddc875d61711f1 MD5 | raw file
Possible License(s): MIT
  1. $(document).ready(function() {
  2. var Library = Backbone.Collection.extend({
  3. url : function() { return '/library'; }
  4. });
  5. var library;
  6. var attrs = {
  7. title : "The Tempest",
  8. author : "Bill Shakespeare",
  9. length : 123
  10. };
  11. module('Backbone.CrossDomain', _.extend(new Environment, {
  12. setup : function() {
  13. Environment.prototype.setup.apply(this, arguments);
  14. library = new Library;
  15. library.create(attrs, {wait: false});
  16. },
  17. teardown: function() {
  18. Environment.prototype.teardown.apply(this, arguments);
  19. Backbone.emulateHTTP = false;
  20. }
  21. }));
  22. test("initialize", function() {
  23. var xdm = new Backbone.Model();
  24. ok(xdm instanceof Backbone.Model, 'Backbone.Model created');
  25. });
  26. test("urlError", 2, function() {
  27. var model = new Backbone.Model();
  28. raises(function() {
  29. model.fetch();
  30. });
  31. model.fetch({url: '/one/two'});
  32. equal(this.ajaxSettings.url, '/one/two');
  33. });
  34. test("#1052 - `options` is optional.", 0, function() {
  35. var model = new Backbone.Model();
  36. model.url = '/test';
  37. Backbone.sync('create', model);
  38. });
  39. test("Backbone.ajax", 1, function() {
  40. Backbone.ajax = function(settings){
  41. strictEqual(settings.url, '/test');
  42. };
  43. var model = new Backbone.Model();
  44. model.url = '/test';
  45. Backbone.sync('create', model);
  46. });
  47. test("Call provided error callback on error.", 1, function() {
  48. var model = new Backbone.Model;
  49. model.url = '/test';
  50. Backbone.sync('read', model, {
  51. error: function() { ok(true); }
  52. });
  53. this.ajaxSettings.error();
  54. });
  55. test('Use Backbone.emulateHTTP as default.', 2, function() {
  56. var model = new Backbone.Model;
  57. model.url = '/test';
  58. Backbone.emulateHTTP = true;
  59. model.sync('create', model);
  60. strictEqual(this.ajaxSettings.emulateHTTP, true);
  61. Backbone.emulateHTTP = false;
  62. model.sync('create', model);
  63. strictEqual(this.ajaxSettings.emulateHTTP, false);
  64. });
  65. test('Use Backbone.emulateJSON as default.', 2, function() {
  66. var model = new Backbone.Model;
  67. model.url = '/test';
  68. Backbone.emulateJSON = true;
  69. model.sync('create', model);
  70. strictEqual(this.ajaxSettings.emulateJSON, true);
  71. Backbone.emulateJSON = false;
  72. model.sync('create', model);
  73. strictEqual(this.ajaxSettings.emulateJSON, false);
  74. });
  75. // Perform these tests only for IE
  76. if (window.XDomainRequest) {
  77. // Make sure non-cross domain requests work fine and as normal
  78. test("Backbone.ajax", 1, function() {
  79. Backbone.ajax = function(settings){
  80. strictEqual(settings.url, '/test');
  81. };
  82. var model = new Backbone.Model();
  83. model.url = '/test';
  84. Backbone.sync('create', model);
  85. });
  86. test("Call provided error callback on error.", 1, function() {
  87. var model = new Backbone.Model;
  88. model.url = '/test';
  89. Backbone.sync('read', model, {
  90. error: function() { ok(true); }
  91. });
  92. this.ajaxSettings.error();
  93. });
  94. test('Use Backbone.emulateHTTP as default.', 2, function() {
  95. var model = new Backbone.Model;
  96. model.url = '/test';
  97. Backbone.emulateHTTP = true;
  98. model.sync('create', model);
  99. strictEqual(this.ajaxSettings.emulateHTTP, true);
  100. Backbone.emulateHTTP = false;
  101. model.sync('create', model);
  102. strictEqual(this.ajaxSettings.emulateHTTP, false);
  103. });
  104. test('Use Backbone.emulateJSON as default.', 2, function() {
  105. var model = new Backbone.Model;
  106. model.url = '/test';
  107. Backbone.emulateJSON = true;
  108. model.sync('create', model);
  109. strictEqual(this.ajaxSettings.emulateJSON, true);
  110. Backbone.emulateJSON = false;
  111. model.sync('create', model);
  112. strictEqual(this.ajaxSettings.emulateJSON, false);
  113. });
  114. test("#1756 - Call user provided beforeSend function.", 4, function() {
  115. Backbone.emulateHTTP = true;
  116. var model = new Backbone.Model;
  117. model.url = '/test';
  118. var xhr = {
  119. setRequestHeader: function(header, value) {
  120. strictEqual(header, 'X-HTTP-Method-Override');
  121. strictEqual(value, 'DELETE');
  122. }
  123. };
  124. model.sync('delete', model, {
  125. beforeSend: function(_xhr) {
  126. ok(_xhr === xhr);
  127. return false;
  128. }
  129. });
  130. strictEqual(this.ajaxSettings.beforeSend(xhr), false);
  131. });
  132. // Make sure cross domain requests to DELETE, PATCH, and PUT fail with emulateHTTP off
  133. test("Try Forbidden requests with emulateHTTP on.", 3, function() {
  134. Backbone.emulateHTTP = true;
  135. var model = new Backbone.Model;
  136. model.url = 'http://example.com/test';
  137. model.sync('delete', model);
  138. strictEqual(this.ajaxSettings.emulateHTTP,
  139. true,
  140. "CrossDomain Sync appropriately allowed DELETE with emulateHTTP on");
  141. model.sync('patch', model);
  142. strictEqual(this.ajaxSettings.emulateHTTP,
  143. true,
  144. "CrossDomain Sync appropriately allowed PATCH with emulateHTTP on");
  145. model.sync('update', model);
  146. strictEqual(this.ajaxSettings.emulateHTTP,
  147. true,
  148. "CrossDomain Sync appropriately allowed PUT with emulateHTTP on");
  149. });
  150. // Make sure cross domain requests to DELETE, PATCH, and PUT work with emulateHTTP on
  151. test("Try Forbidden requests with emulateHTTP off.", 3, function() {
  152. Backbone.emulateHTTP = false;
  153. var model = new Backbone.Model;
  154. model.url = 'http://example.com/test';
  155. try {
  156. // This should fail and throw an exception.
  157. model.sync('delete', model);
  158. } catch (x) {
  159. strictEqual(x.message,
  160. "Backbone.CrossDomain cannot use PUT, PATCH, DELETE with XDomainRequest (IE) and emulateHTTP=false",
  161. "CrossDomain Sync appropriately denied DELETE request with emulateHTTP off on IE.");
  162. }
  163. try {
  164. // This should fail and throw an exception.
  165. model.sync('patch', model);
  166. } catch (x) {
  167. strictEqual(x.message,
  168. "Backbone.CrossDomain cannot use PUT, PATCH, DELETE with XDomainRequest (IE) and emulateHTTP=false",
  169. "CrossDomain Sync appropriately denied PATCH request with emulateHTTP off on IE.");
  170. }
  171. try {
  172. // This should fail and throw an exception.
  173. model.sync('update', model);
  174. } catch (x) {
  175. strictEqual(x.message,
  176. "Backbone.CrossDomain cannot use PUT, PATCH, DELETE with XDomainRequest (IE) and emulateHTTP=false",
  177. "CrossDomain Sync appropriately denied PUT request with emulateHTTP off on IE.");
  178. }
  179. });
  180. test("Test out different combos of protocols", 2, function() {
  181. Backbone.emulateHTTP = false;
  182. var model = new Backbone.Model;
  183. model.url = 'https://example.com/test';
  184. try {
  185. // This should fail and throw an exception.
  186. model.sync('read', model);
  187. } catch (x) {
  188. strictEqual(x.message,
  189. "Backbone.CrossDomain only works for same protocol requests (HTTP -> HTTP, HTTPS -> HTTPS) cannot mix.",
  190. "CrossDomain Sync appropriately threw error when met with mixed protocols on IE.");
  191. }
  192. model.url = '//example.com/test';
  193. model.sync('read', model);
  194. ok(model, "Model read worked without a protocol specified and no errors thrown.");
  195. });
  196. }
  197. else {
  198. test("#1756 - Call user provided beforeSend function.", 4, function() {
  199. Backbone.emulateHTTP = true;
  200. var model = new Backbone.Model;
  201. model.url = '/test';
  202. var xhr = {
  203. setRequestHeader: function(header, value) {
  204. strictEqual(header, 'X-HTTP-Method-Override');
  205. strictEqual(value, 'DELETE');
  206. }
  207. };
  208. model.sync('delete', model, {
  209. beforeSend: function(_xhr) {
  210. ok(_xhr === xhr);
  211. return false;
  212. }
  213. });
  214. strictEqual(this.ajaxSettings.beforeSend(xhr), false);
  215. });
  216. }
  217. });