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 XHRCrossOriginTest; | 5 library XHRCrossOriginTest; |
| 6 |
6 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
7 import 'package:unittest/html_individual_config.dart'; | 8 import 'package:unittest/html_individual_config.dart'; |
8 import 'dart:html'; | 9 import 'dart:html'; |
9 import "dart:convert"; | 10 import "dart:convert"; |
10 | 11 |
11 /** | 12 /** |
12 * Examine the value of "crossOriginPort" as passed in from the url from | 13 * Examine the value of "crossOriginPort" as passed in from the url from |
13 * [window.location] to determine what the cross-origin port is for | 14 * [window.location] to determine what the cross-origin port is for |
14 * this test. | 15 * this test. |
15 */ | 16 */ |
16 // TODO(efortuna): If we need to use this function frequently, make a | 17 // TODO(efortuna): If we need to use this function frequently, make a |
17 // url_analyzer library that is part of test.dart that these tests can import. | 18 // url_analyzer library that is part of test.dart that these tests can import. |
18 int get crossOriginPort { | 19 int get crossOriginPort { |
19 var searchUrl = window.location.search; | 20 var searchUrl = window.location.search; |
20 var crossOriginStr = 'crossOriginPort='; | 21 var crossOriginStr = 'crossOriginPort='; |
21 var index = searchUrl.indexOf(crossOriginStr); | 22 var index = searchUrl.indexOf(crossOriginStr); |
22 var nextArg = searchUrl.indexOf('&', index); | 23 var nextArg = searchUrl.indexOf('&', index); |
23 return int.parse(searchUrl.substring(index + crossOriginStr.length, | 24 return int.parse(searchUrl.substring(index + crossOriginStr.length, |
24 nextArg == -1 ? searchUrl.length : nextArg)); | 25 nextArg == -1 ? searchUrl.length : nextArg)); |
25 } | 26 } |
26 | 27 |
27 main() { | 28 main() { |
28 useHtmlIndividualConfiguration(); | 29 useHtmlIndividualConfiguration(); |
29 | 30 |
30 group('supported', () { | 31 group('supported', () { |
31 test('supported', () { | 32 test('supported', () { |
32 expect(HttpRequest.supportsCrossOrigin, isTrue); | 33 expect(HttpRequest.supportsCrossOrigin, isTrue); |
33 }); | 34 }); |
34 }); | 35 }); |
35 | 36 |
36 group('functional', () { | 37 group('functional', () { |
37 | |
38 var port = crossOriginPort; | 38 var port = crossOriginPort; |
39 var host = '${window.location.protocol}//${window.location.hostname}:$port'; | 39 var host = '${window.location.protocol}//${window.location.hostname}:$port'; |
40 | 40 |
41 test('XHR.get Cross-domain', () { | 41 test('XHR.get Cross-domain', () { |
42 var gotError = false; | 42 var gotError = false; |
43 var url = '$host/root_dart/tests/html/xhr_cross_origin_data.txt'; | 43 var url = '$host/root_dart/tests/html/xhr_cross_origin_data.txt'; |
44 return HttpRequest.request(url).then((xhr) { | 44 return HttpRequest.request(url).then((xhr) { |
45 var data = JSON.decode(xhr.response); | 45 var data = JSON.decode(xhr.response); |
46 expect(data, contains('feed')); | 46 expect(data, contains('feed')); |
47 expect(data['feed'], contains('entry')); | 47 expect(data['feed'], contains('entry')); |
(...skipping 26 matching lines...) Expand all Loading... |
74 expect(gotError, isTrue); | 74 expect(gotError, isTrue); |
75 }); | 75 }); |
76 }); | 76 }); |
77 | 77 |
78 // Skip the rest if not supported. | 78 // Skip the rest if not supported. |
79 if (!HttpRequest.supportsCrossOrigin) { | 79 if (!HttpRequest.supportsCrossOrigin) { |
80 return; | 80 return; |
81 } | 81 } |
82 | 82 |
83 test('XHR Cross-domain', () { | 83 test('XHR Cross-domain', () { |
84 var url = '$host/root_dart/tests/html/xhr_cross_origin_data.txt'; | 84 var url = '$host/root_dart/tests/html/xhr_cross_origin_data.txt'; |
85 var xhr = new HttpRequest(); | 85 var xhr = new HttpRequest(); |
86 xhr.open('GET', url, async: true); | 86 xhr.open('GET', url, async: true); |
87 var validate = expectAsync((data) { | 87 var validate = expectAsync((data) { |
88 expect(data, contains('feed')); | 88 expect(data, contains('feed')); |
89 expect(data['feed'], contains('entry')); | 89 expect(data['feed'], contains('entry')); |
90 expect(data, isMap); | 90 expect(data, isMap); |
91 }); | 91 }); |
92 xhr.onReadyStateChange.listen((e) { | 92 xhr.onReadyStateChange.listen((e) { |
93 if (xhr.readyState == HttpRequest.DONE) { | 93 if (xhr.readyState == HttpRequest.DONE) { |
94 validate(JSON.decode(xhr.response)); | 94 validate(JSON.decode(xhr.response)); |
95 } | 95 } |
96 }); | 96 }); |
97 xhr.send(); | 97 xhr.send(); |
98 }); | 98 }); |
99 | 99 |
100 test('XHR.getWithCredentials Cross-domain', () { | 100 test('XHR.getWithCredentials Cross-domain', () { |
101 var url = '$host/root_dart/tests/html/xhr_cross_origin_data.txt'; | 101 var url = '$host/root_dart/tests/html/xhr_cross_origin_data.txt'; |
102 return HttpRequest.request(url, withCredentials: true).then((xhr) { | 102 return HttpRequest.request(url, withCredentials: true).then((xhr) { |
103 var data = JSON.decode(xhr.response); | 103 var data = JSON.decode(xhr.response); |
104 expect(data, contains('feed')); | 104 expect(data, contains('feed')); |
105 expect(data['feed'], contains('entry')); | 105 expect(data['feed'], contains('entry')); |
106 expect(data, isMap); | 106 expect(data, isMap); |
107 }); | 107 }); |
108 }); | 108 }); |
109 }); | 109 }); |
110 } | 110 } |
OLD | NEW |