| 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) { |
| 25 } | 34 resolve(); |
| 26 xhr.onerror = function() { | 35 } else { |
| 27 resolve(); | 36 reject('Result of url:' + url + ' ' + |
| 28 } | 37 ' with_credentials: ' + with_credentials + ' must be ' + |
| 29 xhr.responseType = 'text'; | 38 expected_result + ' but ' + result); |
| 30 xhr.withCredentials = with_credentials; | 39 } |
| 31 xhr.open('GET', url, true); | 40 }) |
| 32 xhr.send(); | |
| 33 }); | 41 }); |
| 34 } | 42 } |
| 35 | 43 |
| 44 function create_serial_promise(test_cases) { |
| 45 var promise = Promise.resolve(); |
| 46 test_cases.forEach(function(test_case) { |
| 47 promise = promise.then(function() { |
| 48 return create_test_promise(test_case[0], test_case[1], test_case[2]); |
| 49 }); |
| 50 }); |
| 51 return promise; |
| 52 } |
| 53 |
| 36 window.addEventListener('message', function(evt) { | 54 window.addEventListener('message', function(evt) { |
| 37 var port = evt.ports[0]; | 55 var port = evt.ports[0]; |
| 38 create_success_test_promise(host_info['HTTP_ORIGIN'] + path, false) | 56 var url = host_info['HTTP_ORIGIN'] + path; |
| 39 .then(function() { | 57 var remote_url = host_info['HTTP_REMOTE_ORIGIN'] + path; |
| 40 return create_failure_test_promise( | 58 // If the 4th value of the item of TEST_CASES is true, the test case outputs |
| 41 host_info['HTTP_REMOTE_ORIGIN'] + path, | 59 // warning messages. So such tests must be executed in serial to match the |
| 42 false); | 60 // expected output text. |
| 43 }) | 61 var TEST_CASES = [ |
| 44 .then(function() { | 62 // Reject tests |
| 45 return create_failure_test_promise('./dummy?reject', false); | 63 [url + '?reject', false, FAIL], |
| 46 }) | 64 [url + '?reject', true, FAIL], |
| 47 .then(function() { | 65 [remote_url + '?reject', false, FAIL], |
| 48 return create_failure_test_promise('./dummy?resolve-null', false); | 66 [remote_url + '?reject', true, FAIL], |
| 49 }) | 67 // Reject(resolve-null) tests |
| 50 .then(function() { | 68 [url + '?resolve-null', false, FAIL], |
| 51 return create_success_test_promise( | 69 [url + '?resolve-null', true, FAIL], |
| 52 './dummy?url=' + | 70 [remote_url + '?resolve-null', false, FAIL], |
| 53 encodeURIComponent(host_info['HTTP_ORIGIN'] + path), | 71 [remote_url + '?resolve-null', true, FAIL], |
| 54 false); | 72 // Fallback tests |
| 55 }) | 73 [url + '?ignore', false, SUCCESS], |
| 56 .then(function() { | 74 [url + '?ignore', true, SUCCESS], |
| 57 return create_failure_test_promise( | 75 [remote_url + '?ignore', false, FAIL, true], // Executed in serial. |
| 58 './dummy?mode=no-cors&url=' + | 76 [remote_url + '?ignore', true, FAIL, true], // Executed in serial. |
| 59 encodeURIComponent(host_info['HTTP_REMOTE_ORIGIN'] + path), | 77 [ |
| 60 false); | 78 remote_url + '?ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
| 61 }) | 79 false, SUCCESS |
| 62 .then(function() { | 80 ], |
| 63 return create_success_test_promise( | 81 [ |
| 64 './dummy?url=' + | 82 remote_url + '?ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
| 65 encodeURIComponent(host_info['HTTP_REMOTE_ORIGIN'] + path + | 83 true, FAIL, true // Executed in serial. |
| 66 '?ACAOrigin=' + host_info['HTTP_ORIGIN']), | 84 ], |
| 67 false); | 85 [ |
| 68 }) | 86 remote_url + '?ACAOrigin=' + host_info['HTTP_ORIGIN'] + |
| 69 .then(function() { | 87 '&ACACredentials=true&ignore', |
| 70 return create_failure_test_promise( | 88 true, SUCCESS |
| 71 './dummy?url=' + | 89 ], |
| 72 encodeURIComponent(host_info['HTTP_REMOTE_ORIGIN'] + path + | 90 // Credential test (fallback) |
| 73 '?ACAOrigin=' + host_info['HTTP_ORIGIN']), | 91 [url + '?Auth&ignore', false, SUCCESS], |
| 74 true); | 92 [url + '?Auth&ignore', true, SUCCESS], |
| 75 }) | 93 [remote_url + '?Auth&ignore', false, FAIL, true], // Executed in serial. |
| 76 .then(function() { | 94 [remote_url + '?Auth&ignore', true, FAIL, true], // Executed in serial. |
| 77 return create_success_test_promise( | 95 [ |
| 78 './dummy?url=' + | 96 remote_url + '?Auth&ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
| 79 encodeURIComponent( | 97 false, 'STATUS401' |
| 80 host_info['HTTP_REMOTE_ORIGIN'] + path + | 98 ], |
| 81 '?ACACredentials=true&ACAOrigin=' + host_info['HTTP_ORIGIN']), | 99 [ |
| 82 true); | 100 remote_url + '?Auth&ACAOrigin=' + host_info['HTTP_ORIGIN'] + '&ignore', |
| 83 }) | 101 true, FAIL, true // Executed in serial. |
| 102 ], |
| 103 [ |
| 104 remote_url + '?Auth&ACAOrigin=' + host_info['HTTP_ORIGIN'] + |
| 105 '&ACACredentials=true&ignore', |
| 106 true, SUCCESS |
| 107 ], |
| 108 // Basic response |
| 109 [ |
| 110 url + '?mode=same-origin&url=' + encodeURIComponent(url), |
| 111 false, SUCCESS |
| 112 ], |
| 113 [ |
| 114 url + '?mode=same-origin&url=' + encodeURIComponent(url), |
| 115 false, SUCCESS |
| 116 ], |
| 117 [ |
| 118 remote_url + '?mode=same-origin&url=' + encodeURIComponent(url), |
| 119 false, SUCCESS |
| 120 ], |
| 121 [ |
| 122 remote_url + '?mode=same-origin&url=' + encodeURIComponent(url), |
| 123 false, SUCCESS |
| 124 ], |
| 125 // Opaque response |
| 126 [ |
| 127 url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), |
| 128 false, FAIL |
| 129 ], |
| 130 [ |
| 131 url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), |
| 132 false, FAIL |
| 133 ], |
| 134 [ |
| 135 remote_url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), |
| 136 false, FAIL |
| 137 ], |
| 138 [ |
| 139 remote_url + '?mode=no-cors&url=' + encodeURIComponent(remote_url), |
| 140 false, FAIL |
| 141 ], |
| 142 // CORS response |
| 143 [ |
| 144 url + '?mode=cors&url=' + |
| 145 encodeURIComponent(remote_url + '?ACAOrigin=' + |
| 146 host_info['HTTP_ORIGIN']), |
| 147 false, SUCCESS |
| 148 ], |
| 149 [ |
| 150 url + '?mode=cors&url=' + |
| 151 encodeURIComponent(remote_url + '?ACAOrigin=' + |
| 152 host_info['HTTP_ORIGIN']), |
| 153 true, SUCCESS |
| 154 ], |
| 155 [ |
| 156 remote_url + '?mode=cors&url=' + |
| 157 encodeURIComponent(remote_url + '?ACAOrigin=' + |
| 158 host_info['HTTP_ORIGIN']), |
| 159 false, SUCCESS |
| 160 ], |
| 161 [ |
| 162 remote_url + |
| 163 '?mode=cors&url=' + |
| 164 encodeURIComponent(remote_url + '?ACAOrigin=' + |
| 165 host_info['HTTP_ORIGIN']), |
| 166 true, SUCCESS |
| 167 ] |
| 168 ]; |
| 169 var promises = []; |
| 170 var serial_tests = []; |
| 171 for (var i = 0; i < TEST_CASES.length ; ++i) { |
| 172 if (!TEST_CASES[i][3]) { |
| 173 promises.push(create_test_promise(TEST_CASES[i][0], |
| 174 TEST_CASES[i][1], |
| 175 TEST_CASES[i][2])); |
| 176 } else { |
| 177 serial_tests.push(TEST_CASES[i]); |
| 178 } |
| 179 } |
| 180 promises.push(create_serial_promise(serial_tests)); |
| 181 Promise.all(promises) |
| 84 .then(function() { | 182 .then(function() { |
| 85 port.postMessage({results: 'finish'}); | 183 port.postMessage({results: 'finish'}); |
| 86 }) | 184 }) |
| 87 .catch(function(e) { | 185 .catch(function(e) { |
| 88 port.postMessage({results: 'failure:' + e}); | 186 port.postMessage({results: 'failure:' + e}); |
| 89 }); | 187 }); |
| 90 }, false); | 188 }, false); |
| 91 </script> | 189 </script> |
| OLD | NEW |