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 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 import 'package:unittest/html_individual_config.dart'; | 10 import 'package:unittest/html_individual_config.dart'; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 test('XHR file', () { | 70 test('XHR file', () { |
71 var loadEndCalled = false; | 71 var loadEndCalled = false; |
72 | 72 |
73 var xhr = new HttpRequest(); | 73 var xhr = new HttpRequest(); |
74 xhr.open('GET', url, async: true); | 74 xhr.open('GET', url, async: true); |
75 xhr.onReadyStateChange.listen(expectAsyncUntil1((e) { | 75 xhr.onReadyStateChange.listen(expectAsyncUntil1((e) { |
76 if (xhr.readyState == HttpRequest.DONE) { | 76 if (xhr.readyState == HttpRequest.DONE) { |
77 validate200Response(xhr); | 77 validate200Response(xhr); |
78 | 78 |
79 Timer.run(expectAsync0(() { | 79 Timer.run(expectAsync(() { |
80 expect(loadEndCalled, HttpRequest.supportsLoadEndEvent); | 80 expect(loadEndCalled, HttpRequest.supportsLoadEndEvent); |
81 })); | 81 })); |
82 } | 82 } |
83 }, () => xhr.readyState == HttpRequest.DONE)); | 83 }, () => xhr.readyState == HttpRequest.DONE)); |
84 | 84 |
85 xhr.onLoadEnd.listen((ProgressEvent e) { | 85 xhr.onLoadEnd.listen((ProgressEvent e) { |
86 loadEndCalled = true; | 86 loadEndCalled = true; |
87 }); | 87 }); |
88 xhr.send(); | 88 xhr.send(); |
89 }); | 89 }); |
90 | 90 |
91 test('XHR.request No file', () { | 91 test('XHR.request No file', () { |
92 HttpRequest.request('NonExistingFile').then( | 92 HttpRequest.request('NonExistingFile').then( |
93 (_) { fail('Request should not have succeeded.'); }, | 93 (_) { fail('Request should not have succeeded.'); }, |
94 onError: expectAsync1((error) { | 94 onError: expectAsync((error) { |
95 var xhr = error.target; | 95 var xhr = error.target; |
96 expect(xhr.readyState, equals(HttpRequest.DONE)); | 96 expect(xhr.readyState, equals(HttpRequest.DONE)); |
97 validate404(xhr); | 97 validate404(xhr); |
98 })); | 98 })); |
99 }); | 99 }); |
100 | 100 |
101 test('XHR.request file', () { | 101 test('XHR.request file', () { |
102 HttpRequest.request(url).then(expectAsync1((xhr) { | 102 HttpRequest.request(url).then(expectAsync((xhr) { |
103 expect(xhr.readyState, equals(HttpRequest.DONE)); | 103 expect(xhr.readyState, equals(HttpRequest.DONE)); |
104 validate200Response(xhr); | 104 validate200Response(xhr); |
105 })); | 105 })); |
106 }); | 106 }); |
107 | 107 |
108 test('XHR.request onProgress', () { | 108 test('XHR.request onProgress', () { |
109 var progressCalled = false; | 109 var progressCalled = false; |
110 HttpRequest.request(url, | 110 HttpRequest.request(url, |
111 onProgress: (_) { | 111 onProgress: (_) { |
112 progressCalled = true; | 112 progressCalled = true; |
113 }).then(expectAsync1( | 113 }).then(expectAsync( |
114 (xhr) { | 114 (xhr) { |
115 expect(xhr.readyState, equals(HttpRequest.DONE)); | 115 expect(xhr.readyState, equals(HttpRequest.DONE)); |
116 expect(progressCalled, HttpRequest.supportsProgressEvent); | 116 expect(progressCalled, HttpRequest.supportsProgressEvent); |
117 validate200Response(xhr); | 117 validate200Response(xhr); |
118 })); | 118 })); |
119 }); | 119 }); |
120 | 120 |
121 test('XHR.request withCredentials No file', () { | 121 test('XHR.request withCredentials No file', () { |
122 HttpRequest.request('NonExistingFile', withCredentials: true).then( | 122 HttpRequest.request('NonExistingFile', withCredentials: true).then( |
123 (_) { fail('Request should not have succeeded.'); }, | 123 (_) { fail('Request should not have succeeded.'); }, |
124 onError: expectAsync1((error) { | 124 onError: expectAsync((error) { |
125 var xhr = error.target; | 125 var xhr = error.target; |
126 expect(xhr.readyState, equals(HttpRequest.DONE)); | 126 expect(xhr.readyState, equals(HttpRequest.DONE)); |
127 validate404(xhr); | 127 validate404(xhr); |
128 })); | 128 })); |
129 }); | 129 }); |
130 | 130 |
131 test('XHR.request withCredentials file', () { | 131 test('XHR.request withCredentials file', () { |
132 HttpRequest.request(url, withCredentials: true).then(expectAsync1((xhr) { | 132 HttpRequest.request(url, withCredentials: true).then(expectAsync((xhr) { |
133 expect(xhr.readyState, equals(HttpRequest.DONE)); | 133 expect(xhr.readyState, equals(HttpRequest.DONE)); |
134 validate200Response(xhr); | 134 validate200Response(xhr); |
135 })); | 135 })); |
136 }); | 136 }); |
137 | 137 |
138 test('XHR.getString file', () { | 138 test('XHR.getString file', () { |
139 HttpRequest.getString(url).then(expectAsync1((str) {})); | 139 HttpRequest.getString(url).then(expectAsync((str) {})); |
140 }); | 140 }); |
141 | 141 |
142 test('XHR.getString No file', () { | 142 test('XHR.getString No file', () { |
143 HttpRequest.getString('NonExistingFile').then( | 143 HttpRequest.getString('NonExistingFile').then( |
144 (_) { fail('Succeeded for non-existing file.'); }, | 144 (_) { fail('Succeeded for non-existing file.'); }, |
145 onError: expectAsync1((error) { | 145 onError: expectAsync((error) { |
146 validate404(error.target); | 146 validate404(error.target); |
147 })); | 147 })); |
148 }); | 148 }); |
149 | 149 |
150 test('XHR.request responseType arraybuffer', () { | 150 test('XHR.request responseType arraybuffer', () { |
151 if (Platform.supportsTypedData) { | 151 if (Platform.supportsTypedData) { |
152 HttpRequest.request(url, responseType: 'arraybuffer', | 152 HttpRequest.request(url, responseType: 'arraybuffer', |
153 requestHeaders: {'Content-Type': 'text/xml'}).then( | 153 requestHeaders: {'Content-Type': 'text/xml'}).then( |
154 expectAsync1((xhr) { | 154 expectAsync((xhr) { |
155 expect(xhr.status, equals(200)); | 155 expect(xhr.status, equals(200)); |
156 var byteBuffer = xhr.response; | 156 var byteBuffer = xhr.response; |
157 expect(byteBuffer, new isInstanceOf<ByteBuffer>()); | 157 expect(byteBuffer, new isInstanceOf<ByteBuffer>()); |
158 expect(byteBuffer, isNotNull); | 158 expect(byteBuffer, isNotNull); |
159 })); | 159 })); |
160 } | 160 } |
161 }); | 161 }); |
162 | 162 |
163 test('overrideMimeType', () { | 163 test('overrideMimeType', () { |
164 var expectation = | 164 var expectation = |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 var data = { | 232 var data = { |
233 'key': 'value', | 233 'key': 'value', |
234 'a': 'b', | 234 'a': 'b', |
235 'one': 2, | 235 'one': 2, |
236 }; | 236 }; |
237 | 237 |
238 HttpRequest.request(url, | 238 HttpRequest.request(url, |
239 method: 'POST', | 239 method: 'POST', |
240 sendData: JSON.encode(data), | 240 sendData: JSON.encode(data), |
241 responseType: 'json').then( | 241 responseType: 'json').then( |
242 expectAsync1((xhr) { | 242 expectAsync((xhr) { |
243 expect(xhr.status, equals(200)); | 243 expect(xhr.status, equals(200)); |
244 var json = xhr.response; | 244 var json = xhr.response; |
245 expect(json, equals(data)); | 245 expect(json, equals(data)); |
246 })); | 246 })); |
247 }); | 247 }); |
248 }); | 248 }); |
249 | 249 |
250 group('headers', () { | 250 group('headers', () { |
251 test('xhr responseHeaders', () { | 251 test('xhr responseHeaders', () { |
252 return HttpRequest.request(url).then( | 252 return HttpRequest.request(url).then( |
253 (xhr) { | 253 (xhr) { |
254 var serverHeader = xhr.responseHeaders['server']; | 254 var serverHeader = xhr.responseHeaders['server']; |
255 expect(serverHeader, isNotNull); | 255 expect(serverHeader, isNotNull); |
256 // Should be like: 'Dart/0.1 (dart:io)' | 256 // Should be like: 'Dart/0.1 (dart:io)' |
257 expect(serverHeader.startsWith('Dart/'), isTrue); | 257 expect(serverHeader.startsWith('Dart/'), isTrue); |
258 }); | 258 }); |
259 }); | 259 }); |
260 }); | 260 }); |
261 } | 261 } |
OLD | NEW |