addon-sdk/source/test/test-xhr.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial