multipart-image.https.html (2617B)
1 <!DOCTYPE html> 2 <title>Tests for cross-origin multipart image returned by service worker</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="resources/test-helpers.sub.js"></script> 6 7 <script> 8 // This tests loading a multipart image via service worker. The service worker responds with 9 // an opaque or a non-opaque response. The content of opaque response should not be readable. 10 11 const script = 'resources/multipart-image-worker.js'; 12 const scope = 'resources/multipart-image-iframe.html'; 13 let frame; 14 15 function check_image_data(data) { 16 assert_equals(data[0], 255); 17 assert_equals(data[1], 0); 18 assert_equals(data[2], 0); 19 assert_equals(data[3], 255); 20 } 21 22 promise_test(t => { 23 return service_worker_unregister_and_register(t, script, scope) 24 .then(registration => { 25 promise_test(() => { 26 if (frame) { 27 frame.remove(); 28 } 29 return registration.unregister(); 30 }, 'restore global state'); 31 32 return wait_for_state(t, registration.installing, 'activated'); 33 }) 34 .then(() => with_iframe(scope)) 35 .then(f => { 36 frame = f; 37 }); 38 }, 'initialize global state'); 39 40 promise_test(t => { 41 return frame.contentWindow.load_multipart_image('same-origin-multipart-image') 42 .then(img => frame.contentWindow.get_image_data(img)) 43 .then(img_data => { 44 check_image_data(img_data.data); 45 }); 46 }, 'same-origin multipart image via SW should be readable'); 47 48 promise_test(t => { 49 return frame.contentWindow.load_multipart_image('cross-origin-multipart-image-with-cors-approved') 50 .then(img => frame.contentWindow.get_image_data(img)) 51 .then(img_data => { 52 check_image_data(img_data.data); 53 }); 54 }, 'cross-origin multipart image via SW with approved CORS should be readable'); 55 56 promise_test(t => { 57 return frame.contentWindow.load_multipart_image('cross-origin-multipart-image-with-no-cors') 58 .then(img => { 59 assert_throws_dom('SecurityError', frame.contentWindow.DOMException, 60 () => frame.contentWindow.get_image_data(img)); 61 }); 62 }, 'cross-origin multipart image with no-cors via SW should not be readable'); 63 64 promise_test(t => { 65 const promise = frame.contentWindow.load_multipart_image('cross-origin-multipart-image-with-cors-rejected'); 66 return promise_rejects_dom(t, 'NetworkError', frame.contentWindow.DOMException, promise); 67 }, 'cross-origin multipart image via SW with rejected CORS should fail to load'); 68 </script>