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>
21 function did_preflight(expect
, client
, settings
) {
22 var uuid_token
= (settings
&& settings
.token
) || token();
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
,
34 client
.setRequestHeader('x-print', uuid_token
)
37 client
.open('GET', 'resources/preflight.py?check&token=' + uuid_token
, false)
39 assert_equals(client
.response
, expect
=== true ? '1' : '0', "did preflight")
44 * Should run preflight
48 var time
= new Date().getTime()
49 var client
= new XMLHttpRequest()
50 did_preflight(true, client
);
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')
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')
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');
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)
88 client
.setRequestHeader('x-print', msg
)
89 client
.onload
= test
.step_func(function() {
90 assert_equals(client
.response
, msg
, "response " + url
)
94 client
.onerror
= test
.step_func(function(e
) {
95 assert_unreached("Got unexpected error event on the XHR object")
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
;
105 'first', true, function() {
108 /* Check if we did a preflight like we expected */
109 dothing('resources/preflight.py?check&1&token=' + token1
,
110 '1', false, function() {
113 'second', true, function() {
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() {
121 /* Wait until the preflight cache age is old (and thus cleared) */
122 test
.step_timeout(() => {
124 'third', true, function() {
127 /* Expect that we did indeed do a preflight */
128 dothing('resources/preflight.py?check&3&token=' + token1
,
129 '1', false, function() {