preflight-cache.htm (4743B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>CORS - preflight cache</title> 4 <meta name="timeout" content="long"> 5 <meta name=author title="Odin Hørthe Omdal" href="mailto:odiho@opera.com"> 6 7 <script src=/resources/testharness.js></script> 8 <script src=/resources/testharnessreport.js></script> 9 <script src=/common/utils.js></script> 10 <script src=support.js?pipe=sub></script> 11 12 <h1>Preflight cache</h1> 13 14 <div id=log></div> 15 <script> 16 17 /* 18 * Cache 19 */ 20 21 function did_preflight(expect, client, settings) { 22 var uuid_token = (settings && settings.token) || token(); 23 if(!settings) 24 settings = {} 25 26 set = { 27 method: 'method' in settings ? settings.method : 'GET', 28 extra: 'extra' in settings ? '&' + settings.extra : '' 29 } 30 31 client.open(set.method, 32 CROSSDOMAIN + 'resources/preflight.py?token=' + uuid_token + set.extra, 33 false) 34 client.setRequestHeader('x-print', uuid_token) 35 client.send() 36 37 client.open('GET', 'resources/preflight.py?check&token=' + uuid_token, false) 38 client.send() 39 assert_equals(client.response, expect === true ? '1' : '0', "did preflight") 40 return uuid_token; 41 } 42 43 /* 44 * Should run preflight 45 */ 46 47 test(function() { 48 var time = new Date().getTime() 49 var client = new XMLHttpRequest() 50 did_preflight(true, client); 51 }, 52 'Test preflight') 53 54 test(function() { 55 var time = new Date().getTime() 56 var client = new XMLHttpRequest() 57 58 var id = did_preflight(true, client) 59 did_preflight(false, client, {token: id}) 60 }, 61 'preflight for x-print should be cached') 62 63 test(function() { 64 var time = new Date().getTime() 65 var client = new XMLHttpRequest() 66 67 var id = did_preflight(true, client, {extra:'max_age='}) 68 did_preflight(false, client, {extra:'max_age=', token: id}) 69 }, 70 'age = blank, should be cached') 71 72 test(function() { 73 var time = new Date().getTime() 74 var client = new XMLHttpRequest() 75 76 var id = did_preflight(true, client, {extra:'max_age=0'}) 77 did_preflight(true, client, {extra:'max_age=0', token: id}) 78 }, 79 'age = 0, should not be cached') 80 81 test(function() { 82 var time = new Date().getTime() 83 var client = new XMLHttpRequest() 84 85 var id = did_preflight(true, client, {extra:'max_age=-1'}) 86 did_preflight(true, client, {extra:'max_age=-1', token: id}) 87 }, 88 'age = -1, should not be cached'); 89 90 (function() { 91 var test = async_test("preflight first request, second from cache, wait, third should preflight again"), 92 time = new Date().getTime(), 93 dothing = function (url, msg, set_request, func) { 94 client = new XMLHttpRequest(), 95 client.open('GET', url, true) 96 if (set_request) 97 client.setRequestHeader('x-print', msg) 98 client.onload = test.step_func(function() { 99 assert_equals(client.response, msg, "response " + url) 100 if (func) 101 test.step(func) 102 }) 103 client.onerror = test.step_func(function(e) { 104 assert_unreached("Got unexpected error event on the XHR object") 105 }) 106 client.send() 107 } 108 109 var token1 = token(); 110 test.step(function() { 111 /* First cycle, gets x-print into the cache, with timeout 1 */ 112 var request_url = CROSSDOMAIN + 'resources/preflight.py?max_age=1&token=' + token1; 113 dothing(request_url, 114 'first', true, function() { 115 test = test; 116 117 /* Check if we did a preflight like we expected */ 118 dothing('resources/preflight.py?check&1&token=' + token1, 119 '1', false, function() { 120 test = test; 121 dothing(request_url, 122 'second', true, function() { 123 test = test; 124 125 /* Check that we didn't do a preflight (hasn't gone 1 second yet) */ 126 dothing('resources/preflight.py?check&2&token=' + token1, 127 '0', false, function() { 128 test = test; 129 130 /* Wait until the preflight cache age is old (and thus cleared) */ 131 test.step_timeout(() => { 132 dothing(request_url, 133 'third', true, function() { 134 test = test; 135 136 /* Expect that we did indeed do a preflight */ 137 dothing('resources/preflight.py?check&3&token=' + token1, 138 '1', false, function() { 139 test.done() 140 }) 141 }) 142 }, 1500) 143 }) 144 }) 145 }) 146 }) 147 }) 148 })(); 149 150 </script>