| Index: remoting/webapp/crd/js/xhr_unittest.js
|
| diff --git a/remoting/webapp/crd/js/xhr_unittest.js b/remoting/webapp/crd/js/xhr_unittest.js
|
| index 7102c2e1ca6cd14ed390cb7997dcbc8f67087d40..b02987e13aac18f83e0003a869906402856e1305 100644
|
| --- a/remoting/webapp/crd/js/xhr_unittest.js
|
| +++ b/remoting/webapp/crd/js/xhr_unittest.js
|
| @@ -4,193 +4,185 @@
|
|
|
| /**
|
| * @fileoverview
|
| - * @suppress {checkTypes|checkVars|reportUnknownTypes}
|
| */
|
|
|
| (function() {
|
|
|
| 'use strict';
|
| -QUnit.module('xhr');
|
| +
|
| +/** @type {sinon.FakeXhr} */
|
| +var fakeXhr;
|
| +
|
| +QUnit.module('xhr', {
|
| + beforeEach: function() {
|
| + fakeXhr = null;
|
| + sinon.useFakeXMLHttpRequest().onCreate =
|
| + function(/** sinon.FakeXhr */ xhr) {
|
| + fakeXhr = xhr;
|
| + };
|
| + }
|
| +});
|
|
|
| QUnit.test('urlencodeParamHash', function(assert) {
|
| assert.equal(
|
| - remoting.xhr.urlencodeParamHash({}),
|
| + remoting.Xhr.urlencodeParamHash({}),
|
| '');
|
| assert.equal(
|
| - remoting.xhr.urlencodeParamHash({'key': 'value'}),
|
| + remoting.Xhr.urlencodeParamHash({'key': 'value'}),
|
| 'key=value');
|
| assert.equal(
|
| - remoting.xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}),
|
| + remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}),
|
| 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26');
|
| assert.equal(
|
| - remoting.xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}),
|
| + remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}),
|
| 'k1=v1&k2=v2');
|
| });
|
|
|
| QUnit.test('basic GET', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| - var done = assert.async();
|
| - var request = remoting.xhr.start({
|
| + var promise = new remoting.Xhr({
|
| method: 'GET',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - }
|
| + responseType: remoting.Xhr.ResponseType.TEXT
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| });
|
| - assert.equal(request.method, 'GET');
|
| - assert.equal(request.url, 'http://foo.com');
|
| - assert.equal(request.withCredentials, false);
|
| - assert.equal(request.requestBody, null);
|
| - assert.ok(!('Content-type' in request.requestHeaders));
|
| - request.respond(200, {}, 'body');
|
| + assert.equal(fakeXhr.method, 'GET');
|
| + assert.equal(fakeXhr.url, 'http://foo.com');
|
| + assert.equal(fakeXhr.withCredentials, false);
|
| + assert.equal(fakeXhr.requestBody, null);
|
| + assert.ok(!('Content-type' in fakeXhr.requestHeaders));
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
| QUnit.test('GET with param string', function(assert) {
|
| - var done = assert.async();
|
| - sinon.useFakeXMLHttpRequest();
|
| - var request = remoting.xhr.start({
|
| + var promise = new remoting.Xhr({
|
| method: 'GET',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - },
|
| + responseType: remoting.Xhr.ResponseType.TEXT,
|
| urlParams: 'the_param_string'
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| });
|
| - assert.equal(request.method, 'GET');
|
| - assert.equal(request.url, 'http://foo.com?the_param_string');
|
| - assert.equal(request.withCredentials, false);
|
| - assert.equal(request.requestBody, null);
|
| - assert.ok(!('Content-type' in request.requestHeaders));
|
| - request.respond(200, {}, 'body');
|
| + assert.equal(fakeXhr.method, 'GET');
|
| + assert.equal(fakeXhr.url, 'http://foo.com?the_param_string');
|
| + assert.equal(fakeXhr.withCredentials, false);
|
| + assert.equal(fakeXhr.requestBody, null);
|
| + assert.ok(!('Content-type' in fakeXhr.requestHeaders));
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
| QUnit.test('GET with param object', function(assert) {
|
| - var done = assert.async();
|
| - sinon.useFakeXMLHttpRequest();
|
| - var request = remoting.xhr.start({
|
| + var promise = new remoting.Xhr({
|
| method: 'GET',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - },
|
| + responseType: remoting.Xhr.ResponseType.TEXT,
|
| urlParams: {'a': 'b', 'c': 'd'}
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| });
|
| - assert.equal(request.method, 'GET');
|
| - assert.equal(request.url, 'http://foo.com?a=b&c=d');
|
| - assert.equal(request.withCredentials, false);
|
| - assert.equal(request.requestBody, null);
|
| - assert.ok(!('Content-type' in request.requestHeaders));
|
| - request.respond(200, {}, 'body');
|
| + assert.equal(fakeXhr.method, 'GET');
|
| + assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d');
|
| + assert.equal(fakeXhr.withCredentials, false);
|
| + assert.equal(fakeXhr.requestBody, null);
|
| + assert.ok(!('Content-type' in fakeXhr.requestHeaders));
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
| QUnit.test('GET with headers', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| - var done = assert.async();
|
| - var request = remoting.xhr.start({
|
| + var promise = new remoting.Xhr({
|
| method: 'GET',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - },
|
| + responseType: remoting.Xhr.ResponseType.TEXT,
|
| headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'}
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| });
|
| - assert.equal(request.method, 'GET');
|
| - assert.equal(request.url, 'http://foo.com');
|
| - assert.equal(request.withCredentials, false);
|
| - assert.equal(request.requestBody, null);
|
| + assert.equal(fakeXhr.method, 'GET');
|
| + assert.equal(fakeXhr.url, 'http://foo.com');
|
| + assert.equal(fakeXhr.withCredentials, false);
|
| + assert.equal(fakeXhr.requestBody, null);
|
| assert.equal(
|
| - request.requestHeaders['Header1'],
|
| + fakeXhr.requestHeaders['Header1'],
|
| 'headerValue1');
|
| assert.equal(
|
| - request.requestHeaders['Header2'],
|
| + fakeXhr.requestHeaders['Header2'],
|
| 'headerValue2');
|
| - assert.ok(!('Content-type' in request.requestHeaders));
|
| - request.respond(200, {}, 'body');
|
| + assert.ok(!('Content-type' in fakeXhr.requestHeaders));
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
|
|
| QUnit.test('GET with credentials', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| - var done = assert.async();
|
| - var request = remoting.xhr.start({
|
| + var promise = new remoting.Xhr({
|
| method: 'GET',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - },
|
| + responseType: remoting.Xhr.ResponseType.TEXT,
|
| withCredentials: true
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| });
|
| - assert.equal(request.method, 'GET');
|
| - assert.equal(request.url, 'http://foo.com');
|
| - assert.equal(request.withCredentials, true);
|
| - assert.equal(request.requestBody, null);
|
| - assert.ok(!('Content-type' in request.requestHeaders));
|
| - request.respond(200, {}, 'body');
|
| + assert.equal(fakeXhr.method, 'GET');
|
| + assert.equal(fakeXhr.url, 'http://foo.com');
|
| + assert.equal(fakeXhr.withCredentials, true);
|
| + assert.equal(fakeXhr.requestBody, null);
|
| + assert.ok(!('Content-type' in fakeXhr.requestHeaders));
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
| QUnit.test('POST with text content', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| var done = assert.async();
|
| - var request = remoting.xhr.start({
|
| +
|
| + var promise = new remoting.Xhr({
|
| method: 'POST',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - },
|
| + responseType: remoting.Xhr.ResponseType.TEXT,
|
| textContent: 'the_content_string'
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| + done();
|
| });
|
| - assert.equal(request.method, 'POST');
|
| - assert.equal(request.url, 'http://foo.com');
|
| - assert.equal(request.withCredentials, false);
|
| - assert.equal(request.requestBody, 'the_content_string');
|
| - assert.ok(!('Content-type' in request.requestHeaders));
|
| - request.respond(200, {}, 'body');
|
| + assert.equal(fakeXhr.method, 'POST');
|
| + assert.equal(fakeXhr.url, 'http://foo.com');
|
| + assert.equal(fakeXhr.withCredentials, false);
|
| + assert.equal(fakeXhr.requestBody, 'the_content_string');
|
| + assert.ok(!('Content-type' in fakeXhr.requestHeaders));
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
| QUnit.test('POST with form content', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| - var done = assert.async();
|
| - var request = remoting.xhr.start({
|
| + var promise = new remoting.Xhr({
|
| method: 'POST',
|
| url: 'http://foo.com',
|
| - onDone: function(xhr) {
|
| - assert.ok(xhr === request);
|
| - assert.equal(xhr.status, 200);
|
| - assert.equal(xhr.responseText, 'body');
|
| - done();
|
| - },
|
| + responseType: remoting.Xhr.ResponseType.TEXT,
|
| formContent: {'a': 'b', 'c': 'd'}
|
| + }).start().then(function(response) {
|
| + assert.equal(response.status, 200);
|
| + assert.equal(response.getText(), 'body');
|
| });
|
| - assert.equal(request.method, 'POST');
|
| - assert.equal(request.url, 'http://foo.com');
|
| - assert.equal(request.withCredentials, false);
|
| - assert.equal(request.requestBody, 'a=b&c=d');
|
| + assert.equal(fakeXhr.method, 'POST');
|
| + assert.equal(fakeXhr.url, 'http://foo.com');
|
| + assert.equal(fakeXhr.withCredentials, false);
|
| + assert.equal(fakeXhr.requestBody, 'a=b&c=d');
|
| assert.equal(
|
| - request.requestHeaders['Content-type'],
|
| + fakeXhr.requestHeaders['Content-type'],
|
| 'application/x-www-form-urlencoded');
|
| - request.respond(200, {}, 'body');
|
| + fakeXhr.respond(200, {}, 'body');
|
| + return promise;
|
| });
|
|
|
| QUnit.test('defaultResponse 200', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| var done = assert.async();
|
|
|
| var onDone = function() {
|
| @@ -203,18 +195,17 @@ QUnit.test('defaultResponse 200', function(assert) {
|
| done();
|
| };
|
|
|
| - var request = remoting.xhr.start({
|
| + new remoting.Xhr({
|
| method: 'POST',
|
| - url: 'http://foo.com',
|
| - onDone: remoting.xhr.defaultResponse(onDone, onError)
|
| - });
|
| - request.respond(200);
|
| + url: 'http://foo.com'
|
| + }).start().then(remoting.Xhr.defaultResponse(onDone, onError));
|
| + fakeXhr.respond(200, {}, '');
|
| });
|
|
|
|
|
| QUnit.test('defaultResponse 404', function(assert) {
|
| - sinon.useFakeXMLHttpRequest();
|
| var done = assert.async();
|
| +
|
| var onDone = function() {
|
| assert.ok(false);
|
| done();
|
| @@ -225,12 +216,11 @@ QUnit.test('defaultResponse 404', function(assert) {
|
| done();
|
| };
|
|
|
| - var request = remoting.xhr.start({
|
| + new remoting.Xhr({
|
| method: 'POST',
|
| - url: 'http://foo.com',
|
| - onDone: remoting.xhr.defaultResponse(onDone, onError)
|
| - });
|
| - request.respond(404);
|
| + url: 'http://foo.com'
|
| + }).start().then(remoting.Xhr.defaultResponse(onDone, onError));
|
| + fakeXhr.respond(404, {}, '');
|
| });
|
|
|
| })();
|
|
|