OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 (function() { | |
6 'use strict'; | |
7 | |
8 QUnit.module('mock_xhr', { | |
9 beforeEach: function() { | |
10 base.debug.throwOnAssert = true; | |
Jamie
2015/04/03 01:45:58
You're setting a global here without restoring it
John Williams
2015/04/04 01:40:26
Couldn't find a good place to move it to, so I jus
| |
11 remoting.MockXhr.activate(); | |
12 }, | |
13 afterEach: function() { | |
14 remoting.MockXhr.restore(); | |
15 } | |
16 }); | |
17 | |
18 QUnit.test('multiple calls to activate() fail', function(assert) { | |
19 assert.throws(function() { | |
20 remoting.MockXhr.activate(); | |
21 }); | |
22 }); | |
23 | |
24 QUnit.test('restore() without activate() fails', function(assert) { | |
25 remoting.MockXhr.restore(); | |
26 assert.throws(function() { | |
27 remoting.MockXhr.restore(); | |
28 }); | |
29 remoting.MockXhr.activate(); | |
30 }); | |
31 | |
32 /** | |
33 * @param {string=} opt_url The URL to request. | |
34 * @return {!Promise<!remoting.Xhr.Response>} | |
35 */ | |
36 function sendRequest(opt_url) { | |
37 return new remoting.Xhr({ | |
38 method: 'GET', | |
39 url: opt_url || 'http://foo.com' | |
40 }).start(); | |
41 }; | |
42 | |
43 /** | |
44 * @return {!Promise<!remoting.Xhr.Response>} | |
45 */ | |
46 function sendRequestForJson() { | |
47 return new remoting.Xhr({ | |
48 method: 'GET', | |
49 url: 'http://foo.com', | |
50 acceptJson: true | |
51 }).start(); | |
52 } | |
53 | |
54 QUnit.test('unhandled requests fail', function(assert) { | |
55 assert.throws(sendRequest); | |
56 }); | |
57 | |
58 QUnit.test('setEmptyResponseFor', function(assert) { | |
59 remoting.MockXhr.setEmptyResponseFor('GET', 'http://foo.com'); | |
60 var promise = sendRequest(); | |
61 assert.throws(sendRequest); | |
62 return promise.then(function(/** remoting.Xhr.Response */ result) { | |
63 assert.equal(result.status, 204); | |
64 assert.equal(result.getText(), ''); | |
65 assert.throws(result.getJson.bind(result)); | |
66 }); | |
67 }); | |
68 | |
69 QUnit.test('setEmptyResponseFor with repeat', function(assert) { | |
70 remoting.MockXhr.setEmptyResponseFor('GET', 'http://foo.com', true); | |
71 var promise1 = sendRequest(); | |
72 var promise2 = sendRequest(); | |
73 return promise1.then(function(/** remoting.Xhr.Response */ result) { | |
74 assert.equal(result.status, 204); | |
75 assert.equal(result.getText(), ''); | |
76 assert.throws(result.getJson.bind(result)); | |
77 return promise2; | |
78 }).then(function(/** remoting.Xhr.Response */ result) { | |
79 assert.equal(result.status, 204); | |
80 assert.equal(result.getText(), ''); | |
81 assert.throws(result.getJson.bind(result)); | |
82 }); | |
83 }); | |
84 | |
85 QUnit.test('setEmptyResponseFor with RegExp', function(assert) { | |
86 remoting.MockXhr.setEmptyResponseFor('GET', /foo/); | |
87 var promise = sendRequest(); | |
88 assert.throws(sendRequest); | |
89 return promise.then(function(/** remoting.Xhr.Response */ result) { | |
90 assert.equal(result.status, 204); | |
91 assert.equal(result.getText(), ''); | |
92 assert.throws(result.getJson.bind(result)); | |
93 }); | |
94 }); | |
95 | |
96 QUnit.test('setTextResponseFor', function(assert) { | |
97 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'first'); | |
98 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'second'); | |
99 var promise1 = sendRequest(); | |
100 var promise2 = sendRequest(); | |
101 return promise1.then(function(/** remoting.Xhr.Response */ result) { | |
102 assert.equal(result.status, 200); | |
103 assert.equal(result.getText(), 'first'); | |
104 assert.throws(result.getJson.bind(result)); | |
105 return promise2; | |
106 }).then(function(/** remoting.Xhr.Response */ result) { | |
107 assert.equal(result.status, 200); | |
108 assert.equal(result.getText(), 'second'); | |
109 assert.throws(result.getJson.bind(result)); | |
110 }); | |
111 }); | |
112 | |
113 QUnit.test('setTextResponseFor with different URLs', function(assert) { | |
114 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'first'); | |
115 remoting.MockXhr.setTextResponseFor('GET', /bar/, 'second'); | |
116 var promise1 = sendRequest('http://bar.com'); | |
117 var promise2 = sendRequest(); | |
118 return promise1.then(function(/** remoting.Xhr.Response */ result) { | |
119 assert.equal(result.status, 200); | |
120 assert.equal(result.getText(), 'second'); | |
121 assert.throws(result.getJson.bind(result)); | |
122 return promise2; | |
123 }).then(function(/** remoting.Xhr.Response */ result) { | |
124 assert.equal(result.status, 200); | |
125 assert.equal(result.getText(), 'first'); | |
126 assert.throws(result.getJson.bind(result)); | |
127 }); | |
128 }); | |
129 | |
130 | |
131 QUnit.test('setTextResponseFor with default', function(assert) { | |
132 remoting.MockXhr.setTextResponseFor('GET', /foo/, 'specific'); | |
133 remoting.MockXhr.setTextResponseFor('GET', null, 'default', true); | |
134 var promise1 = sendRequest('http://bar.com'); | |
135 var promise2 = sendRequest(); | |
136 var promise3 = sendRequest(); | |
137 return promise1.then(function(/** remoting.Xhr.Response */ result) { | |
138 assert.equal(result.status, 200); | |
139 assert.equal(result.getText(), 'default'); | |
140 assert.throws(result.getJson.bind(result)); | |
141 return promise2; | |
142 }).then(function(/** remoting.Xhr.Response */ result) { | |
143 assert.equal(result.status, 200); | |
144 assert.equal(result.getText(), 'specific'); | |
145 assert.throws(result.getJson.bind(result)); | |
146 return promise3; | |
147 }).then(function(/** remoting.Xhr.Response */ result) { | |
148 assert.equal(result.status, 200); | |
149 assert.equal(result.getText(), 'default'); | |
150 assert.throws(result.getJson.bind(result)); | |
151 }); | |
152 }); | |
153 | |
154 | |
155 QUnit.test('setJsonResponseFor', function(assert) { | |
156 remoting.MockXhr.setJsonResponseFor('GET', 'http://foo.com', 'foo'); | |
157 var promise = sendRequestForJson(); | |
158 assert.throws(sendRequestForJson); | |
159 return promise.then(function(/** remoting.Xhr.Response */ result) { | |
160 assert.equal(result.status, 200); | |
161 assert.equal(JSON.parse(result.getText()), 'foo'); | |
162 assert.equal(result.getJson(), 'foo'); | |
163 }); | |
164 }); | |
165 | |
166 })(); | |
OLD | NEW |