OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Mixing response using ServiceWorker</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"></script> |
| 7 <body> |
| 8 <script> |
| 9 // We put this test file in chromium/ directory, because the behavior of mixing |
| 10 // multiple resources are not defined in the spec. See |
| 11 // https://github.com/slightlyoff/ServiceWorker/issues/703 for details. |
| 12 |
| 13 function create_failure_audio_test(frame, url) { |
| 14 return new Promise(function(resolve, reject) { |
| 15 var audio = frame.contentWindow.document.createElement('audio'); |
| 16 audio.oncanplay = function() { |
| 17 reject('canplay event should not be fired. url: ' + url); |
| 18 }; |
| 19 audio.onerror = resolve; |
| 20 audio.src = url; |
| 21 frame.contentWindow.document.body.appendChild(audio); |
| 22 }); |
| 23 } |
| 24 |
| 25 function create_success_audio_test(frame, url) { |
| 26 return new Promise(function(resolve, reject) { |
| 27 var audio = frame.contentWindow.document.createElement('audio'); |
| 28 audio.oncanplay = resolve; |
| 29 audio.onerror = function(e) { |
| 30 reject('error event should not be fired. url: ' + url); |
| 31 }; |
| 32 audio.src = url; |
| 33 frame.contentWindow.document.body.appendChild(audio); |
| 34 }); |
| 35 } |
| 36 |
| 37 // Creates a test case to check the following behavior: |
| 38 // 1. The audio element sends the first request. |
| 39 // - If |original| is 'same', the request is same-origin request. |
| 40 // - If |original| is 'cross', the request is cross-origin request. |
| 41 // 2. The Service Worker may intercept the request depending on |first_byte|. |
| 42 // - If |first_byte| is '', the SW doesn't intercept, and the native server |
| 43 // returns the all data response and the test finishes. |
| 44 // - If |first_byte| is 'gen', the SW generates and returns the first byte |
| 45 // response 'O'. |
| 46 // - If |first_byte| is 'same', the SW does a fetch to |
| 47 // 'service-worker-mixed-response.php' in the same origin server and returns |
| 48 // the response. |
| 49 // - If |first_byte| is 'cross', the SW does a fetch to |
| 50 // 'service-worker-mixed-response.php' in the cross origin server and |
| 51 // returns the response. |
| 52 // 3. The element sends the second request with "Range: bytes=1-" header. |
| 53 // 4. The Service Worker may intercept the request depending on |second_byte|. |
| 54 // - If |second_byte| is '', the SW doesn't intercept, and the native server |
| 55 // returns the remaining data response and the test finishes. |
| 56 // - If |second_byte| is 'gen', the SW generates and returns the second byte |
| 57 // response 'g'. |
| 58 // - If |second_byte| is 'same', the SW does a fetch to |
| 59 // 'service-worker-mixed-response.php' in the same origin server and returns |
| 60 // the response. |
| 61 // - If |second_byte| is 'cross', the SW does a fetch to |
| 62 // 'service-worker-mixed-response.php' in the cross origin server and |
| 63 // returns the response. |
| 64 // 5. The element sends the third request with "Range: bytes=2-" header. |
| 65 // 6. The native server returns the remaining data. |
| 66 // |
| 67 // When the audio element recieves the first response, it remembers the original |
| 68 // URL of it. And when it receives the succeeding response, it checks the origin |
| 69 // of the new response. If the origin is not same as the origin of the first |
| 70 // response, the response must be treated as an error. |
| 71 function audio_test(frame, original, first_byte, second_byte, expect_success) { |
| 72 var url; |
| 73 var HOST_INFO = get_host_info(); |
| 74 var AUDIO_PATH = '/media/resources/load-video.php?' + |
| 75 'name=../../../../media/content/silence.oga&type=audio/ogg'; |
| 76 if (original == 'same') { |
| 77 url = HOST_INFO['HTTP_ORIGIN'] + AUDIO_PATH; |
| 78 } else if (original == 'cross') { |
| 79 url = HOST_INFO['HTTP_REMOTE_ORIGIN'] + AUDIO_PATH; |
| 80 } |
| 81 url += '&SW_FIRST=' + first_byte + '&SW_SECOND=' + second_byte; |
| 82 if (expect_success) { |
| 83 return create_success_audio_test(frame, url); |
| 84 } else { |
| 85 return create_failure_audio_test(frame, url); |
| 86 } |
| 87 } |
| 88 |
| 89 promise_test(function(t) { |
| 90 var SCOPE = 'resources/blank.html?/service-worker-mixed-response'; |
| 91 var SCRIPT = 'resources/service-worker-mixed-response-worker.js'; |
| 92 var frame; |
| 93 return service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
| 94 .then(function(registration) { |
| 95 return wait_for_state(t, registration.installing, 'activated'); |
| 96 }) |
| 97 .then(function() { return with_iframe(SCOPE); }) |
| 98 .then(function(f) { |
| 99 frame = f; |
| 100 return Promise.all([ |
| 101 audio_test(f, 'same', '', '', true), |
| 102 audio_test(f, 'same', 'gen', '', false), |
| 103 audio_test(f, 'same', 'gen', 'gen', false), |
| 104 audio_test(f, 'same', 'gen', 'same', false), |
| 105 audio_test(f, 'same', 'gen', 'cross', false), |
| 106 audio_test(f, 'same', 'same', '', true), |
| 107 audio_test(f, 'same', 'same', 'gen', false), |
| 108 audio_test(f, 'same', 'same', 'same', true), |
| 109 audio_test(f, 'same', 'same', 'cross', false), |
| 110 audio_test(f, 'same', 'cross', '', false), |
| 111 audio_test(f, 'same', 'cross', 'gen', false), |
| 112 audio_test(f, 'same', 'cross', 'same', false), |
| 113 audio_test(f, 'same', 'cross', 'cross', false), |
| 114 audio_test(f, 'cross', '', '', true), |
| 115 audio_test(f, 'cross', 'gen', '', false), |
| 116 audio_test(f, 'cross', 'gen', 'gen', false), |
| 117 audio_test(f, 'cross', 'gen', 'same', false), |
| 118 audio_test(f, 'cross', 'gen', 'cross', false), |
| 119 audio_test(f, 'cross', 'same', '', false), |
| 120 audio_test(f, 'cross', 'same', 'gen', false), |
| 121 audio_test(f, 'cross', 'same', 'same', false), |
| 122 audio_test(f, 'cross', 'same', 'cross', false), |
| 123 audio_test(f, 'cross', 'cross', '', true), |
| 124 audio_test(f, 'cross', 'cross', 'gen', false), |
| 125 audio_test(f, 'cross', 'cross', 'same', false), |
| 126 audio_test(f, 'cross', 'cross', 'cross', true)]); |
| 127 }) |
| 128 .then(function() { |
| 129 frame.remove(); |
| 130 return service_worker_unregister_and_done(t, SCOPE); |
| 131 }); |
| 132 }, 'Tests for Service Worker generated mixed responses.'); |
| 133 </script> |
| 134 </body> |
OLD | NEW |