OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library XHRTest; | 5 library XHRTest; |
| 6 |
6 import 'dart:async'; | 7 import 'dart:async'; |
7 import 'dart:convert'; | 8 import 'dart:convert'; |
8 import 'dart:html'; | 9 import 'dart:html'; |
9 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
10 import 'package:unittest/html_individual_config.dart'; | 11 import 'package:unittest/html_individual_config.dart'; |
11 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
12 | 13 |
13 main() { | 14 main() { |
14 useHtmlIndividualConfiguration(); | 15 useHtmlIndividualConfiguration(); |
15 // Cache blocker is a workaround for: | 16 // Cache blocker is a workaround for: |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 80 } |
80 }, () => xhr.readyState == HttpRequest.DONE)); | 81 }, () => xhr.readyState == HttpRequest.DONE)); |
81 | 82 |
82 xhr.onLoadEnd.listen((ProgressEvent e) { | 83 xhr.onLoadEnd.listen((ProgressEvent e) { |
83 loadEndCalled = true; | 84 loadEndCalled = true; |
84 }); | 85 }); |
85 xhr.send(); | 86 xhr.send(); |
86 }); | 87 }); |
87 | 88 |
88 test('XHR.request No file', () { | 89 test('XHR.request No file', () { |
89 HttpRequest.request('NonExistingFile').then( | 90 HttpRequest.request('NonExistingFile').then((_) { |
90 (_) { fail('Request should not have succeeded.'); }, | 91 fail('Request should not have succeeded.'); |
91 onError: expectAsync((error) { | 92 }, onError: expectAsync((error) { |
92 var xhr = error.target; | 93 var xhr = error.target; |
93 expect(xhr.readyState, equals(HttpRequest.DONE)); | 94 expect(xhr.readyState, equals(HttpRequest.DONE)); |
94 validate404(xhr); | 95 validate404(xhr); |
95 })); | 96 })); |
96 }); | 97 }); |
97 | 98 |
98 test('XHR.request file', () { | 99 test('XHR.request file', () { |
99 HttpRequest.request(url).then(expectAsync((xhr) { | 100 HttpRequest.request(url).then(expectAsync((xhr) { |
100 expect(xhr.readyState, equals(HttpRequest.DONE)); | 101 expect(xhr.readyState, equals(HttpRequest.DONE)); |
101 validate200Response(xhr); | 102 validate200Response(xhr); |
102 })); | 103 })); |
103 }); | 104 }); |
104 | 105 |
105 test('XHR.request onProgress', () { | 106 test('XHR.request onProgress', () { |
106 var progressCalled = false; | 107 var progressCalled = false; |
107 HttpRequest.request(url, | 108 HttpRequest.request(url, onProgress: (_) { |
108 onProgress: (_) { | 109 progressCalled = true; |
109 progressCalled = true; | 110 }).then(expectAsync((xhr) { |
110 }).then(expectAsync( | 111 expect(xhr.readyState, equals(HttpRequest.DONE)); |
111 (xhr) { | 112 expect(progressCalled, HttpRequest.supportsProgressEvent); |
112 expect(xhr.readyState, equals(HttpRequest.DONE)); | 113 validate200Response(xhr); |
113 expect(progressCalled, HttpRequest.supportsProgressEvent); | 114 })); |
114 validate200Response(xhr); | |
115 })); | |
116 }); | 115 }); |
117 | 116 |
118 test('XHR.request withCredentials No file', () { | 117 test('XHR.request withCredentials No file', () { |
119 HttpRequest.request('NonExistingFile', withCredentials: true).then( | 118 HttpRequest.request('NonExistingFile', withCredentials: true).then((_) { |
120 (_) { fail('Request should not have succeeded.'); }, | 119 fail('Request should not have succeeded.'); |
121 onError: expectAsync((error) { | 120 }, onError: expectAsync((error) { |
122 var xhr = error.target; | 121 var xhr = error.target; |
123 expect(xhr.readyState, equals(HttpRequest.DONE)); | 122 expect(xhr.readyState, equals(HttpRequest.DONE)); |
124 validate404(xhr); | 123 validate404(xhr); |
125 })); | 124 })); |
126 }); | 125 }); |
127 | 126 |
128 test('XHR.request withCredentials file', () { | 127 test('XHR.request withCredentials file', () { |
129 HttpRequest.request(url, withCredentials: true).then(expectAsync((xhr) { | 128 HttpRequest.request(url, withCredentials: true).then(expectAsync((xhr) { |
130 expect(xhr.readyState, equals(HttpRequest.DONE)); | 129 expect(xhr.readyState, equals(HttpRequest.DONE)); |
131 validate200Response(xhr); | 130 validate200Response(xhr); |
132 })); | 131 })); |
133 }); | 132 }); |
134 | 133 |
135 test('XHR.getString file', () { | 134 test('XHR.getString file', () { |
136 HttpRequest.getString(url).then(expectAsync((str) {})); | 135 HttpRequest.getString(url).then(expectAsync((str) {})); |
137 }); | 136 }); |
138 | 137 |
139 test('XHR.getString No file', () { | 138 test('XHR.getString No file', () { |
140 HttpRequest.getString('NonExistingFile').then( | 139 HttpRequest.getString('NonExistingFile').then((_) { |
141 (_) { fail('Succeeded for non-existing file.'); }, | 140 fail('Succeeded for non-existing file.'); |
142 onError: expectAsync((error) { | 141 }, onError: expectAsync((error) { |
143 validate404(error.target); | 142 validate404(error.target); |
144 })); | 143 })); |
145 }); | 144 }); |
146 | 145 |
147 test('XHR.request responseType arraybuffer', () { | 146 test('XHR.request responseType arraybuffer', () { |
148 if (Platform.supportsTypedData) { | 147 if (Platform.supportsTypedData) { |
149 HttpRequest.request(url, responseType: 'arraybuffer', | 148 HttpRequest.request(url, responseType: 'arraybuffer', requestHeaders: { |
150 requestHeaders: {'Content-Type': 'text/xml'}).then( | 149 'Content-Type': 'text/xml' |
151 expectAsync((xhr) { | 150 }).then(expectAsync((xhr) { |
152 expect(xhr.status, equals(200)); | 151 expect(xhr.status, equals(200)); |
153 var byteBuffer = xhr.response; | 152 var byteBuffer = xhr.response; |
154 expect(byteBuffer, new isInstanceOf<ByteBuffer>()); | 153 expect(byteBuffer, new isInstanceOf<ByteBuffer>()); |
155 expect(byteBuffer, isNotNull); | 154 expect(byteBuffer, isNotNull); |
156 })); | 155 })); |
157 } | 156 } |
158 }); | 157 }); |
159 | 158 |
160 test('overrideMimeType', () { | 159 test('overrideMimeType', () { |
161 var expectation = | 160 var expectation = |
162 HttpRequest.supportsOverrideMimeType ? returnsNormally : throws; | 161 HttpRequest.supportsOverrideMimeType ? returnsNormally : throws; |
163 | 162 |
164 expect(() { | 163 expect(() { |
165 HttpRequest.request(url, mimeType: 'application/binary'); | 164 HttpRequest.request(url, mimeType: 'application/binary'); |
166 }, expectation); | 165 }, expectation); |
167 }); | 166 }); |
168 | 167 |
169 if (Platform.supportsTypedData) { | 168 if (Platform.supportsTypedData) { |
170 test('xhr upload', () { | 169 test('xhr upload', () { |
171 var xhr = new HttpRequest(); | 170 var xhr = new HttpRequest(); |
172 var progressCalled = false; | 171 var progressCalled = false; |
173 xhr.upload.onProgress.listen((e) { | 172 xhr.upload.onProgress.listen((e) { |
174 progressCalled = true; | 173 progressCalled = true; |
175 }); | 174 }); |
176 | 175 |
177 xhr.open('POST', | 176 xhr.open('POST', |
178 '${window.location.protocol}//${window.location.host}/echo'); | 177 '${window.location.protocol}//${window.location.host}/echo'); |
179 | 178 |
180 // 10MB of payload data w/ a bit of data to make sure it | 179 // 10MB of payload data w/ a bit of data to make sure it |
181 // doesn't get compressed to nil. | 180 // doesn't get compressed to nil. |
182 var data = new Uint8List(1 * 1024 * 1024); | 181 var data = new Uint8List(1 * 1024 * 1024); |
183 for (var i = 0; i < data.length; ++i) { | 182 for (var i = 0; i < data.length; ++i) { |
184 data[i] = i & 0xFF; | 183 data[i] = i & 0xFF; |
185 } | 184 } |
186 xhr.send(new Uint8List.view(data.buffer)); | 185 xhr.send(new Uint8List.view(data.buffer)); |
187 | 186 |
188 return xhr.onLoad.first.then((_) { | 187 return xhr.onLoad.first.then((_) { |
189 expect(progressCalled, isTrue, reason: 'onProgress should be fired'); | 188 expect(progressCalled, isTrue, reason: 'onProgress should be fired'); |
190 }); | 189 }); |
191 }); | 190 }); |
192 } | 191 } |
193 | 192 |
194 test('xhr postFormData', () { | 193 test('xhr postFormData', () { |
195 var data = { 'name': 'John', 'time': '2 pm' }; | 194 var data = {'name': 'John', 'time': '2 pm'}; |
196 | 195 |
197 var parts = []; | 196 var parts = []; |
198 for (var key in data.keys) { | 197 for (var key in data.keys) { |
199 parts.add('${Uri.encodeQueryComponent(key)}=' | 198 parts.add('${Uri.encodeQueryComponent(key)}=' |
200 '${Uri.encodeQueryComponent(data[key])}'); | 199 '${Uri.encodeQueryComponent(data[key])}'); |
201 } | 200 } |
202 var encodedData = parts.join('&'); | 201 var encodedData = parts.join('&'); |
203 | 202 |
204 return HttpRequest.postFormData( | 203 return HttpRequest |
205 '${window.location.protocol}//${window.location.host}/echo', data) | 204 .postFormData( |
| 205 '${window.location.protocol}//${window.location.host}/echo', data) |
206 .then((xhr) { | 206 .then((xhr) { |
207 expect(xhr.responseText, encodedData); | 207 expect(xhr.responseText, encodedData); |
208 }); | 208 }); |
209 }); | 209 }); |
210 }); | 210 }); |
211 | 211 |
212 group('xhr_requestBlob', () { | 212 group('xhr_requestBlob', () { |
213 test('XHR.request responseType blob', () { | 213 test('XHR.request responseType blob', () { |
214 if (Platform.supportsTypedData) { | 214 if (Platform.supportsTypedData) { |
215 return HttpRequest.request(url, responseType: 'blob').then( | 215 return HttpRequest.request(url, responseType: 'blob').then((xhr) { |
216 (xhr) { | 216 expect(xhr.status, equals(200)); |
217 expect(xhr.status, equals(200)); | 217 var blob = xhr.response; |
218 var blob = xhr.response; | 218 expect(blob is Blob, isTrue); |
219 expect(blob is Blob, isTrue); | 219 expect(blob, isNotNull); |
220 expect(blob, isNotNull); | 220 }); |
221 }); | |
222 } | 221 } |
223 }); | 222 }); |
224 }); | 223 }); |
225 | 224 |
226 group('json', () { | 225 group('json', () { |
227 test('xhr responseType json', () { | 226 test('xhr responseType json', () { |
228 var url = '${window.location.protocol}//${window.location.host}/echo'; | 227 var url = '${window.location.protocol}//${window.location.host}/echo'; |
229 var data = { | 228 var data = { |
230 'key': 'value', | 229 'key': 'value', |
231 'a': 'b', | 230 'a': 'b', |
232 'one': 2, | 231 'one': 2, |
233 }; | 232 }; |
234 | 233 |
235 HttpRequest.request(url, | 234 HttpRequest |
236 method: 'POST', | 235 .request(url, |
237 sendData: JSON.encode(data), | 236 method: 'POST', sendData: JSON.encode(data), responseType: 'json') |
238 responseType: 'json').then( | 237 .then(expectAsync((xhr) { |
239 expectAsync((xhr) { | 238 expect(xhr.status, equals(200)); |
240 expect(xhr.status, equals(200)); | 239 var json = xhr.response; |
241 var json = xhr.response; | 240 expect(json, equals(data)); |
242 expect(json, equals(data)); | 241 })); |
243 })); | |
244 }); | 242 }); |
245 }); | 243 }); |
246 | 244 |
247 group('headers', () { | 245 group('headers', () { |
248 test('xhr responseHeaders', () { | 246 test('xhr responseHeaders', () { |
249 return HttpRequest.request(url).then( | 247 return HttpRequest.request(url).then((xhr) { |
250 (xhr) { | 248 var contentTypeHeader = xhr.responseHeaders['content-type']; |
251 var contentTypeHeader = xhr.responseHeaders['content-type']; | 249 expect(contentTypeHeader, isNotNull); |
252 expect(contentTypeHeader, isNotNull); | 250 // Should be like: 'text/plain; charset=utf-8' |
253 // Should be like: 'text/plain; charset=utf-8' | 251 expect(contentTypeHeader.contains('text/plain'), isTrue); |
254 expect(contentTypeHeader.contains('text/plain'), isTrue); | 252 expect(contentTypeHeader.contains('charset=utf-8'), isTrue); |
255 expect(contentTypeHeader.contains('charset=utf-8'), isTrue); | 253 }); |
256 }); | |
257 }); | 254 }); |
258 }); | 255 }); |
259 } | 256 } |
OLD | NEW |