OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Load a plugin with the given paramaters. |
| 6 function createPluginForUrl(streamUrl, url, headers, progressCallback) { |
| 7 var plugin = document.createElement('embed'); |
| 8 plugin.type = 'application/x-google-chrome-pdf'; |
| 9 plugin.addEventListener('message', function(message) { |
| 10 switch (message.data.type.toString()) { |
| 11 case 'loadProgress': |
| 12 progressCallback(message.data.progress); |
| 13 break; |
| 14 } |
| 15 }, false); |
| 16 |
| 17 plugin.setAttribute('src', url); |
| 18 plugin.setAttribute('stream-url', streamUrl); |
| 19 plugin.setAttribute('full-frame', ''); |
| 20 plugin.setAttribute('headers', headers); |
| 21 document.body.appendChild(plugin); |
| 22 } |
| 23 |
| 24 function parseUrl(url) { |
| 25 var a = document.createElement("a"); |
| 26 a.href = url; |
| 27 return a; |
| 28 }; |
| 29 |
| 30 var tests = [ |
| 31 // Test that if the plugin is loaded with a URL that redirects it fails. |
| 32 function redirectsFail() { |
| 33 var url = parseUrl(viewer.originalUrl_); |
| 34 var redirectUrl = url.origin + '/server-redirect?' + viewer.originalUrl_; |
| 35 createPluginForUrl(redirectUrl, redirectUrl, '', function(progress) { |
| 36 if (progress == -1) |
| 37 chrome.test.succeed(); |
| 38 else |
| 39 chrome.test.fail(); |
| 40 }); |
| 41 }, |
| 42 |
| 43 // Test that if the plugin is loaded with a URL that doesn't redirect but |
| 44 // subsequent requests do redirect, it fails. |
| 45 function partialRedirectsFail() { |
| 46 var url = parseUrl(viewer.originalUrl_); |
| 47 var redirectUrl = url.origin + '/server-redirect?' + viewer.originalUrl_; |
| 48 // Set the headers manually so that the first request is made using a URL |
| 49 // that doesn't redirect and subsequent requests are made using a URL that |
| 50 // does. |
| 51 var headers = 'Accept-Ranges: bytes\n' + |
| 52 'Content-Length: 101688487\n' + |
| 53 'Content-Type: application/pdf\n'; |
| 54 createPluginForUrl(viewer.originalUrl_, redirectUrl, headers, |
| 55 function(progress) { |
| 56 if (progress == -1) |
| 57 chrome.test.succeed(); |
| 58 else |
| 59 chrome.test.fail(); |
| 60 }); |
| 61 }, |
| 62 |
| 63 // Test that if the plugin is loaded with a URL that doesn't redirect, it |
| 64 // succeeds. |
| 65 function noRedirectsSucceed() { |
| 66 createPluginForUrl(viewer.originalUrl_, viewer.originalUrl_, '', |
| 67 function(progress) { |
| 68 if (progress == 100) |
| 69 chrome.test.succeed() |
| 70 }); |
| 71 }, |
| 72 ]; |
| 73 |
| 74 var scriptingAPI = new PDFScriptingAPI(window, window); |
| 75 scriptingAPI.setLoadCallback(function() { |
| 76 chrome.test.runTests(tests); |
| 77 }); |
OLD | NEW |