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 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import '../../pkg/unittest/lib/html_config.dart'; | 7 import '../../pkg/unittest/lib/html_config.dart'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 import 'dart:json' as json; |
9 | 10 |
10 main() { | 11 main() { |
11 useHtmlConfiguration(); | 12 useHtmlConfiguration(); |
| 13 var url = "../../../../tests/html/xhr_cross_origin_data.txt"; |
| 14 |
| 15 void validate200Response(xhr) { |
| 16 expect(xhr.status, equals(200)); |
| 17 var data = json.parse(xhr.response); |
| 18 expect(data, contains('feed')); |
| 19 expect(data['feed'], contains('entry')); |
| 20 expect(data, isMap); |
| 21 } |
| 22 |
| 23 void validate404(xhr) { |
| 24 expect(xhr.status, equals(404)); |
| 25 expect(xhr.responseText, equals('')); |
| 26 } |
12 | 27 |
13 test('XHR No file', () { | 28 test('XHR No file', () { |
14 HttpRequest xhr = new HttpRequest(); | 29 HttpRequest xhr = new HttpRequest(); |
15 xhr.open("GET", "NonExistingFile", true); | 30 xhr.open("GET", "NonExistingFile", true); |
16 xhr.on.readyStateChange.add(expectAsync1((event) { | 31 xhr.on.readyStateChange.add(expectAsyncUntil1((event) { |
17 if (xhr.readyState == HttpRequest.DONE) { | 32 if (xhr.readyState == HttpRequest.DONE) { |
18 expect(xhr.status, equals(0)); | 33 validate404(xhr); |
19 expect(xhr.responseText, equals('')); | |
20 } | 34 } |
21 })); | 35 }, () => xhr.readyState == HttpRequest.DONE)); |
| 36 xhr.send(); |
| 37 }); |
| 38 |
| 39 test('XHR file', () { |
| 40 var xhr = new HttpRequest(); |
| 41 xhr.open('GET', url, true); |
| 42 xhr.on.readyStateChange.add(expectAsyncUntil1((e) { |
| 43 if (xhr.readyState == HttpRequest.DONE) { |
| 44 validate200Response(xhr); |
| 45 } |
| 46 }, () => xhr.readyState == HttpRequest.DONE)); |
22 xhr.send(); | 47 xhr.send(); |
23 }); | 48 }); |
24 | 49 |
25 test('XHR.get No file', () { | 50 test('XHR.get No file', () { |
26 new HttpRequest.get("NonExistingFile", expectAsync1((xhr) { | 51 new HttpRequest.get("NonExistingFile", expectAsync1((xhr) { |
27 expect(xhr.readyState, equals(HttpRequest.DONE)); | 52 expect(xhr.readyState, equals(HttpRequest.DONE)); |
28 expect(xhr.status, equals(0)); | 53 validate404(xhr); |
29 expect(xhr.responseText, equals('')); | 54 })); |
| 55 }); |
| 56 |
| 57 test('XHR.get file', () { |
| 58 var xhr = new HttpRequest.get(url, expectAsync1((event) { |
| 59 expect(event.readyState, equals(HttpRequest.DONE)); |
| 60 validate200Response(event); |
| 61 })); |
| 62 }); |
| 63 |
| 64 test('XHR.getWithCredentials No file', () { |
| 65 new HttpRequest.getWithCredentials("NonExistingFile", expectAsync1((xhr) { |
| 66 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 67 validate404(xhr); |
| 68 })); |
| 69 }); |
| 70 |
| 71 test('XHR.getWithCredentials file', () { |
| 72 new HttpRequest.getWithCredentials(url, expectAsync1((xhr) { |
| 73 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 74 validate200Response(xhr); |
30 })); | 75 })); |
31 }); | 76 }); |
32 } | 77 } |
OLD | NEW |