test-xhr.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict'
  5. const { XMLHttpRequest } = require('sdk/net/xhr');
  6. const { LoaderWithHookedConsole } = require('sdk/test/loader');
  7. const { set: setPref } = require("sdk/preferences/service");
  8. const data = require("./fixtures");
  9. const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
  10. exports.testAPIExtension = function(assert) {
  11. let { loader, messages } = LoaderWithHookedConsole(module);
  12. let { XMLHttpRequest } = loader.require("sdk/net/xhr");
  13. setPref(DEPRECATE_PREF, true);
  14. let xhr = new XMLHttpRequest();
  15. assert.equal(typeof(xhr.forceAllowThirdPartyCookie), "function",
  16. "forceAllowThirdPartyCookie is defined");
  17. assert.equal(xhr.forceAllowThirdPartyCookie(), undefined,
  18. "function can be called");
  19. assert.ok(messages[0].msg.indexOf("`xhr.forceAllowThirdPartyCookie()` is deprecated") >= 0,
  20. "deprecation warning was dumped");
  21. assert.ok(xhr.mozBackgroundRequest, "is background request");
  22. loader.unload();
  23. };
  24. exports.testAbortedXhr = function(assert, done) {
  25. let req = new XMLHttpRequest();
  26. req.open('GET', data.url('testLocalXhr.json'));
  27. req.addEventListener("abort", function() {
  28. assert.pass("request was aborted");
  29. done();
  30. });
  31. req.send(null);
  32. req.abort();
  33. };
  34. exports.testLocalXhr = function(assert, done) {
  35. let req = new XMLHttpRequest();
  36. let ready = false;
  37. req.overrideMimeType('text/plain');
  38. req.open('GET', data.url('testLocalXhr.json'));
  39. req.onreadystatechange = function() {
  40. if (req.readyState == 4 && (req.status == 0 || req.status == 200)) {
  41. ready = true;
  42. assert.equal(req.responseText, '{}\n', 'XMLHttpRequest should get local files');
  43. }
  44. };
  45. req.addEventListener('load', function onload() {
  46. req.removeEventListener('load', onload);
  47. assert.pass('addEventListener for load event worked');
  48. assert.ok(ready, 'onreadystatechange listener worked');
  49. done();
  50. });
  51. req.send(null);
  52. };
  53. exports.testResponseHeaders = function(assert, done) {
  54. let req = new XMLHttpRequest();
  55. req.overrideMimeType('text/plain');
  56. req.open('GET', module.uri);
  57. req.onreadystatechange = function() {
  58. if (req.readyState == 4 && (req.status == 0 || req.status == 200)) {
  59. var headers = req.getAllResponseHeaders();
  60. headers = headers.split("\r\n");
  61. if (headers.length == 1) {
  62. headers = headers[0].split("\n");
  63. }
  64. for (let i in headers) {
  65. if (headers[i] && headers[i].search('Content-Type') >= 0) {
  66. assert.equal(headers[i], 'Content-Type: text/plain',
  67. 'XHR\'s headers are valid');
  68. }
  69. }
  70. done();
  71. }
  72. };
  73. req.send(null);
  74. }
  75. require('test').run(exports);