Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(410)

Side by Side Diff: remoting/webapp/crd/js/xhr_unittest.js

Issue 1003433002: Updated remoting.xhr API to use promises. Removed access to the native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@spy-promise
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * @suppress {checkTypes|checkVars|reportUnknownTypes}
8 */ 7 */
9 8
10 (function() { 9 (function() {
11 10
12 'use strict'; 11 'use strict';
13 12
14 module('xhr', { 13 /** @type {sinon.FakeXhr} */
15 setup: function() { 14 var fakeXhr;
15
16 QUnit.module('xhr', {
17 beforeEach: function() {
18 fakeXhr = null;
19 sinon.useFakeXMLHttpRequest().onCreate =
20 function(/** sinon.FakeXhr */ xhr) {
21 fakeXhr = xhr;
22 };
16 }, 23 },
17 teardown: function() { 24 afterEach: function() {
18 } 25 }
19 }); 26 });
20 27
21 test('urlencodeParamHash', function() { 28 QUnit.test('urlencodeParamHash', function() {
22 QUnit.equal( 29 QUnit.equal(
23 remoting.xhr.urlencodeParamHash({}), 30 remoting.Xhr.urlencodeParamHash({}),
24 ''); 31 '');
25 QUnit.equal( 32 QUnit.equal(
26 remoting.xhr.urlencodeParamHash({'key': 'value'}), 33 remoting.Xhr.urlencodeParamHash({'key': 'value'}),
27 'key=value'); 34 'key=value');
28 QUnit.equal( 35 QUnit.equal(
29 remoting.xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), 36 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}),
30 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); 37 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26');
31 QUnit.equal( 38 QUnit.equal(
32 remoting.xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), 39 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}),
33 'k1=v1&k2=v2'); 40 'k1=v1&k2=v2');
34 }); 41 });
35 42
36 asyncTest('basic GET', function() { 43 QUnit.test('basic GET', function(assert) {
37 sinon.useFakeXMLHttpRequest(); 44 var done = assert.async();
38 var request = remoting.xhr.start({ 45
39 method: 'GET', 46 new remoting.Xhr({
40 url: 'http://foo.com', 47 method: 'GET',
41 onDone: function(xhr) { 48 url: 'http://foo.com',
42 QUnit.ok(xhr === request); 49 responseType: remoting.Xhr.ResponseType.TEXT
43 QUnit.equal(xhr.status, 200); 50 }).start().then(function(response) {
44 QUnit.equal(xhr.responseText, 'body'); 51 QUnit.equal(response.status, 200);
45 QUnit.start(); 52 QUnit.equal(response.getText(), 'body');
46 } 53 done();
47 }); 54 });
48 QUnit.equal(request.method, 'GET'); 55 QUnit.equal(fakeXhr.method, 'GET');
49 QUnit.equal(request.url, 'http://foo.com'); 56 QUnit.equal(fakeXhr.url, 'http://foo.com');
50 QUnit.equal(request.withCredentials, false); 57 QUnit.equal(fakeXhr.withCredentials, false);
51 QUnit.equal(request.requestBody, null); 58 QUnit.equal(fakeXhr.requestBody, null);
52 QUnit.ok(!('Content-type' in request.requestHeaders)); 59 QUnit.ok(!('Content-type' in fakeXhr.requestHeaders));
53 request.respond(200, {}, 'body'); 60 fakeXhr.respond(200, {}, 'body');
54 }); 61 });
55 62
56 asyncTest('GET with param string', function() { 63 QUnit.test('GET with param string', function(assert) {
57 sinon.useFakeXMLHttpRequest(); 64 var done = assert.async();
58 var request = remoting.xhr.start({ 65
59 method: 'GET', 66 new remoting.Xhr({
60 url: 'http://foo.com', 67 method: 'GET',
61 onDone: function(xhr) { 68 url: 'http://foo.com',
62 QUnit.ok(xhr === request); 69 responseType: remoting.Xhr.ResponseType.TEXT,
63 QUnit.equal(xhr.status, 200);
64 QUnit.equal(xhr.responseText, 'body');
65 QUnit.start();
66 },
67 urlParams: 'the_param_string' 70 urlParams: 'the_param_string'
68 }); 71 }).start().then(function(response) {
69 QUnit.equal(request.method, 'GET'); 72 QUnit.equal(response.status, 200);
70 QUnit.equal(request.url, 'http://foo.com?the_param_string'); 73 QUnit.equal(response.getText(), 'body');
71 QUnit.equal(request.withCredentials, false); 74 done();
72 QUnit.equal(request.requestBody, null); 75 });
73 QUnit.ok(!('Content-type' in request.requestHeaders)); 76 QUnit.equal(fakeXhr.method, 'GET');
74 request.respond(200, {}, 'body'); 77 QUnit.equal(fakeXhr.url, 'http://foo.com?the_param_string');
75 }); 78 QUnit.equal(fakeXhr.withCredentials, false);
76 79 QUnit.equal(fakeXhr.requestBody, null);
77 asyncTest('GET with param object', function() { 80 QUnit.ok(!('Content-type' in fakeXhr.requestHeaders));
78 sinon.useFakeXMLHttpRequest(); 81 fakeXhr.respond(200, {}, 'body');
79 var request = remoting.xhr.start({ 82 });
80 method: 'GET', 83
81 url: 'http://foo.com', 84 QUnit.test('GET with param object', function(assert) {
82 onDone: function(xhr) { 85 var done = assert.async();
83 QUnit.ok(xhr === request); 86
84 QUnit.equal(xhr.status, 200); 87 new remoting.Xhr({
85 QUnit.equal(xhr.responseText, 'body'); 88 method: 'GET',
86 QUnit.start(); 89 url: 'http://foo.com',
87 }, 90 responseType: remoting.Xhr.ResponseType.TEXT,
88 urlParams: {'a': 'b', 'c': 'd'} 91 urlParams: {'a': 'b', 'c': 'd'}
89 }); 92 }).start().then(function(response) {
90 QUnit.equal(request.method, 'GET'); 93 QUnit.equal(response.status, 200);
91 QUnit.equal(request.url, 'http://foo.com?a=b&c=d'); 94 QUnit.equal(response.getText(), 'body');
92 QUnit.equal(request.withCredentials, false); 95 done();
93 QUnit.equal(request.requestBody, null); 96 });
94 QUnit.ok(!('Content-type' in request.requestHeaders)); 97 QUnit.equal(fakeXhr.method, 'GET');
95 request.respond(200, {}, 'body'); 98 QUnit.equal(fakeXhr.url, 'http://foo.com?a=b&c=d');
96 }); 99 QUnit.equal(fakeXhr.withCredentials, false);
97 100 QUnit.equal(fakeXhr.requestBody, null);
98 asyncTest('GET with headers', function() { 101 QUnit.ok(!('Content-type' in fakeXhr.requestHeaders));
99 sinon.useFakeXMLHttpRequest(); 102 fakeXhr.respond(200, {}, 'body');
100 var request = remoting.xhr.start({ 103 });
101 method: 'GET', 104
102 url: 'http://foo.com', 105 QUnit.test('GET with headers', function(assert) {
103 onDone: function(xhr) { 106 var done = assert.async();
104 QUnit.ok(xhr === request); 107
105 QUnit.equal(xhr.status, 200); 108 new remoting.Xhr({
106 QUnit.equal(xhr.responseText, 'body'); 109 method: 'GET',
107 QUnit.start(); 110 url: 'http://foo.com',
108 }, 111 responseType: remoting.Xhr.ResponseType.TEXT,
109 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} 112 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'}
110 }); 113 }).start().then(function(response) {
111 QUnit.equal(request.method, 'GET'); 114 QUnit.equal(response.status, 200);
112 QUnit.equal(request.url, 'http://foo.com'); 115 QUnit.equal(response.getText(), 'body');
113 QUnit.equal(request.withCredentials, false); 116 done();
114 QUnit.equal(request.requestBody, null); 117 });
115 QUnit.equal( 118 QUnit.equal(fakeXhr.method, 'GET');
116 request.requestHeaders['Header1'], 119 QUnit.equal(fakeXhr.url, 'http://foo.com');
120 QUnit.equal(fakeXhr.withCredentials, false);
121 QUnit.equal(fakeXhr.requestBody, null);
122 QUnit.equal(
123 fakeXhr.requestHeaders['Header1'],
117 'headerValue1'); 124 'headerValue1');
118 QUnit.equal( 125 QUnit.equal(
119 request.requestHeaders['Header2'], 126 fakeXhr.requestHeaders['Header2'],
120 'headerValue2'); 127 'headerValue2');
121 QUnit.ok(!('Content-type' in request.requestHeaders)); 128 QUnit.ok(!('Content-type' in fakeXhr.requestHeaders));
122 request.respond(200, {}, 'body'); 129 fakeXhr.respond(200, {}, 'body');
123 }); 130 });
124 131
125 132
126 asyncTest('GET with credentials', function() { 133 QUnit.test('GET with credentials', function(assert) {
127 sinon.useFakeXMLHttpRequest(); 134 var done = assert.async();
128 var request = remoting.xhr.start({ 135
129 method: 'GET', 136 new remoting.Xhr({
130 url: 'http://foo.com', 137 method: 'GET',
131 onDone: function(xhr) { 138 url: 'http://foo.com',
132 QUnit.ok(xhr === request); 139 responseType: remoting.Xhr.ResponseType.TEXT,
133 QUnit.equal(xhr.status, 200);
134 QUnit.equal(xhr.responseText, 'body');
135 QUnit.start();
136 },
137 withCredentials: true 140 withCredentials: true
138 }); 141 }).start().then(function(response) {
139 QUnit.equal(request.method, 'GET'); 142 QUnit.equal(response.status, 200);
140 QUnit.equal(request.url, 'http://foo.com'); 143 QUnit.equal(response.getText(), 'body');
141 QUnit.equal(request.withCredentials, true); 144 done();
142 QUnit.equal(request.requestBody, null); 145 });
143 QUnit.ok(!('Content-type' in request.requestHeaders)); 146 QUnit.equal(fakeXhr.method, 'GET');
144 request.respond(200, {}, 'body'); 147 QUnit.equal(fakeXhr.url, 'http://foo.com');
145 }); 148 QUnit.equal(fakeXhr.withCredentials, true);
146 149 QUnit.equal(fakeXhr.requestBody, null);
147 asyncTest('POST with text content', function() { 150 QUnit.ok(!('Content-type' in fakeXhr.requestHeaders));
148 sinon.useFakeXMLHttpRequest(); 151 fakeXhr.respond(200, {}, 'body');
149 var request = remoting.xhr.start({ 152 });
150 method: 'POST', 153
151 url: 'http://foo.com', 154 QUnit.test('POST with text content', function(assert) {
152 onDone: function(xhr) { 155 var done = assert.async();
153 QUnit.ok(xhr === request); 156
154 QUnit.equal(xhr.status, 200); 157 new remoting.Xhr({
155 QUnit.equal(xhr.responseText, 'body'); 158 method: 'POST',
156 QUnit.start(); 159 url: 'http://foo.com',
157 }, 160 responseType: remoting.Xhr.ResponseType.TEXT,
158 textContent: 'the_content_string' 161 textContent: 'the_content_string'
159 }); 162 }).start().then(function(response) {
160 QUnit.equal(request.method, 'POST'); 163 QUnit.equal(response.status, 200);
161 QUnit.equal(request.url, 'http://foo.com'); 164 QUnit.equal(response.getText(), 'body');
162 QUnit.equal(request.withCredentials, false); 165 done();
163 QUnit.equal(request.requestBody, 'the_content_string'); 166 });
164 QUnit.ok(!('Content-type' in request.requestHeaders)); 167 QUnit.equal(fakeXhr.method, 'POST');
165 request.respond(200, {}, 'body'); 168 QUnit.equal(fakeXhr.url, 'http://foo.com');
166 }); 169 QUnit.equal(fakeXhr.withCredentials, false);
167 170 QUnit.equal(fakeXhr.requestBody, 'the_content_string');
168 asyncTest('POST with form content', function() { 171 QUnit.ok(!('Content-type' in fakeXhr.requestHeaders));
169 sinon.useFakeXMLHttpRequest(); 172 fakeXhr.respond(200, {}, 'body');
170 var request = remoting.xhr.start({ 173 });
171 method: 'POST', 174
172 url: 'http://foo.com', 175 QUnit.test('POST with form content', function(assert) {
173 onDone: function(xhr) { 176 var done = assert.async();
174 QUnit.ok(xhr === request); 177
175 QUnit.equal(xhr.status, 200); 178 new remoting.Xhr({
176 QUnit.equal(xhr.responseText, 'body'); 179 method: 'POST',
177 QUnit.start(); 180 url: 'http://foo.com',
178 }, 181 responseType: remoting.Xhr.ResponseType.TEXT,
179 formContent: {'a': 'b', 'c': 'd'} 182 formContent: {'a': 'b', 'c': 'd'}
180 }); 183 }).start().then(function(response) {
181 QUnit.equal(request.method, 'POST'); 184 QUnit.equal(response.status, 200);
182 QUnit.equal(request.url, 'http://foo.com'); 185 QUnit.equal(response.getText(), 'body');
183 QUnit.equal(request.withCredentials, false); 186 done();
184 QUnit.equal(request.requestBody, 'a=b&c=d'); 187 });
185 QUnit.equal( 188 QUnit.equal(fakeXhr.method, 'POST');
186 request.requestHeaders['Content-type'], 189 QUnit.equal(fakeXhr.url, 'http://foo.com');
190 QUnit.equal(fakeXhr.withCredentials, false);
191 QUnit.equal(fakeXhr.requestBody, 'a=b&c=d');
192 QUnit.equal(
193 fakeXhr.requestHeaders['Content-type'],
187 'application/x-www-form-urlencoded'); 194 'application/x-www-form-urlencoded');
188 request.respond(200, {}, 'body'); 195 fakeXhr.respond(200, {}, 'body');
189 }); 196 });
190 197
191 asyncTest('defaultResponse 200', function() { 198 QUnit.test('defaultResponse 200', function(assert) {
192 sinon.useFakeXMLHttpRequest(); 199 var done = assert.async();
193 200
194 var onDone = function() { 201 var onDone = function() {
195 QUnit.ok(true); 202 QUnit.ok(true);
196 QUnit.start(); 203 done();
197 }; 204 };
198 205
199 var onError = function(error) { 206 var onError = function(error) {
200 QUnit.ok(false); 207 QUnit.ok(false);
201 QUnit.start(); 208 done();
202 }; 209 };
203 210
204 var request = remoting.xhr.start({ 211 new remoting.Xhr({
205 method: 'POST', 212 method: 'POST',
206 url: 'http://foo.com', 213 url: 'http://foo.com'
207 onDone: remoting.xhr.defaultResponse(onDone, onError) 214 }).start().then(remoting.Xhr.defaultResponse(onDone, onError));
208 }); 215 fakeXhr.respond(200, {}, '');
209 request.respond(200); 216 });
210 }); 217
211 218
212 219 QUnit.test('defaultResponse 404', function(assert) {
213 asyncTest('defaultResponse 404', function() { 220 var done = assert.async();
214 sinon.useFakeXMLHttpRequest();
215 221
216 var onDone = function() { 222 var onDone = function() {
217 QUnit.ok(false); 223 QUnit.ok(false);
218 QUnit.start(); 224 done();
219 }; 225 };
220 226
221 var onError = function(error) { 227 var onError = function(error) {
222 QUnit.ok(true); 228 QUnit.ok(true);
223 QUnit.start(); 229 done();
224 }; 230 };
225 231
226 var request = remoting.xhr.start({ 232 new remoting.Xhr({
227 method: 'POST', 233 method: 'POST',
228 url: 'http://foo.com', 234 url: 'http://foo.com'
229 onDone: remoting.xhr.defaultResponse(onDone, onError) 235 }).start().then(remoting.Xhr.defaultResponse(onDone, onError));
230 }); 236 fakeXhr.respond(404, {}, '');
231 request.respond(404);
232 }); 237 });
233 238
234 })(); 239 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698