OLD | NEW |
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} | 7 * @suppress {checkTypes|checkVars|reportUnknownTypes} |
8 */ | 8 */ |
9 | 9 |
10 (function() { | 10 (function() { |
11 | 11 |
12 'use strict'; | 12 'use strict'; |
| 13 QUnit.module('xhr'); |
13 | 14 |
14 module('xhr', { | 15 QUnit.test('urlencodeParamHash', function(assert) { |
15 setup: function() { | 16 assert.equal( |
16 }, | |
17 teardown: function() { | |
18 } | |
19 }); | |
20 | |
21 test('urlencodeParamHash', function() { | |
22 QUnit.equal( | |
23 remoting.xhr.urlencodeParamHash({}), | 17 remoting.xhr.urlencodeParamHash({}), |
24 ''); | 18 ''); |
25 QUnit.equal( | 19 assert.equal( |
26 remoting.xhr.urlencodeParamHash({'key': 'value'}), | 20 remoting.xhr.urlencodeParamHash({'key': 'value'}), |
27 'key=value'); | 21 'key=value'); |
28 QUnit.equal( | 22 assert.equal( |
29 remoting.xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), | 23 remoting.xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), |
30 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); | 24 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); |
31 QUnit.equal( | 25 assert.equal( |
32 remoting.xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), | 26 remoting.xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), |
33 'k1=v1&k2=v2'); | 27 'k1=v1&k2=v2'); |
34 }); | 28 }); |
35 | 29 |
36 asyncTest('basic GET', function() { | 30 QUnit.test('basic GET', function(assert) { |
| 31 sinon.useFakeXMLHttpRequest(); |
| 32 var done = assert.async(); |
| 33 var request = remoting.xhr.start({ |
| 34 method: 'GET', |
| 35 url: 'http://foo.com', |
| 36 onDone: function(xhr) { |
| 37 assert.ok(xhr === request); |
| 38 assert.equal(xhr.status, 200); |
| 39 assert.equal(xhr.responseText, 'body'); |
| 40 done(); |
| 41 } |
| 42 }); |
| 43 assert.equal(request.method, 'GET'); |
| 44 assert.equal(request.url, 'http://foo.com'); |
| 45 assert.equal(request.withCredentials, false); |
| 46 assert.equal(request.requestBody, null); |
| 47 assert.ok(!('Content-type' in request.requestHeaders)); |
| 48 request.respond(200, {}, 'body'); |
| 49 }); |
| 50 |
| 51 QUnit.test('GET with param string', function(assert) { |
| 52 var done = assert.async(); |
37 sinon.useFakeXMLHttpRequest(); | 53 sinon.useFakeXMLHttpRequest(); |
38 var request = remoting.xhr.start({ | 54 var request = remoting.xhr.start({ |
39 method: 'GET', | 55 method: 'GET', |
40 url: 'http://foo.com', | 56 url: 'http://foo.com', |
41 onDone: function(xhr) { | 57 onDone: function(xhr) { |
42 QUnit.ok(xhr === request); | 58 assert.ok(xhr === request); |
43 QUnit.equal(xhr.status, 200); | 59 assert.equal(xhr.status, 200); |
44 QUnit.equal(xhr.responseText, 'body'); | 60 assert.equal(xhr.responseText, 'body'); |
45 QUnit.start(); | 61 done(); |
46 } | 62 }, |
| 63 urlParams: 'the_param_string' |
47 }); | 64 }); |
48 QUnit.equal(request.method, 'GET'); | 65 assert.equal(request.method, 'GET'); |
49 QUnit.equal(request.url, 'http://foo.com'); | 66 assert.equal(request.url, 'http://foo.com?the_param_string'); |
50 QUnit.equal(request.withCredentials, false); | 67 assert.equal(request.withCredentials, false); |
51 QUnit.equal(request.requestBody, null); | 68 assert.equal(request.requestBody, null); |
52 QUnit.ok(!('Content-type' in request.requestHeaders)); | 69 assert.ok(!('Content-type' in request.requestHeaders)); |
53 request.respond(200, {}, 'body'); | 70 request.respond(200, {}, 'body'); |
54 }); | 71 }); |
55 | 72 |
56 asyncTest('GET with param string', function() { | 73 QUnit.test('GET with param object', function(assert) { |
| 74 var done = assert.async(); |
57 sinon.useFakeXMLHttpRequest(); | 75 sinon.useFakeXMLHttpRequest(); |
58 var request = remoting.xhr.start({ | 76 var request = remoting.xhr.start({ |
59 method: 'GET', | 77 method: 'GET', |
60 url: 'http://foo.com', | 78 url: 'http://foo.com', |
61 onDone: function(xhr) { | 79 onDone: function(xhr) { |
62 QUnit.ok(xhr === request); | 80 assert.ok(xhr === request); |
63 QUnit.equal(xhr.status, 200); | 81 assert.equal(xhr.status, 200); |
64 QUnit.equal(xhr.responseText, 'body'); | 82 assert.equal(xhr.responseText, 'body'); |
65 QUnit.start(); | 83 done(); |
66 }, | 84 }, |
67 urlParams: 'the_param_string' | 85 urlParams: {'a': 'b', 'c': 'd'} |
68 }); | 86 }); |
69 QUnit.equal(request.method, 'GET'); | 87 assert.equal(request.method, 'GET'); |
70 QUnit.equal(request.url, 'http://foo.com?the_param_string'); | 88 assert.equal(request.url, 'http://foo.com?a=b&c=d'); |
71 QUnit.equal(request.withCredentials, false); | 89 assert.equal(request.withCredentials, false); |
72 QUnit.equal(request.requestBody, null); | 90 assert.equal(request.requestBody, null); |
73 QUnit.ok(!('Content-type' in request.requestHeaders)); | 91 assert.ok(!('Content-type' in request.requestHeaders)); |
74 request.respond(200, {}, 'body'); | 92 request.respond(200, {}, 'body'); |
75 }); | 93 }); |
76 | 94 |
77 asyncTest('GET with param object', function() { | 95 QUnit.test('GET with headers', function(assert) { |
78 sinon.useFakeXMLHttpRequest(); | 96 sinon.useFakeXMLHttpRequest(); |
| 97 var done = assert.async(); |
79 var request = remoting.xhr.start({ | 98 var request = remoting.xhr.start({ |
80 method: 'GET', | 99 method: 'GET', |
81 url: 'http://foo.com', | 100 url: 'http://foo.com', |
82 onDone: function(xhr) { | 101 onDone: function(xhr) { |
83 QUnit.ok(xhr === request); | 102 assert.ok(xhr === request); |
84 QUnit.equal(xhr.status, 200); | 103 assert.equal(xhr.status, 200); |
85 QUnit.equal(xhr.responseText, 'body'); | 104 assert.equal(xhr.responseText, 'body'); |
86 QUnit.start(); | 105 done(); |
87 }, | 106 }, |
88 urlParams: {'a': 'b', 'c': 'd'} | 107 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} |
89 }); | 108 }); |
90 QUnit.equal(request.method, 'GET'); | 109 assert.equal(request.method, 'GET'); |
91 QUnit.equal(request.url, 'http://foo.com?a=b&c=d'); | 110 assert.equal(request.url, 'http://foo.com'); |
92 QUnit.equal(request.withCredentials, false); | 111 assert.equal(request.withCredentials, false); |
93 QUnit.equal(request.requestBody, null); | 112 assert.equal(request.requestBody, null); |
94 QUnit.ok(!('Content-type' in request.requestHeaders)); | 113 assert.equal( |
| 114 request.requestHeaders['Header1'], |
| 115 'headerValue1'); |
| 116 assert.equal( |
| 117 request.requestHeaders['Header2'], |
| 118 'headerValue2'); |
| 119 assert.ok(!('Content-type' in request.requestHeaders)); |
95 request.respond(200, {}, 'body'); | 120 request.respond(200, {}, 'body'); |
96 }); | 121 }); |
97 | 122 |
98 asyncTest('GET with headers', function() { | 123 |
| 124 QUnit.test('GET with credentials', function(assert) { |
99 sinon.useFakeXMLHttpRequest(); | 125 sinon.useFakeXMLHttpRequest(); |
| 126 var done = assert.async(); |
100 var request = remoting.xhr.start({ | 127 var request = remoting.xhr.start({ |
101 method: 'GET', | 128 method: 'GET', |
102 url: 'http://foo.com', | 129 url: 'http://foo.com', |
103 onDone: function(xhr) { | 130 onDone: function(xhr) { |
104 QUnit.ok(xhr === request); | 131 assert.ok(xhr === request); |
105 QUnit.equal(xhr.status, 200); | 132 assert.equal(xhr.status, 200); |
106 QUnit.equal(xhr.responseText, 'body'); | 133 assert.equal(xhr.responseText, 'body'); |
107 QUnit.start(); | 134 done(); |
108 }, | 135 }, |
109 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} | 136 withCredentials: true |
110 }); | 137 }); |
111 QUnit.equal(request.method, 'GET'); | 138 assert.equal(request.method, 'GET'); |
112 QUnit.equal(request.url, 'http://foo.com'); | 139 assert.equal(request.url, 'http://foo.com'); |
113 QUnit.equal(request.withCredentials, false); | 140 assert.equal(request.withCredentials, true); |
114 QUnit.equal(request.requestBody, null); | 141 assert.equal(request.requestBody, null); |
115 QUnit.equal( | 142 assert.ok(!('Content-type' in request.requestHeaders)); |
116 request.requestHeaders['Header1'], | |
117 'headerValue1'); | |
118 QUnit.equal( | |
119 request.requestHeaders['Header2'], | |
120 'headerValue2'); | |
121 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
122 request.respond(200, {}, 'body'); | 143 request.respond(200, {}, 'body'); |
123 }); | 144 }); |
124 | 145 |
125 | 146 QUnit.test('POST with text content', function(assert) { |
126 asyncTest('GET with credentials', function() { | |
127 sinon.useFakeXMLHttpRequest(); | 147 sinon.useFakeXMLHttpRequest(); |
128 var request = remoting.xhr.start({ | 148 var done = assert.async(); |
129 method: 'GET', | |
130 url: 'http://foo.com', | |
131 onDone: function(xhr) { | |
132 QUnit.ok(xhr === request); | |
133 QUnit.equal(xhr.status, 200); | |
134 QUnit.equal(xhr.responseText, 'body'); | |
135 QUnit.start(); | |
136 }, | |
137 withCredentials: true | |
138 }); | |
139 QUnit.equal(request.method, 'GET'); | |
140 QUnit.equal(request.url, 'http://foo.com'); | |
141 QUnit.equal(request.withCredentials, true); | |
142 QUnit.equal(request.requestBody, null); | |
143 QUnit.ok(!('Content-type' in request.requestHeaders)); | |
144 request.respond(200, {}, 'body'); | |
145 }); | |
146 | |
147 asyncTest('POST with text content', function() { | |
148 sinon.useFakeXMLHttpRequest(); | |
149 var request = remoting.xhr.start({ | 149 var request = remoting.xhr.start({ |
150 method: 'POST', | 150 method: 'POST', |
151 url: 'http://foo.com', | 151 url: 'http://foo.com', |
152 onDone: function(xhr) { | 152 onDone: function(xhr) { |
153 QUnit.ok(xhr === request); | 153 assert.ok(xhr === request); |
154 QUnit.equal(xhr.status, 200); | 154 assert.equal(xhr.status, 200); |
155 QUnit.equal(xhr.responseText, 'body'); | 155 assert.equal(xhr.responseText, 'body'); |
156 QUnit.start(); | 156 done(); |
157 }, | 157 }, |
158 textContent: 'the_content_string' | 158 textContent: 'the_content_string' |
159 }); | 159 }); |
160 QUnit.equal(request.method, 'POST'); | 160 assert.equal(request.method, 'POST'); |
161 QUnit.equal(request.url, 'http://foo.com'); | 161 assert.equal(request.url, 'http://foo.com'); |
162 QUnit.equal(request.withCredentials, false); | 162 assert.equal(request.withCredentials, false); |
163 QUnit.equal(request.requestBody, 'the_content_string'); | 163 assert.equal(request.requestBody, 'the_content_string'); |
164 QUnit.ok(!('Content-type' in request.requestHeaders)); | 164 assert.ok(!('Content-type' in request.requestHeaders)); |
165 request.respond(200, {}, 'body'); | 165 request.respond(200, {}, 'body'); |
166 }); | 166 }); |
167 | 167 |
168 asyncTest('POST with form content', function() { | 168 QUnit.test('POST with form content', function(assert) { |
169 sinon.useFakeXMLHttpRequest(); | 169 sinon.useFakeXMLHttpRequest(); |
| 170 var done = assert.async(); |
170 var request = remoting.xhr.start({ | 171 var request = remoting.xhr.start({ |
171 method: 'POST', | 172 method: 'POST', |
172 url: 'http://foo.com', | 173 url: 'http://foo.com', |
173 onDone: function(xhr) { | 174 onDone: function(xhr) { |
174 QUnit.ok(xhr === request); | 175 assert.ok(xhr === request); |
175 QUnit.equal(xhr.status, 200); | 176 assert.equal(xhr.status, 200); |
176 QUnit.equal(xhr.responseText, 'body'); | 177 assert.equal(xhr.responseText, 'body'); |
177 QUnit.start(); | 178 done(); |
178 }, | 179 }, |
179 formContent: {'a': 'b', 'c': 'd'} | 180 formContent: {'a': 'b', 'c': 'd'} |
180 }); | 181 }); |
181 QUnit.equal(request.method, 'POST'); | 182 assert.equal(request.method, 'POST'); |
182 QUnit.equal(request.url, 'http://foo.com'); | 183 assert.equal(request.url, 'http://foo.com'); |
183 QUnit.equal(request.withCredentials, false); | 184 assert.equal(request.withCredentials, false); |
184 QUnit.equal(request.requestBody, 'a=b&c=d'); | 185 assert.equal(request.requestBody, 'a=b&c=d'); |
185 QUnit.equal( | 186 assert.equal( |
186 request.requestHeaders['Content-type'], | 187 request.requestHeaders['Content-type'], |
187 'application/x-www-form-urlencoded'); | 188 'application/x-www-form-urlencoded'); |
188 request.respond(200, {}, 'body'); | 189 request.respond(200, {}, 'body'); |
189 }); | 190 }); |
190 | 191 |
191 asyncTest('defaultResponse 200', function() { | 192 QUnit.test('defaultResponse 200', function(assert) { |
192 sinon.useFakeXMLHttpRequest(); | 193 sinon.useFakeXMLHttpRequest(); |
| 194 var done = assert.async(); |
193 | 195 |
194 var onDone = function() { | 196 var onDone = function() { |
195 QUnit.ok(true); | 197 assert.ok(true); |
196 QUnit.start(); | 198 done(); |
197 }; | 199 }; |
198 | 200 |
199 var onError = function(error) { | 201 var onError = function(error) { |
200 QUnit.ok(false); | 202 assert.ok(false); |
201 QUnit.start(); | 203 done(); |
202 }; | 204 }; |
203 | 205 |
204 var request = remoting.xhr.start({ | 206 var request = remoting.xhr.start({ |
205 method: 'POST', | 207 method: 'POST', |
206 url: 'http://foo.com', | 208 url: 'http://foo.com', |
207 onDone: remoting.xhr.defaultResponse(onDone, onError) | 209 onDone: remoting.xhr.defaultResponse(onDone, onError) |
208 }); | 210 }); |
209 request.respond(200); | 211 request.respond(200); |
210 }); | 212 }); |
211 | 213 |
212 | 214 |
213 asyncTest('defaultResponse 404', function() { | 215 QUnit.test('defaultResponse 404', function(assert) { |
214 sinon.useFakeXMLHttpRequest(); | 216 sinon.useFakeXMLHttpRequest(); |
215 | 217 var done = assert.async(); |
216 var onDone = function() { | 218 var onDone = function() { |
217 QUnit.ok(false); | 219 assert.ok(false); |
218 QUnit.start(); | 220 done(); |
219 }; | 221 }; |
220 | 222 |
221 var onError = function(error) { | 223 var onError = function(error) { |
222 QUnit.ok(true); | 224 assert.ok(true); |
223 QUnit.start(); | 225 done(); |
224 }; | 226 }; |
225 | 227 |
226 var request = remoting.xhr.start({ | 228 var request = remoting.xhr.start({ |
227 method: 'POST', | 229 method: 'POST', |
228 url: 'http://foo.com', | 230 url: 'http://foo.com', |
229 onDone: remoting.xhr.defaultResponse(onDone, onError) | 231 onDone: remoting.xhr.defaultResponse(onDone, onError) |
230 }); | 232 }); |
231 request.respond(404); | 233 request.respond(404); |
232 }); | 234 }); |
233 | 235 |
234 })(); | 236 })(); |
OLD | NEW |