1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 QUnit
.module('mock_xhr', {
9 beforeEach: function() {
10 remoting
.MockXhr
.activate();
12 afterEach: function() {
13 remoting
.MockXhr
.restore();
18 * @param {string=} opt_url The URL to request.
19 * @return {!Promise<!remoting.Xhr.Response>}
21 function sendRequest(opt_url
) {
22 return new remoting
.Xhr({
24 url
: opt_url
|| 'http://foo.com'
29 * @return {!Promise<!remoting.Xhr.Response>}
31 function sendRequestForJson() {
32 return new remoting
.Xhr({
34 url
: 'http://foo.com',
39 QUnit
.test('unhandled requests fail', function(assert
) {
40 assert
.throws(sendRequest
);
43 QUnit
.test('setEmptyResponseFor', function(assert
) {
44 remoting
.MockXhr
.setEmptyResponseFor('GET', 'http://foo.com');
45 var promise
= sendRequest();
46 assert
.throws(sendRequest
);
47 return promise
.then(function(/** remoting.Xhr.Response */ result
) {
48 assert
.equal(result
.status
, 204);
49 assert
.equal(result
.getText(), '');
50 assert
.throws(result
.getJson
.bind(result
));
54 QUnit
.test('setEmptyResponseFor with repeat', function(assert
) {
55 remoting
.MockXhr
.setEmptyResponseFor('GET', 'http://foo.com', 404, true);
56 var promise1
= sendRequest();
57 var promise2
= sendRequest();
58 return promise1
.then(function(/** remoting.Xhr.Response */ result
) {
59 assert
.equal(result
.status
, 404);
60 assert
.equal(result
.getText(), '');
61 assert
.throws(result
.getJson
.bind(result
));
63 }).then(function(/** remoting.Xhr.Response */ result
) {
64 assert
.equal(result
.status
, 404);
65 assert
.equal(result
.getText(), '');
66 assert
.throws(result
.getJson
.bind(result
));
70 QUnit
.test('setEmptyResponseFor with RegExp', function(assert
) {
71 remoting
.MockXhr
.setEmptyResponseFor('GET', /foo/);
72 var promise
= sendRequest();
73 assert
.throws(sendRequest
);
74 return promise
.then(function(/** remoting.Xhr.Response */ result
) {
75 assert
.equal(result
.status
, 204);
76 assert
.equal(result
.getText(), '');
77 assert
.throws(result
.getJson
.bind(result
));
81 QUnit
.test('setTextResponseFor', function(assert
) {
82 remoting
.MockXhr
.setTextResponseFor('GET', /foo/, 'first');
83 remoting
.MockXhr
.setTextResponseFor('GET', /foo/, 'second');
84 var promise1
= sendRequest();
85 var promise2
= sendRequest();
86 return promise1
.then(function(/** remoting.Xhr.Response */ result
) {
87 assert
.equal(result
.status
, 200);
88 assert
.equal(result
.getText(), 'first');
89 assert
.throws(result
.getJson
.bind(result
));
91 }).then(function(/** remoting.Xhr.Response */ result
) {
92 assert
.equal(result
.status
, 200);
93 assert
.equal(result
.getText(), 'second');
94 assert
.throws(result
.getJson
.bind(result
));
98 QUnit
.test('setTextResponseFor with different URLs', function(assert
) {
99 remoting
.MockXhr
.setTextResponseFor('GET', /foo/, 'first');
100 remoting
.MockXhr
.setTextResponseFor('GET', /bar/, 'second');
101 var promise1
= sendRequest('http://bar.com');
102 var promise2
= sendRequest();
103 return promise1
.then(function(/** remoting.Xhr.Response */ result
) {
104 assert
.equal(result
.status
, 200);
105 assert
.equal(result
.getText(), 'second');
106 assert
.throws(result
.getJson
.bind(result
));
108 }).then(function(/** remoting.Xhr.Response */ result
) {
109 assert
.equal(result
.status
, 200);
110 assert
.equal(result
.getText(), 'first');
111 assert
.throws(result
.getJson
.bind(result
));
116 QUnit
.test('setTextResponseFor with default', function(assert
) {
117 remoting
.MockXhr
.setTextResponseFor('GET', /foo/, 'specific');
118 remoting
.MockXhr
.setTextResponseFor('GET', null, 'default', true);
119 var promise1
= sendRequest('http://bar.com');
120 var promise2
= sendRequest();
121 var promise3
= sendRequest();
122 return promise1
.then(function(/** remoting.Xhr.Response */ result
) {
123 assert
.equal(result
.status
, 200);
124 assert
.equal(result
.getText(), 'default');
125 assert
.throws(result
.getJson
.bind(result
));
127 }).then(function(/** remoting.Xhr.Response */ result
) {
128 assert
.equal(result
.status
, 200);
129 assert
.equal(result
.getText(), 'specific');
130 assert
.throws(result
.getJson
.bind(result
));
132 }).then(function(/** remoting.Xhr.Response */ result
) {
133 assert
.equal(result
.status
, 200);
134 assert
.equal(result
.getText(), 'default');
135 assert
.throws(result
.getJson
.bind(result
));
140 QUnit
.test('setJsonResponseFor', function(assert
) {
141 remoting
.MockXhr
.setJsonResponseFor('GET', 'http://foo.com', 'foo');
142 var promise
= sendRequestForJson();
143 assert
.throws(sendRequestForJson
);
144 return promise
.then(function(/** remoting.Xhr.Response */ result
) {
145 assert
.equal(result
.status
, 200);
146 assert
.equal(JSON
.parse(result
.getText()), 'foo');
147 assert
.equal(result
.getJson(), 'foo');