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 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:convert"; |
10 | 10 |
11 /** | 11 /** |
12 * Examine the value of "crossOriginPort" as passed in from the url from | 12 * Examine the value of "crossOriginPort" as passed in from the url from |
13 * [window.location] to determine what the cross-origin port is for | 13 * [window.location] to determine what the cross-origin port is for |
14 * this test. | 14 * this test. |
15 */ | 15 */ |
16 // TODO(efortuna): If we need to use this function frequently, make a | 16 // 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. | 17 // url_analyzer library that is part of test.dart that these tests can import. |
18 int get crossOriginPort { | 18 int get crossOriginPort { |
19 var searchUrl = window.location.search; | 19 var searchUrl = window.location.search; |
(...skipping 15 matching lines...) Expand all Loading... |
35 | 35 |
36 group('functional', () { | 36 group('functional', () { |
37 | 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.parse(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')); |
48 expect(data, isMap); | 48 expect(data, isMap); |
49 }).catchError((error) {}, test: (error) { | 49 }).catchError((error) {}, test: (error) { |
50 gotError = true; | 50 gotError = true; |
51 // Consume errors when not supporting cross origin. | 51 // Consume errors when not supporting cross origin. |
52 return !HttpRequest.supportsCrossOrigin; | 52 return !HttpRequest.supportsCrossOrigin; |
53 }).whenComplete(() { | 53 }).whenComplete(() { |
54 // Expect that we got an error when cross origin is not supported. | 54 // Expect that we got an error when cross origin is not supported. |
55 expect(gotError, !HttpRequest.supportsCrossOrigin); | 55 expect(gotError, !HttpRequest.supportsCrossOrigin); |
(...skipping 10 matching lines...) Expand all Loading... |
66 var xhr = new HttpRequest(); | 66 var xhr = new HttpRequest(); |
67 xhr.open('GET', url, async: true); | 67 xhr.open('GET', url, async: true); |
68 var validate = expectAsync1((data) { | 68 var validate = expectAsync1((data) { |
69 expect(data, contains('feed')); | 69 expect(data, contains('feed')); |
70 expect(data['feed'], contains('entry')); | 70 expect(data['feed'], contains('entry')); |
71 expect(data, isMap); | 71 expect(data, isMap); |
72 }); | 72 }); |
73 xhr.onReadyStateChange.listen((e) { | 73 xhr.onReadyStateChange.listen((e) { |
74 guardAsync(() { | 74 guardAsync(() { |
75 if (xhr.readyState == HttpRequest.DONE) { | 75 if (xhr.readyState == HttpRequest.DONE) { |
76 validate(json.parse(xhr.response)); | 76 validate(JSON.decode(xhr.response)); |
77 } | 77 } |
78 }); | 78 }); |
79 }); | 79 }); |
80 xhr.send(); | 80 xhr.send(); |
81 }); | 81 }); |
82 | 82 |
83 test('XHR.getWithCredentials Cross-domain', () { | 83 test('XHR.getWithCredentials 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 return HttpRequest.request(url, withCredentials: true).then((xhr) { | 85 return HttpRequest.request(url, withCredentials: true).then((xhr) { |
86 var data = json.parse(xhr.response); | 86 var data = JSON.decode(xhr.response); |
87 expect(data, contains('feed')); | 87 expect(data, contains('feed')); |
88 expect(data['feed'], contains('entry')); | 88 expect(data['feed'], contains('entry')); |
89 expect(data, isMap); | 89 expect(data, isMap); |
90 }); | 90 }); |
91 }); | 91 }); |
92 }); | 92 }); |
93 } | 93 } |
OLD | NEW |