OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library XHRTest; |
| 6 import 'dart:async'; |
| 7 import 'dart:convert'; |
| 8 import 'dart:html'; |
| 9 import 'dart:typed_data'; |
| 10 import 'package:unittest/html_individual_config.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 main() { |
| 14 useHtmlIndividualConfiguration(); |
| 15 // Cache blocker is a workaround for: |
| 16 // https://code.google.com/p/dart/issues/detail?id=11834 |
| 17 var cacheBlocker = new DateTime.now().millisecondsSinceEpoch; |
| 18 var url = '/root_dart/tests/html/xhr_cross_origin_data.txt?' |
| 19 'cacheBlock=$cacheBlocker'; |
| 20 |
| 21 void validate200Response(xhr) { |
| 22 expect(xhr.status, equals(200)); |
| 23 var data = JSON.decode(xhr.responseText); |
| 24 expect(data, contains('feed')); |
| 25 expect(data['feed'], contains('entry')); |
| 26 expect(data, isMap); |
| 27 } |
| 28 |
| 29 void validate404(xhr) { |
| 30 expect(xhr.status, equals(404)); |
| 31 // We cannot say much about xhr.responseText, most HTTP servers will |
| 32 // include an HTML page explaining the error to a human. |
| 33 String responseText = xhr.responseText; |
| 34 expect(responseText, isNotNull); |
| 35 } |
| 36 |
| 37 group('supported_onProgress', () { |
| 38 test('supported', () { |
| 39 expect(HttpRequest.supportsProgressEvent, isTrue); |
| 40 }); |
| 41 }); |
| 42 |
| 43 group('supported_onLoadEnd', () { |
| 44 test('supported', () { |
| 45 expect(HttpRequest.supportsLoadEndEvent, isTrue); |
| 46 }); |
| 47 }); |
| 48 |
| 49 group('supported_overrideMimeType', () { |
| 50 test('supported', () { |
| 51 expect(HttpRequest.supportsOverrideMimeType, isTrue); |
| 52 }); |
| 53 }); |
| 54 |
| 55 group('xhr', () { |
| 56 test('XHR No file', () { |
| 57 HttpRequest xhr = new HttpRequest(); |
| 58 xhr.open("GET", "NonExistingFile", async: true); |
| 59 xhr.onReadyStateChange.listen(expectAsyncUntil((event) { |
| 60 if (xhr.readyState == HttpRequest.DONE) { |
| 61 validate404(xhr); |
| 62 } |
| 63 }, () => xhr.readyState == HttpRequest.DONE)); |
| 64 xhr.send(); |
| 65 }); |
| 66 |
| 67 test('XHR_file', () { |
| 68 var loadEndCalled = false; |
| 69 |
| 70 var xhr = new HttpRequest(); |
| 71 xhr.open('GET', url, async: true); |
| 72 xhr.onReadyStateChange.listen(expectAsyncUntil((e) { |
| 73 if (xhr.readyState == HttpRequest.DONE) { |
| 74 validate200Response(xhr); |
| 75 |
| 76 Timer.run(expectAsync(() { |
| 77 expect(loadEndCalled, HttpRequest.supportsLoadEndEvent); |
| 78 })); |
| 79 } |
| 80 }, () => xhr.readyState == HttpRequest.DONE)); |
| 81 |
| 82 xhr.onLoadEnd.listen((ProgressEvent e) { |
| 83 loadEndCalled = true; |
| 84 }); |
| 85 xhr.send(); |
| 86 }); |
| 87 |
| 88 test('XHR.request No file', () { |
| 89 HttpRequest.request('NonExistingFile').then( |
| 90 (_) { fail('Request should not have succeeded.'); }, |
| 91 onError: expectAsync((error) { |
| 92 var xhr = error.target; |
| 93 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 94 validate404(xhr); |
| 95 })); |
| 96 }); |
| 97 |
| 98 test('XHR.request file', () { |
| 99 HttpRequest.request(url).then(expectAsync((xhr) { |
| 100 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 101 validate200Response(xhr); |
| 102 })); |
| 103 }); |
| 104 |
| 105 test('XHR.request onProgress', () { |
| 106 var progressCalled = false; |
| 107 HttpRequest.request(url, |
| 108 onProgress: (_) { |
| 109 progressCalled = true; |
| 110 }).then(expectAsync( |
| 111 (xhr) { |
| 112 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 113 expect(progressCalled, HttpRequest.supportsProgressEvent); |
| 114 validate200Response(xhr); |
| 115 })); |
| 116 }); |
| 117 |
| 118 test('XHR.request withCredentials No file', () { |
| 119 HttpRequest.request('NonExistingFile', withCredentials: true).then( |
| 120 (_) { fail('Request should not have succeeded.'); }, |
| 121 onError: expectAsync((error) { |
| 122 var xhr = error.target; |
| 123 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 124 validate404(xhr); |
| 125 })); |
| 126 }); |
| 127 |
| 128 test('XHR.request withCredentials file', () { |
| 129 HttpRequest.request(url, withCredentials: true).then(expectAsync((xhr) { |
| 130 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 131 validate200Response(xhr); |
| 132 })); |
| 133 }); |
| 134 |
| 135 test('XHR.getString file', () { |
| 136 HttpRequest.getString(url).then(expectAsync((str) {})); |
| 137 }); |
| 138 |
| 139 test('XHR.getString No file', () { |
| 140 HttpRequest.getString('NonExistingFile').then( |
| 141 (_) { fail('Succeeded for non-existing file.'); }, |
| 142 onError: expectAsync((error) { |
| 143 validate404(error.target); |
| 144 })); |
| 145 }); |
| 146 |
| 147 test('XHR.request responseType arraybuffer', () { |
| 148 if (Platform.supportsTypedData) { |
| 149 HttpRequest.request(url, responseType: 'arraybuffer', |
| 150 requestHeaders: {'Content-Type': 'text/xml'}).then( |
| 151 expectAsync((xhr) { |
| 152 expect(xhr.status, equals(200)); |
| 153 var byteBuffer = xhr.response; |
| 154 expect(byteBuffer, new isInstanceOf<ByteBuffer>()); |
| 155 expect(byteBuffer, isNotNull); |
| 156 })); |
| 157 } |
| 158 }); |
| 159 |
| 160 test('overrideMimeType', () { |
| 161 var expectation = |
| 162 HttpRequest.supportsOverrideMimeType ? returnsNormally : throws; |
| 163 |
| 164 expect(() { |
| 165 HttpRequest.request(url, mimeType: 'application/binary'); |
| 166 }, expectation); |
| 167 }); |
| 168 |
| 169 if (Platform.supportsTypedData) { |
| 170 test('xhr upload', () { |
| 171 var xhr = new HttpRequest(); |
| 172 var progressCalled = false; |
| 173 xhr.upload.onProgress.listen((e) { |
| 174 progressCalled = true; |
| 175 }); |
| 176 |
| 177 xhr.open('POST', |
| 178 '${window.location.protocol}//${window.location.host}/echo'); |
| 179 |
| 180 // 10MB of payload data w/ a bit of data to make sure it |
| 181 // doesn't get compressed to nil. |
| 182 var data = new Uint8List(1 * 1024 * 1024); |
| 183 for (var i = 0; i < data.length; ++i) { |
| 184 data[i] = i & 0xFF; |
| 185 } |
| 186 xhr.send(new Uint8List.view(data.buffer)); |
| 187 |
| 188 return xhr.onLoad.first.then((_) { |
| 189 expect(progressCalled, isTrue, reason: 'onProgress should be fired'); |
| 190 }); |
| 191 }); |
| 192 } |
| 193 |
| 194 test('xhr postFormData', () { |
| 195 var data = { 'name': 'John', 'time': '2 pm' }; |
| 196 |
| 197 var parts = []; |
| 198 for (var key in data.keys) { |
| 199 parts.add('${Uri.encodeQueryComponent(key)}=' |
| 200 '${Uri.encodeQueryComponent(data[key])}'); |
| 201 } |
| 202 var encodedData = parts.join('&'); |
| 203 |
| 204 return HttpRequest.postFormData( |
| 205 '${window.location.protocol}//${window.location.host}/echo', data) |
| 206 .then((xhr) { |
| 207 expect(xhr.responseText, encodedData); |
| 208 }); |
| 209 }); |
| 210 }); |
| 211 |
| 212 group('xhr_requestBlob', () { |
| 213 test('XHR.request responseType blob', () { |
| 214 if (Platform.supportsTypedData) { |
| 215 return HttpRequest.request(url, responseType: 'blob').then( |
| 216 (xhr) { |
| 217 expect(xhr.status, equals(200)); |
| 218 var blob = xhr.response; |
| 219 expect(blob is Blob, isTrue); |
| 220 expect(blob, isNotNull); |
| 221 }); |
| 222 } |
| 223 }); |
| 224 }); |
| 225 |
| 226 group('json', () { |
| 227 test('xhr responseType json', () { |
| 228 var url = '${window.location.protocol}//${window.location.host}/echo'; |
| 229 var data = { |
| 230 'key': 'value', |
| 231 'a': 'b', |
| 232 'one': 2, |
| 233 }; |
| 234 |
| 235 HttpRequest.request(url, |
| 236 method: 'POST', |
| 237 sendData: JSON.encode(data), |
| 238 responseType: 'json').then( |
| 239 expectAsync((xhr) { |
| 240 expect(xhr.status, equals(200)); |
| 241 var json = xhr.response; |
| 242 expect(json, equals(data)); |
| 243 })); |
| 244 }); |
| 245 }); |
| 246 |
| 247 group('headers', () { |
| 248 test('xhr responseHeaders', () { |
| 249 return HttpRequest.request(url).then( |
| 250 (xhr) { |
| 251 var contentTypeHeader = xhr.responseHeaders['content-type']; |
| 252 expect(contentTypeHeader, isNotNull); |
| 253 // Should be like: 'text/plain; charset=utf-8' |
| 254 expect(contentTypeHeader.contains('text/plain'), isTrue); |
| 255 expect(contentTypeHeader.contains('charset=utf-8'), isTrue); |
| 256 }); |
| 257 }); |
| 258 }); |
| 259 } |
OLD | NEW |