| OLD | NEW |
| 1 var SCOPE = 'resources/fetch-access-control-iframe.html'; | 1 var SCOPE = 'resources/fetch-access-control-iframe.html'; |
| 2 var BASE_URL = 'http://127.0.0.1:8000/serviceworker/resources/fetch-access-contr
ol.php?'; | 2 var BASE_URL = 'http://127.0.0.1:8000/serviceworker/resources/fetch-access-contr
ol.php?'; |
| 3 var OTHER_BASE_URL = 'http://localhost:8000/serviceworker/resources/fetch-access
-control.php?'; | 3 var OTHER_BASE_URL = 'http://localhost:8000/serviceworker/resources/fetch-access
-control.php?'; |
| 4 var REDIRECT_URL = 'http://127.0.0.1:8000/serviceworker/resources/redirect.php?R
edirect='; | 4 var REDIRECT_URL = 'http://127.0.0.1:8000/serviceworker/resources/redirect.php?R
edirect='; |
| 5 var IFRAME_URL = 'http://127.0.0.1:8000/serviceworker/resources/fetch-access-con
trol-iframe.html'; | 5 var IFRAME_URL = 'http://127.0.0.1:8000/serviceworker/resources/fetch-access-con
trol-iframe.html'; |
| 6 var WORKER_URL = 'http://127.0.0.1:8000/serviceworker/resources/fetch-access-con
trol-worker.js'; | 6 var WORKER_URL = 'http://127.0.0.1:8000/serviceworker/resources/fetch-access-con
trol-worker.js'; |
| 7 var IFRAME_ORIGIN = 'http://127.0.0.1:8000'; | 7 var IFRAME_ORIGIN = 'http://127.0.0.1:8000'; |
| 8 | 8 |
| 9 // Functions to check the result from the ServiceWorker. | 9 // Functions to check the result from the ServiceWorker. |
| 10 var checkFetchResult = function (expected, url, data) { | 10 var checkFetchResult = function(expected, url, data) { |
| 11 assert_equals(data.fetchResult, expected, url + ' should be ' + expected); | 11 assert_equals(data.fetchResult, expected, url + ' should be ' + expected); |
| 12 }; | 12 }; |
| 13 var checkFetchResponseBody = function (hasBody, url, data) { | 13 var checkFetchResponseBody = function(hasBody, url, data) { |
| 14 assert_equals(data.fetchResult, | 14 assert_equals(data.fetchResult, |
| 15 'resolved', | 15 'resolved', |
| 16 'fetchResult must be resolved. url: ' + url); | 16 'fetchResult must be resolved. url: ' + url); |
| 17 if (hasBody) { | 17 if (hasBody) { |
| 18 assert_not_equals(data.body, '', | 18 assert_not_equals(data.body, '', |
| 19 'response must have body. url: ' + url); | 19 'response must have body. url: ' + url); |
| 20 } else { | 20 } else { |
| 21 assert_equals(data.body, '', | 21 assert_equals(data.body, '', |
| 22 'response must not have body. url: ' + url); | 22 'response must not have body. url: ' + url); |
| 23 } | 23 } |
| 24 }; | 24 }; |
| 25 var checkFetchResponseHeader = function (name, expected, url, data) { | 25 var checkFetchResponseHeader = function(name, expected, url, data) { |
| 26 assert_equals(data.fetchResult, | 26 assert_equals(data.fetchResult, |
| 27 'resolved', | 27 'resolved', |
| 28 'fetchResult must be resolved. url: ' + url); | 28 'fetchResult must be resolved. url: ' + url); |
| 29 var exist = false; | 29 var exist = false; |
| 30 for (var i = 0; i < data.headers.length; ++i) { | 30 for (var i = 0; i < data.headers.length; ++i) { |
| 31 if (data.headers[i][0] === name) { | 31 if (data.headers[i][0] === name) { |
| 32 exist = true; | 32 exist = true; |
| 33 } | 33 } |
| 34 } | 34 } |
| 35 assert_equals(exist, | 35 assert_equals(exist, |
| 36 expected, | 36 expected, |
| 37 'header check failed url: ' + url + ' name: ' + name); | 37 'header check failed url: ' + url + ' name: ' + name); |
| 38 }; | 38 }; |
| 39 var checkFetchResponseType = function (type, url, data) { | 39 var checkFetchResponseType = function(type, url, data) { |
| 40 assert_equals(data.fetchResult, | 40 assert_equals(data.fetchResult, |
| 41 'resolved', | 41 'resolved', |
| 42 'fetchResult must be resolved. url = ' + url); | 42 'fetchResult must be resolved. url = ' + url); |
| 43 assert_equals(data.type, | 43 assert_equals(data.type, |
| 44 type, | 44 type, |
| 45 'type must match. url: ' + url); | 45 'type must match. url: ' + url); |
| 46 }; | 46 }; |
| 47 var fetchIgnored = checkFetchResult.bind(this, 'ignored'); | 47 var fetchIgnored = checkFetchResult.bind(this, 'ignored'); |
| 48 var fetchResolved = checkFetchResult.bind(this, 'resolved'); | 48 var fetchResolved = checkFetchResult.bind(this, 'resolved'); |
| 49 var fetchRejected = checkFetchResult.bind(this, 'rejected'); | 49 var fetchRejected = checkFetchResult.bind(this, 'rejected'); |
| 50 var fetchError = checkFetchResult.bind(this, 'error'); | 50 var fetchError = checkFetchResult.bind(this, 'error'); |
| 51 var hasBody = checkFetchResponseBody.bind(this, true); | 51 var hasBody = checkFetchResponseBody.bind(this, true); |
| 52 var noBody = checkFetchResponseBody.bind(this, false); | 52 var noBody = checkFetchResponseBody.bind(this, false); |
| 53 var hasContentLength = | 53 var hasContentLength = |
| 54 checkFetchResponseHeader.bind(this, 'content-length', true); | 54 checkFetchResponseHeader.bind(this, 'content-length', true); |
| 55 var noContentLength = | 55 var noContentLength = |
| 56 checkFetchResponseHeader.bind(this, 'content-length', false); | 56 checkFetchResponseHeader.bind(this, 'content-length', false); |
| 57 var hasServerHeader = | 57 var hasServerHeader = |
| 58 checkFetchResponseHeader.bind(this, 'x-serviceworker-serverheader', true); | 58 checkFetchResponseHeader.bind(this, 'x-serviceworker-serverheader', true); |
| 59 var noServerHeader = | 59 var noServerHeader = |
| 60 checkFetchResponseHeader.bind(this, 'x-serviceworker-serverheader', false); | 60 checkFetchResponseHeader.bind(this, 'x-serviceworker-serverheader', false); |
| 61 var typeBasic = checkFetchResponseType.bind(this, 'basic'); | 61 var typeBasic = checkFetchResponseType.bind(this, 'basic'); |
| 62 var typeCors = checkFetchResponseType.bind(this, 'cors'); | 62 var typeCors = checkFetchResponseType.bind(this, 'cors'); |
| 63 var typeOpaque = checkFetchResponseType.bind(this, 'opaque'); | 63 var typeOpaque = checkFetchResponseType.bind(this, 'opaque'); |
| 64 | 64 |
| 65 // Functions to check the result of JSONP which is evaluated in | 65 // Functions to check the result of JSONP which is evaluated in |
| 66 // fetch-access-control-iframe.html by appending <script> element. | 66 // fetch-access-control-iframe.html by appending <script> element. |
| 67 var checkJsonpResult = function (expected, url, data) { | 67 var checkJsonpResult = function(expected, url, data) { |
| 68 assert_equals(data.jsonpResult, | 68 assert_equals(data.jsonpResult, |
| 69 expected, | 69 expected, |
| 70 url + ' jsonpResult should match'); | 70 url + ' jsonpResult should match'); |
| 71 }; | 71 }; |
| 72 var checkJsonpHeader = function (name, value, url, data) { | 72 var checkJsonpHeader = function(name, value, url, data) { |
| 73 assert_equals(data.jsonpResult, | 73 assert_equals(data.jsonpResult, |
| 74 'success', | 74 'success', |
| 75 url + ' jsonpResult must be success'); | 75 url + ' jsonpResult must be success'); |
| 76 assert_equals(data.headers[name], | 76 assert_equals(data.headers[name], |
| 77 value, | 77 value, |
| 78 'Request header check failed url:' + url + ' name:' + name); | 78 'Request header check failed url:' + url + ' name:' + name); |
| 79 }; | 79 }; |
| 80 var checkJsonpMethod = function (method, url, data) { | 80 var checkJsonpMethod = function(method, url, data) { |
| 81 assert_equals(data.jsonpResult, | 81 assert_equals(data.jsonpResult, |
| 82 'success', | 82 'success', |
| 83 url + ' jsonpResult must be success'); | 83 url + ' jsonpResult must be success'); |
| 84 assert_equals(data.method, | 84 assert_equals(data.method, |
| 85 method, | 85 method, |
| 86 'Method must match url:' + url); | 86 'Method must match url:' + url); |
| 87 }; | 87 }; |
| 88 var checkJsonpAuth = function (username, password, url, data) { | 88 var checkJsonpAuth = function(username, password, url, data) { |
| 89 assert_equals(data.jsonpResult, | 89 assert_equals(data.jsonpResult, |
| 90 'success', | 90 'success', |
| 91 url + ' jsonpResult must be success'); | 91 url + ' jsonpResult must be success'); |
| 92 assert_equals(data.username, | 92 assert_equals(data.username, |
| 93 username, | 93 username, |
| 94 'Username must match. url: ' + url); | 94 'Username must match. url: ' + url); |
| 95 assert_equals(data.password, | 95 assert_equals(data.password, |
| 96 password, | 96 password, |
| 97 'Password must match. url: ' + url); | 97 'Password must match. url: ' + url); |
| 98 assert_equals(data.cookie, | 98 assert_equals(data.cookie, |
| 99 username, | 99 username, |
| 100 'Cookie must match. url: ' + url); | 100 'Cookie must match. url: ' + url); |
| 101 }; | 101 }; |
| 102 var checkJsonpError = checkJsonpResult.bind(this, 'error'); | 102 var checkJsonpError = checkJsonpResult.bind(this, 'error'); |
| 103 var checkJsonpSuccess = checkJsonpResult.bind(this, 'success'); | 103 var checkJsonpSuccess = checkJsonpResult.bind(this, 'success'); |
| 104 var hasCustomHeader = | 104 var hasCustomHeader = |
| 105 checkJsonpHeader.bind(this, 'x-serviceworker-test', 'test'); | 105 checkJsonpHeader.bind(this, 'x-serviceworker-test', 'test'); |
| 106 var noCustomHeader = | 106 var noCustomHeader = |
| 107 checkJsonpHeader.bind(this, 'x-serviceworker-test', undefined); | 107 checkJsonpHeader.bind(this, 'x-serviceworker-test', undefined); |
| 108 var methodIsGET = checkJsonpMethod.bind(this, 'GET'); | 108 var methodIsGET = checkJsonpMethod.bind(this, 'GET'); |
| 109 var methodIsPOST = checkJsonpMethod.bind(this, 'POST'); | 109 var methodIsPOST = checkJsonpMethod.bind(this, 'POST'); |
| 110 var methodIsPUT = checkJsonpMethod.bind(this, 'PUT'); | 110 var methodIsPUT = checkJsonpMethod.bind(this, 'PUT'); |
| 111 var methodIsXXX = checkJsonpMethod.bind(this, 'XXX'); | 111 var methodIsXXX = checkJsonpMethod.bind(this, 'XXX'); |
| 112 var authCheckNone = checkJsonpAuth.bind(this, 'undefined', 'undefined'); | 112 var authCheckNone = checkJsonpAuth.bind(this, 'undefined', 'undefined'); |
| 113 var authCheck1 = checkJsonpAuth.bind(this, 'username1', 'password1'); | 113 var authCheck1 = checkJsonpAuth.bind(this, 'username1', 'password1'); |
| 114 var authCheck2 = checkJsonpAuth.bind(this, 'username2', 'password2'); | 114 var authCheck2 = checkJsonpAuth.bind(this, 'username2', 'password2'); |
| 115 | 115 |
| 116 function executeTests(test, test_targets) { | 116 function executeTests(test, test_targets) { |
| 117 test.step(function() { | 117 test.step(function() { |
| 118 var login1 = | 118 var login1 = |
| 119 test_login(test, 'http://127.0.0.1:8000', 'username1', 'password1'); | 119 test_login(test, 'http://127.0.0.1:8000', 'username1', 'password1'); |
| 120 var login2 = | 120 var login2 = |
| 121 test_login(test, 'http://localhost:8000', 'username2', 'password2'); | 121 test_login(test, 'http://localhost:8000', 'username2', 'password2'); |
| 122 var workerScript = 'resources/fetch-access-control-worker.js'; | 122 var workerScript = 'resources/fetch-access-control-worker.js'; |
| 123 var worker = undefined; | 123 var worker = undefined; |
| 124 var frameWindow = {}; | 124 var frameWindow = {}; |
| 125 var counter = 0; | 125 var counter = 0; |
| 126 window.addEventListener('message', test.step_func(onMessage), false); | 126 window.addEventListener('message', test.step_func(onMessage), false); |
| 127 | 127 |
| 128 Promise.all([login1, login2]) | 128 Promise.all([login1, login2]) |
| 129 .then(function() { | 129 .then(function() { |
| 130 return service_worker_unregister_and_register(test, | 130 return service_worker_unregister_and_register(test, |
| 131 workerScript, | 131 workerScript, |
| 132 SCOPE); | 132 SCOPE); |
| 133 }) | 133 }) |
| 134 .then(function(registration) { | 134 .then(function(registration) { |
| 135 return wait_for_update(test, registration); | 135 return wait_for_update(test, registration); |
| 136 }) | 136 }) |
| 137 .then(function(sw) { | 137 .then(function(sw) { |
| 138 worker = sw; | 138 worker = sw; |
| 139 var messageChannel = new MessageChannel(); | 139 var messageChannel = new MessageChannel(); |
| 140 messageChannel.port1.onmessage = test.step_func(onWorkerMessage); | 140 messageChannel.port1.onmessage = test.step_func(onWorkerMessage); |
| 141 sw.postMessage( | 141 sw.postMessage( |
| 142 {port: messageChannel.port2}, [messageChannel.port2]); | 142 {port: messageChannel.port2}, [messageChannel.port2]); |
| 143 return wait_for_state(test, sw, 'activated'); | 143 return wait_for_state(test, sw, 'activated'); |
| 144 }) | 144 }) |
| 145 .then(function() { | 145 .then(function() { |
| 146 return with_iframe(SCOPE); | 146 return with_iframe(SCOPE); |
| 147 }) | 147 }) |
| 148 .then(function(frame) { | 148 .then(function(frame) { |
| 149 frameWindow = frame.contentWindow; | 149 frameWindow = frame.contentWindow; |
| 150 // Start tests. | 150 // Start tests. |
| 151 loadNext(); | 151 loadNext(); |
| 152 }) | 152 }) |
| 153 .catch(unreached_rejection(test)); | 153 .catch(unreached_rejection(test)); |
| 154 | 154 |
| 155 var readyFromWorkerReceived = undefined; | 155 var readyFromWorkerReceived = undefined; |
| 156 var resultFromWorkerReceived = undefined; | 156 var resultFromWorkerReceived = undefined; |
| 157 var resultFromIframeReceived = undefined; | 157 var resultFromIframeReceived = undefined; |
| 158 | 158 |
| 159 function onMessage(e) { | 159 function onMessage(e) { |
| 160 // The message is sent from fetch-access-control-iframe.html in report() | 160 // The message is sent from fetch-access-control-iframe.html in report() |
| 161 // which is called by appending <script> element which source code is | 161 // which is called by appending <script> element which source code is |
| 162 // generated by fetch-access-control.php. | 162 // generated by fetch-access-control.php. |
| 163 if (TEST_TARGETS[counter][2]) { | 163 if (TEST_TARGETS[counter][2]) { |
| 164 TEST_TARGETS[counter][2].forEach(function(checkFunc) { | 164 TEST_TARGETS[counter][2].forEach(function(checkFunc) { |
| 165 checkFunc.call(this, | 165 checkFunc.call(this, |
| 166 TEST_TARGETS[counter][0], | 166 TEST_TARGETS[counter][0], |
| 167 e.data); | 167 e.data); |
| 168 }); | 168 }); |
| 169 } |
| 170 resultFromIframeReceived(); |
| 169 } | 171 } |
| 170 resultFromIframeReceived(); | |
| 171 } | |
| 172 | 172 |
| 173 function onWorkerMessage(e) { | 173 function onWorkerMessage(e) { |
| 174 // The message is sent from the ServiceWorker. | 174 // The message is sent from the ServiceWorker. |
| 175 var message = e.data; | 175 var message = e.data; |
| 176 if (message.msg === 'READY') { | 176 if (message.msg === 'READY') { |
| 177 readyFromWorkerReceived(); | 177 readyFromWorkerReceived(); |
| 178 return; | 178 return; |
| 179 } |
| 180 TEST_TARGETS[counter][1].forEach(function(checkFunc) { |
| 181 checkFunc.call(this, |
| 182 TEST_TARGETS[counter][0], |
| 183 message); |
| 184 }); |
| 185 resultFromWorkerReceived(); |
| 179 } | 186 } |
| 180 TEST_TARGETS[counter][1].forEach(function(checkFunc) { | |
| 181 checkFunc.call(this, | |
| 182 TEST_TARGETS[counter][0], | |
| 183 message); | |
| 184 }); | |
| 185 resultFromWorkerReceived(); | |
| 186 } | |
| 187 | 187 |
| 188 function loadNext() { | 188 function loadNext() { |
| 189 var workerPromise = new Promise(function(resolve, reject) { | 189 var workerPromise = new Promise(function(resolve, reject) { |
| 190 resultFromWorkerReceived = resolve; | 190 resultFromWorkerReceived = resolve; |
| 191 }); | 191 }); |
| 192 var iframePromise = new Promise(function(resolve, reject) { | 192 var iframePromise = new Promise(function(resolve, reject) { |
| 193 resultFromIframeReceived = resolve; | 193 resultFromIframeReceived = resolve; |
| 194 }); | 194 }); |
| 195 Promise.all([workerPromise, iframePromise]) | 195 Promise.all([workerPromise, iframePromise]) |
| 196 .then(test.step_func(function() { | 196 .then(test.step_func(function() { |
| 197 ++counter; | 197 ++counter; |
| 198 if (counter === TEST_TARGETS.length) { | 198 if (counter === TEST_TARGETS.length) { |
| 199 service_worker_unregister_and_done(test, SCOPE); | 199 service_worker_unregister_and_done(test, SCOPE); |
| 200 } else { | 200 } else { |
| 201 loadNext(); | 201 loadNext(); |
| 202 } | 202 } |
| 203 })); | 203 })); |
| 204 (new Promise(function(resolve, reject) { | 204 (new Promise(function(resolve, reject) { |
| 205 readyFromWorkerReceived = resolve; | 205 readyFromWorkerReceived = resolve; |
| 206 worker.postMessage({msg: 'START TEST CASE'}); | 206 worker.postMessage({msg: 'START TEST CASE'}); |
| 207 })) | 207 })) |
| 208 .then(test.step_func(function() { | 208 .then(test.step_func(function() { |
| 209 frameWindow.postMessage( | 209 frameWindow.postMessage( |
| 210 {url: TEST_TARGETS[counter][0]}, | 210 {url: TEST_TARGETS[counter][0]}, |
| 211 IFRAME_ORIGIN); | 211 IFRAME_ORIGIN); |
| 212 })); | 212 })); |
| 213 } | 213 } |
| 214 }); | 214 }); |
| 215 } | 215 } |
| OLD | NEW |