OLD | NEW |
---|---|
1 <script src="test-helpers.js?pipe=sub"></script> | 1 <script src="test-helpers.js?pipe=sub"></script> |
2 <script> | 2 <script> |
3 var path = base_path() + 'fetch-access-control.php'; | 3 var path = base_path() + 'fetch-access-control.php'; |
4 var host_info = get_host_info(); | 4 var host_info = get_host_info(); |
5 var SUCCESS = 'SUCCESS'; | |
6 var FAIL = 'FAIL'; | |
5 | 7 |
6 function create_success_test_promise(url, with_credentials) { | 8 function create_test_case_promise(url, with_credentials) { |
7 return new Promise(function(resolve, reject) { | 9 return new Promise(function(resolve) { |
8 var xhr = new XMLHttpRequest(); | 10 var xhr = new XMLHttpRequest(); |
9 xhr.onload = resolve; | 11 xhr.onload = function() { |
12 if (xhr.status == 200) { | |
13 resolve(SUCCESS); | |
14 } else { | |
15 resolve("STATUS" + xhr.status); | |
16 } | |
17 } | |
10 xhr.onerror = function() { | 18 xhr.onerror = function() { |
11 reject('XHR to ' + url + ' should succeed.'); | 19 resolve(FAIL); |
12 } | 20 } |
13 xhr.responseType = 'text'; | 21 xhr.responseType = 'text'; |
14 xhr.withCredentials = with_credentials; | 22 xhr.withCredentials = with_credentials; |
15 xhr.open('GET', url, true); | 23 xhr.open('GET', url, true); |
16 xhr.send(); | 24 xhr.send(); |
17 }); | 25 }); |
18 } | 26 } |
19 | 27 |
20 function create_failure_test_promise(url, with_credentials) { | 28 |
29 function create_test_promise(url, with_credentials, expected_result) { | |
21 return new Promise(function(resolve, reject) { | 30 return new Promise(function(resolve, reject) { |
22 var xhr = new XMLHttpRequest(); | 31 create_test_case_promise(url, with_credentials) |
23 xhr.onload = function() { | 32 .then(function(result) { |
24 reject('XHR to ' + url + ' should fail.'); | 33 if (result == expected_result) { |
34 resolve(); | |
35 } else { | |
36 reject('Result of url:' + url + ' ' + | |
37 ' with_credentials: ' + with_credentials + ' must be ' + | |
38 expected_result + ' but ' + result); | |
39 } | |
40 }) | |
41 }); | |
42 } | |
43 | |
44 function create_serial_promise(test_case) { | |
45 return new Promise(function(resolve, reject) { | |
yhirano
2014/10/08 09:31:39
You can write like this:
function create_serial_p
horo
2014/10/08 10:02:46
Done.
| |
46 var func = function(i) { | |
47 if (i == test_case.length) { | |
48 resolve(); | |
49 return; | |
50 } | |
51 create_test_promise(test_case[i][0], | |
52 test_case[i][1], | |
53 test_case[i][2]) | |
54 .then(function() { | |
55 func(i + 1); | |
56 }) | |
57 .catch(function(e) { | |
58 reject(e); | |
59 }); | |
25 } | 60 } |
26 xhr.onerror = function() { | 61 func(0); |
27 resolve(); | |
28 } | |
29 xhr.responseType = 'text'; | |
30 xhr.withCredentials = with_credentials; | |
31 xhr.open('GET', url, true); | |
32 xhr.send(); | |
33 }); | 62 }); |
34 } | 63 } |
35 | 64 |
36 window.addEventListener('message', function(evt) { | 65 window.addEventListener('message', function(evt) { |
37 var port = evt.ports[0]; | 66 var port = evt.ports[0]; |
38 create_success_test_promise(host_info['HTTP_ORIGIN'] + path, false) | 67 var url = host_info['HTTP_ORIGIN'] + path; |
39 .then(function() { | 68 var remote_url = host_info['HTTP_REMOTE_ORIGIN'] + path; |
40 return create_failure_test_promise( | 69 // If the 4th value of the item of TEST_CASES is true, the test case outputs |
41 host_info['HTTP_REMOTE_ORIGIN'] + path, | 70 // warning messages. So such tests must be executed in serial to match the |
42 false); | 71 // expected output text. |
43 }) | 72 var TEST_CASES = [ |
44 .then(function() { | 73 // Reject tests |
45 return create_failure_test_promise('./dummy?reject', false); | 74 [url + '?reject', false, FAIL], |
46 }) | 75 [url + '?reject', true, FAIL], |
47 .then(function() { | 76 [remote_url + '?reject', false, FAIL], |
48 return create_failure_test_promise('./dummy?resolve-null', false); | 77 [remote_url + '?reject', true, FAIL], |
49 }) | 78 // Reject(resolve-null) tests |
50 .then(function() { | 79 [url + '?resolve-null', false, FAIL], |
51 return create_success_test_promise( | 80 [url + '?resolve-null', true, FAIL], |
52 './dummy?url=' + | 81 [remote_url + '?resolve-null', false, FAIL], |
53 encodeURIComponent(host_info['HTTP_ORIGIN'] + path), | 82 [remote_url + '?resolve-null', true, FAIL], |
54 false); | 83 // Fallback tests |
55 }) | 84 [url + '?ignore', false, SUCCESS], |
56 .then(function() { | 85 [url + '?ignore', true, SUCCESS], |
57 return create_failure_test_promise( | 86 [remote_url + '?ignore', false, FAIL, true], // Executed in serial. |
58 './dummy?mode=no-cors&url=' + | 87 [remote_url + '?ignore', true, FAIL, true], // Executed in serial. |
59 encodeURIComponent(host_info['HTTP_REMOTE_ORIGIN'] + path), | 88 [ |
60 false); | 89 remote_url + '?ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
61 }) | 90 false, SUCCESS |
62 .then(function() { | 91 ], |
63 return create_success_test_promise( | 92 [ |
64 './dummy?url=' + | 93 remote_url + '?ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
65 encodeURIComponent(host_info['HTTP_REMOTE_ORIGIN'] + path + | 94 true, FAIL, true // Executed in serial. |
66 '?ACAOrigin=' + host_info['HTTP_ORIGIN']), | 95 ], |
67 false); | 96 [ |
68 }) | 97 remote_url + '?ACAOrigin=' + host_info['HTTP_ORIGIN'] + |
69 .then(function() { | 98 '&ACACredentials=true&ignore', |
70 return create_failure_test_promise( | 99 true, SUCCESS |
71 './dummy?url=' + | 100 ], |
72 encodeURIComponent(host_info['HTTP_REMOTE_ORIGIN'] + path + | 101 // Credential test (fallback) |
73 '?ACAOrigin=' + host_info['HTTP_ORIGIN']), | 102 [url + '?Auth&ignore', false, SUCCESS], |
74 true); | 103 [url + '?Auth&ignore', true, SUCCESS], |
75 }) | 104 [remote_url + '?Auth&ignore', false, FAIL, true], // Executed in serial. |
76 .then(function() { | 105 [remote_url + '?Auth&ignore', true, FAIL, true], // Executed in serial. |
77 return create_success_test_promise( | 106 [ |
78 './dummy?url=' + | 107 remote_url + '?Auth&ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
79 encodeURIComponent( | 108 false, 'STATUS401' |
80 host_info['HTTP_REMOTE_ORIGIN'] + path + | 109 ], |
81 '?ACACredentials=true&ACAOrigin=' + host_info['HTTP_ORIGIN']), | 110 [ |
82 true); | 111 remote_url + '?Auth&ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
83 }) | 112 true, FAIL, true // Executed in serial. |
113 ], | |
114 [ | |
115 remote_url + '?Auth&ACAOrigin=' + host_info['HTTP_ORIGIN'] + | |
116 '&ACACredentials=true&ignore', | |
117 true, SUCCESS | |
118 ], | |
119 // Basic response | |
120 [ | |
121 url + '?mode=same-origin&url=' + encodeURIComponent(url), | |
122 false, SUCCESS | |
123 ], | |
124 [ | |
125 url + '?mode=same-origin&url=' + encodeURIComponent(url), | |
126 false, SUCCESS | |
127 ], | |
128 [ | |
129 remote_url + '?mode=same-origin&url=' + encodeURIComponent(url), | |
130 false, SUCCESS | |
131 ], | |
132 [ | |
133 remote_url + '?mode=same-origin&url=' + encodeURIComponent(url), | |
134 false, SUCCESS | |
135 ], | |
136 // Opaque response | |
137 [ | |
138 url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), | |
139 false, FAIL | |
140 ], | |
141 [ | |
142 url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), | |
143 false, FAIL | |
144 ], | |
145 [ | |
146 remote_url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), | |
147 false, FAIL | |
148 ], | |
149 [ | |
150 remote_url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), | |
151 false, FAIL | |
152 ], | |
153 // CORS response | |
154 [ | |
155 url + '?mode=cors&url=' + | |
156 encodeURIComponent(remote_url + '?ACAOrigin=' + | |
157 host_info['HTTP_ORIGIN']), | |
158 false, SUCCESS | |
159 ], | |
160 [ | |
161 url + '?mode=cors&url=' + | |
162 encodeURIComponent(remote_url + '?ACAOrigin=' + | |
163 host_info['HTTP_ORIGIN']), | |
164 true, SUCCESS | |
165 ], | |
166 [ | |
167 remote_url + '?mode=cors&url=' + | |
168 encodeURIComponent(remote_url + '?ACAOrigin=' + | |
169 host_info['HTTP_ORIGIN']), | |
170 false, SUCCESS | |
171 ], | |
172 [ | |
173 remote_url + | |
174 '?mode=cors&url=' + | |
175 encodeURIComponent(remote_url + '?ACAOrigin=' + | |
176 host_info['HTTP_ORIGIN']), | |
177 true, SUCCESS | |
178 ] | |
179 ]; | |
180 var promises = []; | |
181 var serial_tests = []; | |
182 for (var i = 0; i < TEST_CASES.length ; ++i) { | |
183 if (!TEST_CASES[i][3]) { | |
yhirano
2014/10/08 09:31:39
Why don't you run all test cases serially?
horo
2014/10/08 10:02:46
To reduce the execution time.
| |
184 promises.push(create_test_promise(TEST_CASES[i][0], | |
185 TEST_CASES[i][1], | |
186 TEST_CASES[i][2])); | |
187 } else { | |
188 serial_tests.push(TEST_CASES[i]); | |
189 } | |
190 } | |
191 promises.push(create_serial_promise(serial_tests)); | |
192 Promise.all(promises) | |
84 .then(function() { | 193 .then(function() { |
85 port.postMessage({results: 'finish'}); | 194 port.postMessage({results: 'finish'}); |
86 }) | 195 }) |
87 .catch(function(e) { | 196 .catch(function(e) { |
88 port.postMessage({results: 'failure:' + e}); | 197 port.postMessage({results: 'failure:' + e}); |
89 }); | 198 }); |
90 }, false); | 199 }, false); |
91 </script> | 200 </script> |
OLD | NEW |