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_individual_config.dart'; | 7 import '../../pkg/unittest/lib/html_individual_config.dart'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 | 10 |
| 11 void fail(message) { |
| 12 guardAsync(() { |
| 13 expect(false, isTrue, reason: message); |
| 14 }); |
| 15 } |
| 16 |
11 main() { | 17 main() { |
12 useHtmlIndividualConfiguration(); | 18 useHtmlIndividualConfiguration(); |
13 var url = "/tests/html/xhr_cross_origin_data.txt"; | 19 var url = "/tests/html/xhr_cross_origin_data.txt"; |
14 | 20 |
15 void validate200Response(xhr) { | 21 void validate200Response(xhr) { |
16 expect(xhr.status, equals(200)); | 22 expect(xhr.status, equals(200)); |
17 var data = json.parse(xhr.response); | 23 var data = json.parse(xhr.response); |
18 expect(data, contains('feed')); | 24 expect(data, contains('feed')); |
19 expect(data['feed'], contains('entry')); | 25 expect(data['feed'], contains('entry')); |
20 expect(data, isMap); | 26 expect(data, isMap); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 } | 58 } |
53 }, () => xhr.readyState == HttpRequest.DONE)); | 59 }, () => xhr.readyState == HttpRequest.DONE)); |
54 | 60 |
55 xhr.onLoadEnd.listen(expectAsync1((ProgressEvent e) { | 61 xhr.onLoadEnd.listen(expectAsync1((ProgressEvent e) { |
56 expect(e.currentTarget, xhr); | 62 expect(e.currentTarget, xhr); |
57 expect(e.target, xhr); | 63 expect(e.target, xhr); |
58 })); | 64 })); |
59 xhr.send(); | 65 xhr.send(); |
60 }); | 66 }); |
61 | 67 |
62 test('XHR.get No file', () { | 68 test('XHR.request No file', () { |
63 new HttpRequest.get("NonExistingFile", expectAsync1((xhr) { | 69 HttpRequest.request('NonExistingFile').then( |
64 expect(xhr.readyState, equals(HttpRequest.DONE)); | 70 (_) { fail('Request should not have succeeded.'); }, |
65 validate404(xhr); | 71 onError: expectAsync1((e) { |
66 })); | 72 var xhr = e.error.target; |
| 73 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 74 validate404(xhr); |
| 75 })); |
67 }); | 76 }); |
68 | 77 |
69 test('XHR.get file', () { | 78 test('XHR.request file', () { |
70 var xhr = new HttpRequest.get(url, expectAsync1((event) { | 79 HttpRequest.request(url).then(expectAsync1((xhr) { |
71 expect(event.readyState, equals(HttpRequest.DONE)); | |
72 validate200Response(event); | |
73 })); | |
74 }); | |
75 | |
76 test('XHR.getWithCredentials No file', () { | |
77 new HttpRequest.getWithCredentials("NonExistingFile", expectAsync1((xhr) { | |
78 expect(xhr.readyState, equals(HttpRequest.DONE)); | |
79 validate404(xhr); | |
80 })); | |
81 }); | |
82 | |
83 test('XHR.getWithCredentials file', () { | |
84 new HttpRequest.getWithCredentials(url, expectAsync1((xhr) { | |
85 expect(xhr.readyState, equals(HttpRequest.DONE)); | 80 expect(xhr.readyState, equals(HttpRequest.DONE)); |
86 validate200Response(xhr); | 81 validate200Response(xhr); |
87 })); | 82 })); |
88 }); | 83 }); |
89 | 84 |
| 85 test('XHR.request onProgress', () { |
| 86 var progressCalled = false; |
| 87 HttpRequest.request(url, |
| 88 onProgress: (_) { |
| 89 progressCalled = true; |
| 90 }).then(expectAsync1( |
| 91 (xhr) { |
| 92 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 93 expect(progressCalled, isTrue); |
| 94 validate200Response(xhr); |
| 95 })); |
| 96 }); |
| 97 |
| 98 test('XHR.request withCredentials No file', () { |
| 99 HttpRequest.request('NonExistingFile', withCredentials: true).then( |
| 100 (_) { fail('Request should not have succeeded.'); }, |
| 101 onError: expectAsync1((e) { |
| 102 var xhr = e.error.target; |
| 103 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 104 validate404(xhr); |
| 105 })); |
| 106 }); |
| 107 |
| 108 test('XHR.request withCredentials file', () { |
| 109 HttpRequest.request(url, withCredentials: true).then(expectAsync1((xhr) { |
| 110 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 111 validate200Response(xhr); |
| 112 })); |
| 113 }); |
| 114 |
| 115 test('XHR.getString file', () { |
| 116 HttpRequest.getString(url).then(expectAsync1((str) {})); |
| 117 }); |
| 118 |
| 119 test('XHR.getString No file', () { |
| 120 HttpRequest.getString('NonExistingFile').then( |
| 121 (_) { fail('Succeeded for non-existing file.'); }, |
| 122 onError: expectAsync1((e) { |
| 123 validate404(e.error.target); |
| 124 })); |
| 125 }); |
| 126 |
| 127 test('XHR.request responseType', () { |
| 128 if (ArrayBuffer.supported) { |
| 129 HttpRequest.request(url, responseType: 'ArrayBuffer').then( |
| 130 expectAsync1((xhr) { |
| 131 validate200Response(xhr); |
| 132 var arrayBuffer = xhr.response; |
| 133 expect(arrayBuffer, new isInstanceOf<ArrayBuffer>()); |
| 134 expect(arrayBuffer, isNotNull); |
| 135 })); |
| 136 } |
| 137 }); |
| 138 |
90 test('HttpRequestProgressEvent', () { | 139 test('HttpRequestProgressEvent', () { |
91 var expectation = HttpRequestProgressEvent.supported ? | 140 var expectation = HttpRequestProgressEvent.supported ? |
92 returnsNormally : throws; | 141 returnsNormally : throws; |
93 expect(() { | 142 expect(() { |
94 var event = new Event.eventType('XMLHttpRequestProgressEvent', ''); | 143 var event = new Event.eventType('XMLHttpRequestProgressEvent', ''); |
95 expect(event is HttpRequestProgressEvent, isTrue); | 144 expect(event is HttpRequestProgressEvent, isTrue); |
96 }, expectation); | 145 }, expectation); |
97 }); | 146 }); |
98 }); | 147 }); |
99 } | 148 } |
OLD | NEW |