| 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 | |
| 17 main() { | 11 main() { |
| 18 useHtmlIndividualConfiguration(); | 12 useHtmlIndividualConfiguration(); |
| 19 var url = "/tests/html/xhr_cross_origin_data.txt"; | 13 var url = "/tests/html/xhr_cross_origin_data.txt"; |
| 20 | 14 |
| 21 void validate200Response(xhr) { | 15 void validate200Response(xhr) { |
| 22 expect(xhr.status, equals(200)); | 16 expect(xhr.status, equals(200)); |
| 23 var data = json.parse(xhr.response); | 17 var data = json.parse(xhr.response); |
| 24 expect(data, contains('feed')); | 18 expect(data, contains('feed')); |
| 25 expect(data['feed'], contains('entry')); | 19 expect(data['feed'], contains('entry')); |
| 26 expect(data, isMap); | 20 expect(data, isMap); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 } | 52 } |
| 59 }, () => xhr.readyState == HttpRequest.DONE)); | 53 }, () => xhr.readyState == HttpRequest.DONE)); |
| 60 | 54 |
| 61 xhr.onLoadEnd.listen(expectAsync1((ProgressEvent e) { | 55 xhr.onLoadEnd.listen(expectAsync1((ProgressEvent e) { |
| 62 expect(e.currentTarget, xhr); | 56 expect(e.currentTarget, xhr); |
| 63 expect(e.target, xhr); | 57 expect(e.target, xhr); |
| 64 })); | 58 })); |
| 65 xhr.send(); | 59 xhr.send(); |
| 66 }); | 60 }); |
| 67 | 61 |
| 68 test('XHR.request No file', () { | 62 test('XHR.get No file', () { |
| 69 HttpRequest.request('NonExistingFile').then( | 63 new HttpRequest.get("NonExistingFile", expectAsync1((xhr) { |
| 70 (_) { fail('Request should not have succeeded.'); }, | 64 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 71 onError: expectAsync1((e) { | 65 validate404(xhr); |
| 72 var xhr = e.error.target; | 66 })); |
| 73 expect(xhr.readyState, equals(HttpRequest.DONE)); | |
| 74 validate404(xhr); | |
| 75 })); | |
| 76 }); | 67 }); |
| 77 | 68 |
| 78 test('XHR.request file', () { | 69 test('XHR.get file', () { |
| 79 HttpRequest.request(url).then(expectAsync1((xhr) { | 70 var xhr = new HttpRequest.get(url, expectAsync1((event) { |
| 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) { |
| 80 expect(xhr.readyState, equals(HttpRequest.DONE)); | 85 expect(xhr.readyState, equals(HttpRequest.DONE)); |
| 81 validate200Response(xhr); | 86 validate200Response(xhr); |
| 82 })); | 87 })); |
| 83 }); | 88 }); |
| 84 | 89 |
| 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 | |
| 139 test('HttpRequestProgressEvent', () { | 90 test('HttpRequestProgressEvent', () { |
| 140 var expectation = HttpRequestProgressEvent.supported ? | 91 var expectation = HttpRequestProgressEvent.supported ? |
| 141 returnsNormally : throws; | 92 returnsNormally : throws; |
| 142 expect(() { | 93 expect(() { |
| 143 var event = new Event.eventType('XMLHttpRequestProgressEvent', ''); | 94 var event = new Event.eventType('XMLHttpRequestProgressEvent', ''); |
| 144 expect(event is HttpRequestProgressEvent, isTrue); | 95 expect(event is HttpRequestProgressEvent, isTrue); |
| 145 }, expectation); | 96 }, expectation); |
| 146 }); | 97 }); |
| 147 }); | 98 }); |
| 148 } | 99 } |
| OLD | NEW |