2013-02-01 13:17:34 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2013-04-26 02:04:23 -07:00
|
|
|
'use strict'
|
2013-02-01 13:17:34 -08:00
|
|
|
|
2013-06-07 09:43:59 -07:00
|
|
|
const { XMLHttpRequest } = require('sdk/net/xhr');
|
|
|
|
const { LoaderWithHookedConsole } = require('sdk/test/loader');
|
2013-09-06 11:09:20 -07:00
|
|
|
const { set: setPref } = require("sdk/preferences/service");
|
2013-11-05 13:51:58 -08:00
|
|
|
const data = require("./fixtures");
|
|
|
|
|
2013-09-06 11:09:20 -07:00
|
|
|
const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
|
2013-06-07 09:43:59 -07:00
|
|
|
|
|
|
|
exports.testAPIExtension = function(assert) {
|
|
|
|
let { loader, messages } = LoaderWithHookedConsole(module);
|
|
|
|
let { XMLHttpRequest } = loader.require("sdk/net/xhr");
|
2013-09-06 11:09:20 -07:00
|
|
|
setPref(DEPRECATE_PREF, true);
|
2013-06-07 09:43:59 -07:00
|
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
assert.equal(typeof(xhr.forceAllowThirdPartyCookie), "function",
|
|
|
|
"forceAllowThirdPartyCookie is defined");
|
|
|
|
assert.equal(xhr.forceAllowThirdPartyCookie(), undefined,
|
|
|
|
"function can be called");
|
|
|
|
|
|
|
|
assert.ok(messages[0].msg.indexOf("`xhr.forceAllowThirdPartyCookie()` is deprecated") >= 0,
|
|
|
|
"deprecation warning was dumped");
|
|
|
|
assert.ok(xhr.mozBackgroundRequest, "is background request");
|
|
|
|
|
|
|
|
loader.unload();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.testAbortedXhr = function(assert, done) {
|
|
|
|
let req = new XMLHttpRequest();
|
|
|
|
req.open('GET', data.url('testLocalXhr.json'));
|
|
|
|
req.addEventListener("abort", function() {
|
|
|
|
assert.pass("request was aborted");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
req.send(null);
|
2013-02-01 13:17:34 -08:00
|
|
|
req.abort();
|
|
|
|
};
|
|
|
|
|
2013-04-26 02:04:23 -07:00
|
|
|
exports.testLocalXhr = function(assert, done) {
|
2013-06-07 09:43:59 -07:00
|
|
|
let req = new XMLHttpRequest();
|
2013-04-26 02:04:23 -07:00
|
|
|
let ready = false;
|
|
|
|
|
|
|
|
req.overrideMimeType('text/plain');
|
2013-05-07 19:30:52 -07:00
|
|
|
req.open('GET', data.url('testLocalXhr.json'));
|
2013-02-01 13:17:34 -08:00
|
|
|
req.onreadystatechange = function() {
|
|
|
|
if (req.readyState == 4 && (req.status == 0 || req.status == 200)) {
|
2013-04-26 02:04:23 -07:00
|
|
|
ready = true;
|
2013-05-07 19:30:52 -07:00
|
|
|
assert.equal(req.responseText, '{}\n', 'XMLHttpRequest should get local files');
|
2013-02-01 13:17:34 -08:00
|
|
|
}
|
|
|
|
};
|
2013-04-26 02:04:23 -07:00
|
|
|
req.addEventListener('load', function onload() {
|
|
|
|
req.removeEventListener('load', onload);
|
|
|
|
assert.pass('addEventListener for load event worked');
|
|
|
|
assert.ok(ready, 'onreadystatechange listener worked');
|
|
|
|
done();
|
|
|
|
});
|
2013-02-01 13:17:34 -08:00
|
|
|
req.send(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-26 02:04:23 -07:00
|
|
|
exports.testResponseHeaders = function(assert, done) {
|
2013-06-07 09:43:59 -07:00
|
|
|
let req = new XMLHttpRequest();
|
2013-04-26 02:04:23 -07:00
|
|
|
|
|
|
|
req.overrideMimeType('text/plain');
|
|
|
|
req.open('GET', module.uri);
|
2013-02-01 13:17:34 -08:00
|
|
|
req.onreadystatechange = function() {
|
|
|
|
if (req.readyState == 4 && (req.status == 0 || req.status == 200)) {
|
|
|
|
var headers = req.getAllResponseHeaders();
|
2013-06-07 09:43:59 -07:00
|
|
|
headers = headers.split("\r\n");
|
|
|
|
if (headers.length == 1) {
|
|
|
|
headers = headers[0].split("\n");
|
2013-02-01 13:17:34 -08:00
|
|
|
}
|
2013-06-07 09:43:59 -07:00
|
|
|
for (let i in headers) {
|
|
|
|
if (headers[i] && headers[i].search('Content-Type') >= 0) {
|
|
|
|
assert.equal(headers[i], 'Content-Type: text/plain',
|
|
|
|
'XHR\'s headers are valid');
|
|
|
|
}
|
2013-02-01 13:17:34 -08:00
|
|
|
}
|
2013-04-26 02:04:23 -07:00
|
|
|
|
|
|
|
done();
|
2013-02-01 13:17:34 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send(null);
|
|
|
|
}
|
|
|
|
|
2013-04-26 02:04:23 -07:00
|
|
|
require('test').run(exports);
|