Bug 1532639 [wpt PR 15630] - Add <meta name=timeout content=long> to WPT in SlowTests...
[gecko.git] / testing / web-platform / tests / cors / preflight-cache.htm
blob0b6138bdffacee41537e4ed4131620e863ceb3d1
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">
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>
12 <h1>Preflight cache</h1>
14 <div id=log></div>
15 <script>
18 * Cache
21 function did_preflight(expect, client, settings) {
22 var uuid_token = (settings && settings.token) || token();
23 if(!settings)
24 settings = {}
26 set = {
27 method: 'method' in settings ? settings.method : 'GET',
28 extra: 'extra' in settings ? '&' + settings.extra : ''
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()
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;
44 * Should run preflight
47 test(function() {
48 var time = new Date().getTime()
49 var client = new XMLHttpRequest()
50 did_preflight(true, client);
52 'Test preflight')
54 test(function() {
55 var time = new Date().getTime()
56 var client = new XMLHttpRequest()
58 var id = did_preflight(true, client)
59 did_preflight(false, client, {token: id})
61 'preflight for x-print should be cached')
63 test(function() {
64 var time = new Date().getTime()
65 var client = new XMLHttpRequest()
67 var id = did_preflight(true, client, {extra:'max_age=0'})
68 did_preflight(true, client, {extra:'max_age=0', token: id})
70 'age = 0, should not be cached')
72 test(function() {
73 var time = new Date().getTime()
74 var client = new XMLHttpRequest()
76 var id = did_preflight(true, client, {extra:'max_age=-1'})
77 did_preflight(true, client, {extra:'max_age=-1', token: id})
79 'age = -1, should not be cached');
81 (function() {
82 var test = async_test("preflight first request, second from cache, wait, third should preflight again"),
83 time = new Date().getTime(),
84 dothing = function (url, msg, set_request, func) {
85 client = new XMLHttpRequest(),
86 client.open('GET', url, true)
87 if (set_request)
88 client.setRequestHeader('x-print', msg)
89 client.onload = test.step_func(function() {
90 assert_equals(client.response, msg, "response " + url)
91 if (func)
92 test.step(func)
94 client.onerror = test.step_func(function(e) {
95 assert_unreached("Got unexpected error event on the XHR object")
97 client.send()
100 var token1 = token();
101 test.step(function() {
102 /* First cycle, gets x-print into the cache, with timeout 1 */
103 var request_url = CROSSDOMAIN + 'resources/preflight.py?max_age=1&token=' + token1;
104 dothing(request_url,
105 'first', true, function() {
106 test = test;
108 /* Check if we did a preflight like we expected */
109 dothing('resources/preflight.py?check&1&token=' + token1,
110 '1', false, function() {
111 test = test;
112 dothing(request_url,
113 'second', true, function() {
114 test = test;
116 /* Check that we didn't do a preflight (hasn't gone 1 second yet) */
117 dothing('resources/preflight.py?check&2&token=' + token1,
118 '0', false, function() {
119 test = test;
121 /* Wait until the preflight cache age is old (and thus cleared) */
122 test.step_timeout(() => {
123 dothing(request_url,
124 'third', true, function() {
125 test = test;
127 /* Expect that we did indeed do a preflight */
128 dothing('resources/preflight.py?check&3&token=' + token1,
129 '1', false, function() {
130 test.done()
133 }, 1500)
139 })();
141 </script>