| <!DOCTYPE html> |
| <meta charset=utf-8> |
| <title>CORS - preflight cache 2</title> |
| |
| <script src=/resources/testharness.js></script> |
| <script src=/resources/testharnessreport.js></script> |
| <script src=/common/utils.js></script> |
| <script src=resources/support.js?pipe=sub></script> |
| |
| <h1>Preflight cache 2</h1> |
| |
| <div id=log></div> |
| <script> |
| |
| (function() { |
| /* This function sends a XHR request with 'x-print=$msg' header if |
| set_request=true and other headers in extra_headers; then runs func if the |
| response returned successfully. If the response body is not msg or request |
| failed, test fails. */ |
| dothing = function (test, url, msg, set_request, extra_headers, func) { |
| client = new XMLHttpRequest(), |
| client.open('GET', url, true) |
| if (set_request){ |
| client.setRequestHeader('x-print', msg) |
| for (let i = 0; i < extra_headers.length; i++) { |
| client.setRequestHeader(extra_headers[i], 'extra header') |
| } |
| } |
| client.onload = test.step_func(function() { |
| assert_equals(client.response, msg, "response " + url) |
| if (func){ |
| test.step(func) |
| } |
| }) |
| client.onerror = test.step_func(function(e) { |
| assert_unreached("Got unexpected error event on the XHR object") |
| }) |
| client.send() |
| } |
| |
| var test2 = async_test("preflight a request whose cache entry lasts very"+ |
| " long, fly another one that replaces the first request's cache entry"+ |
| ", wait, third should fly again") |
| var token2 = token(); |
| // Helper function to assemble url components. |
| make_url = function (token) { |
| return CROSSDOMAIN + 'resources/preflight.py?token=' + token; |
| } |
| test2.step(function() { |
| test2_url = make_url(token2) |
| /* First request, gets x-print into the cache, with max-age=3. */ |
| dothing(test2, test2_url, |
| 'first', true, ["first_request"], function() { |
| test2 = test2; |
| /* Another unknown and forbidden header should trigger another |
| preflight*/ |
| dothing(test2, test2_url, |
| 'second', true, ['arbitrary_header'], function() { |
| test2 = test2; |
| |
| /* Verify that we did a preflight. */ |
| dothing(test2, 'resources/preflight.py?check&3&token=' + |
| token2, '1', false, [], function() { |
| test2 = test2; |
| |
| /* The second request should have updated the cache entry |
| for this url and origin and made max-age=1, the new |
| request should require a new preflight.*/ |
| test2.step_timeout(() => { |
| dothing(test2, test2_url, |
| 'third', true, [], function() { |
| test2 = test2; |
| |
| /* Verify that we did indeed do a preflight. */ |
| dothing(test2,'resources/' + |
| 'preflight.py?check&3&token=' + token2, '1', |
| false, [], function() { |
| test2 = test2; |
| |
| /* Trigger cleanup logic for the cache entry.*/ |
| test2.step_timeout(() => { |
| dothing(test2, test2_url, |
| 'fourth', true, [], function() { |
| test2.done() |
| }) |
| }, 2500) |
| }) |
| }) |
| }, 1500) |
| }) |
| }) |
| }) |
| }) |
| })(); |
| </script> |