OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <title>Service Worker: FetchEvent for resources</title> | |
3 <script src="../resources/testharness.js"></script> | |
4 <script src="../resources/testharnessreport.js"></script> | |
5 <script src="../resources/get-host-info.js"></script> | |
6 <script src="resources/test-helpers.js?pipe=sub"></script> | |
falken
2015/08/13 06:42:30
pipe-sub should be on get-host-info
horo
2015/08/13 10:46:36
Done.
| |
7 <script> | |
8 | |
9 function assert_resolves(promise, description) { | |
10 return new Promise(function(resolve, reject) { | |
11 promise | |
12 .then( | |
13 function() { resolve(); }, | |
14 function() { reject(description); }); | |
15 }); | |
16 } | |
17 | |
18 function assert_rejects(promise, description) { | |
19 return new Promise(function(resolve, reject) { | |
20 promise | |
21 .then( | |
22 function() { reject(description); }, | |
23 function() { resolve(); }); | |
24 }); | |
25 } | |
26 | |
27 function iframe_test(url) { | |
28 return new Promise(function(resolve, reject) { | |
29 var frame = document.createElement('iframe'); | |
30 // We can't catch the network error on iframe. So we use the timer. | |
31 var timer = setTimeout(function() { | |
32 reject(); | |
33 frame.remove(); | |
34 }, 500); | |
35 frame.src = url; | |
36 frame.onload = function() { | |
37 clearTimeout(timer); | |
38 if (frame.contentDocument.body.textContent == 'Hello world\n') | |
39 resolve(); | |
40 else | |
41 reject(); | |
42 frame.remove(); | |
43 }; | |
44 document.body.appendChild(frame); | |
45 }); | |
46 } | |
47 | |
48 async_test(function(t) { | |
49 var SCOPE = 'resources/fetch-request-redirect-iframe.html'; | |
50 var SCRIPT = 'resources/fetch-rewrite-worker.js'; | |
51 var host_info = get_host_info(); | |
52 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + | |
53 '/serviceworker/resources/redirect.php?Redirect='; | |
54 var IMAGE_URL = host_info['HTTP_ORIGIN'] + '/resources/square.png'; | |
55 var AUDIO_URL = | |
56 host_info['HTTP_ORIGIN'] + '/media/resources/load-video.php?' + | |
57 'name=../../../../media/content/silence.oga&type=audio/ogg'; | |
58 var XHR_URL = host_info['HTTP_ORIGIN'] + | |
59 '/serviceworker/resources/simple.txt'; | |
60 var HTML_URL = host_info['HTTP_ORIGIN'] + '/resources/dummy.html'; | |
61 | |
62 var REDIRECT_TO_IMAGE_URL = REDIRECT_URL + encodeURIComponent(IMAGE_URL); | |
63 var REDIRECT_TO_AUDIO_URL = REDIRECT_URL + encodeURIComponent(AUDIO_URL); | |
64 var REDIRECT_TO_XHR_URL = REDIRECT_URL + encodeURIComponent(XHR_URL); | |
65 var REDIRECT_TO_HTML_URL = REDIRECT_URL + encodeURIComponent(HTML_URL); | |
66 | |
67 var worker; | |
68 var frame; | |
69 service_worker_unregister_and_register(t, SCRIPT, SCOPE) | |
70 .then(function(registration) { | |
71 worker = registration.installing; | |
72 return wait_for_state(t, worker, 'activated'); | |
73 }) | |
74 .then(function() { return with_iframe(SCOPE); }) | |
75 .then(function(f) { | |
76 frame = f; | |
77 return Promise.all([ | |
78 // XMLHttpRequest tests. | |
79 assert_resolves(frame.contentWindow.xhr(XHR_URL), | |
80 'Normal XHR should succeed.'), | |
81 assert_resolves(frame.contentWindow.xhr(REDIRECT_TO_XHR_URL), | |
82 'Redirected XHR should succeed.'), | |
83 assert_resolves( | |
84 frame.contentWindow.xhr( | |
85 './?url=' + encodeURIComponent(REDIRECT_TO_XHR_URL) + | |
86 '&redirect-mode=follow'), | |
87 'Redirected XHR with Request.redirect=follow should succeed.'), | |
88 assert_rejects( | |
89 frame.contentWindow.xhr( | |
90 './?url=' + encodeURIComponent(REDIRECT_TO_XHR_URL) + | |
91 '&redirect-mode=error'), | |
92 'Redirected XHR with Request.redirect=error should fail.'), | |
93 assert_rejects( | |
94 frame.contentWindow.xhr( | |
95 './?url=' + encodeURIComponent(REDIRECT_TO_XHR_URL) + | |
96 '&redirect-mode=error'), | |
falken
2015/08/13 06:42:30
manual
horo
2015/08/13 10:46:36
Done.
| |
97 'Redirected XHR with Request.redirect=manual should fail.'), | |
98 | |
99 // Image loading tests. | |
100 assert_resolves(frame.contentWindow.load_image(IMAGE_URL), | |
101 'Normal image resource should be loaded.'), | |
102 assert_resolves( | |
103 frame.contentWindow.load_image(REDIRECT_TO_IMAGE_URL), | |
104 'Redirected image resource should be loaded.'), | |
105 assert_resolves( | |
106 frame.contentWindow.load_image( | |
107 './?url=' + encodeURIComponent(REDIRECT_TO_IMAGE_URL) + | |
108 '&redirect-mode=follow'), | |
109 'Loading redirected image with Request.redirect=follow should' + | |
110 ' succeed.'), | |
111 assert_rejects( | |
112 frame.contentWindow.load_image( | |
113 './?url=' + encodeURIComponent(REDIRECT_TO_IMAGE_URL) + | |
114 '&redirect-mode=error'), | |
115 'Loading redirected image with Request.redirect=error should ' + | |
116 'fail.'), | |
117 assert_rejects( | |
118 frame.contentWindow.load_image( | |
119 './?url=' + encodeURIComponent(REDIRECT_TO_IMAGE_URL) + | |
120 '&redirect-mode=manual'), | |
121 'Loading redirected image with Request.redirect=manual should' + | |
122 ' fail.'), | |
123 | |
124 // Audio loading tests. | |
125 assert_resolves(frame.contentWindow.load_audio(AUDIO_URL), | |
126 'Normal audio resource should be loaded.'), | |
127 assert_resolves( | |
128 frame.contentWindow.load_audio(REDIRECT_TO_AUDIO_URL), | |
129 'Redirected audio resource should be loaded.'), | |
130 assert_resolves( | |
131 frame.contentWindow.load_audio( | |
132 './?url=' + encodeURIComponent(REDIRECT_TO_AUDIO_URL) + | |
133 '&redirect-mode=follow'), | |
134 'Loading redirected audio with Request.redirect=follow should' + | |
135 ' succeed.'), | |
136 assert_rejects( | |
137 frame.contentWindow.load_audio( | |
138 './?url=' + encodeURIComponent(REDIRECT_TO_AUDIO_URL) + | |
139 '&redirect-mode=error'), | |
140 'Loading redirected audio with Request.redirect=error should ' + | |
141 'fail.'), | |
142 assert_rejects( | |
143 frame.contentWindow.load_audio( | |
144 './?url=' + encodeURIComponent(REDIRECT_TO_AUDIO_URL) + | |
145 '&redirect-mode=manual'), | |
146 'Loading redirected audio with Request.redirect=manual should' + | |
147 ' fail.'), | |
148 | |
149 // Iframe tests. | |
150 assert_resolves(iframe_test(HTML_URL), | |
151 'Normal iframe loading should succeed.'), | |
152 assert_resolves( | |
153 iframe_test(REDIRECT_TO_HTML_URL), | |
154 'Normal redirected iframe loading should succeed.'), | |
155 assert_resolves( | |
156 iframe_test(SCOPE + '?url=' + | |
157 encodeURIComponent(REDIRECT_TO_HTML_URL) + | |
158 '&redirect-mode=follow'), | |
159 'Redirected iframe loading with with Request.redirect=follow '+ | |
falken
2015/08/13 06:42:30
with with (and below)
horo
2015/08/13 10:46:36
Done.
| |
160 'should succeed.'), | |
161 assert_rejects( | |
162 iframe_test(SCOPE + '?url=' + | |
163 encodeURIComponent(REDIRECT_TO_HTML_URL) + | |
164 '&redirect-mode=error'), | |
165 'Redirected iframe loading with with Request.redirect=error '+ | |
166 'should fail.'), | |
167 assert_resolves( | |
168 iframe_test(SCOPE + '?url=' + | |
169 encodeURIComponent(REDIRECT_TO_HTML_URL) + | |
170 '&redirect-mode=manual'), | |
171 'Redirected iframe loading with with Request.redirect=follow '+ | |
falken
2015/08/13 06:42:30
redirect=manual
horo
2015/08/13 10:46:36
Done.
| |
172 'should succeed.'), | |
173 ]); | |
174 }) | |
175 .then(function() { | |
176 frame.remove(); | |
177 service_worker_unregister_and_done(t, SCOPE); | |
178 }) | |
179 .catch(unreached_rejection(t)); | |
180 }, 'Verify redirect mode of Fetch API and ServiceWorker FetchEvent.'); | |
181 </script> | |
OLD | NEW |