1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/tests/browser/browser_save_resend_postdata.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +var MockFilePicker = SpecialPowers.MockFilePicker; 1.9 +MockFilePicker.init(window); 1.10 + 1.11 +/** 1.12 + * Test for bug 471962 <https://bugzilla.mozilla.org/show_bug.cgi?id=471962>: 1.13 + * When saving an inner frame as file only, the POST data of the outer page is 1.14 + * sent to the address of the inner page. 1.15 + * 1.16 + * Test for bug 485196 <https://bugzilla.mozilla.org/show_bug.cgi?id=485196>: 1.17 + * Web page generated by POST is retried as GET when Save Frame As used, and the 1.18 + * page is no longer in the cache. 1.19 + */ 1.20 +function test() { 1.21 + waitForExplicitFinish(); 1.22 + 1.23 + gBrowser.selectedTab = gBrowser.addTab(); 1.24 + 1.25 + gBrowser.loadURI("http://mochi.test:8888/browser/toolkit/content/tests/browser/data/post_form_outer.sjs"); 1.26 + 1.27 + registerCleanupFunction(function () { 1.28 + gBrowser.removeCurrentTab(); 1.29 + }); 1.30 + 1.31 + gBrowser.addEventListener("pageshow", function pageShown(event) { 1.32 + if (event.target.location == "about:blank") 1.33 + return; 1.34 + gBrowser.removeEventListener("pageshow", pageShown); 1.35 + 1.36 + // Submit the form in the outer page, then wait for both the outer 1.37 + // document and the inner frame to be loaded again. 1.38 + gBrowser.addEventListener("DOMContentLoaded", handleOuterSubmit); 1.39 + gBrowser.contentDocument.getElementById("postForm").submit(); 1.40 + }); 1.41 + 1.42 + var framesLoaded = 0; 1.43 + var innerFrame; 1.44 + 1.45 + function handleOuterSubmit() { 1.46 + if (++framesLoaded < 2) 1.47 + return; 1.48 + 1.49 + gBrowser.removeEventListener("DOMContentLoaded", handleOuterSubmit); 1.50 + 1.51 + innerFrame = gBrowser.contentDocument.getElementById("innerFrame"); 1.52 + 1.53 + // Submit the form in the inner page. 1.54 + gBrowser.addEventListener("DOMContentLoaded", handleInnerSubmit); 1.55 + innerFrame.contentDocument.getElementById("postForm").submit(); 1.56 + } 1.57 + 1.58 + function handleInnerSubmit() { 1.59 + gBrowser.removeEventListener("DOMContentLoaded", handleInnerSubmit); 1.60 + 1.61 + // Create the folder the page will be saved into. 1.62 + var destDir = createTemporarySaveDirectory(); 1.63 + var file = destDir.clone(); 1.64 + file.append("no_default_file_name"); 1.65 + MockFilePicker.returnFiles = [file]; 1.66 + MockFilePicker.showCallback = function(fp) { 1.67 + MockFilePicker.filterIndex = 1; // kSaveAsType_URL 1.68 + }; 1.69 + 1.70 + mockTransferCallback = onTransferComplete; 1.71 + mockTransferRegisterer.register(); 1.72 + 1.73 + registerCleanupFunction(function () { 1.74 + mockTransferRegisterer.unregister(); 1.75 + MockFilePicker.cleanup(); 1.76 + destDir.remove(true); 1.77 + }); 1.78 + 1.79 + var docToSave = innerFrame.contentDocument; 1.80 + // We call internalSave instead of saveDocument to bypass the history 1.81 + // cache. 1.82 + internalSave(docToSave.location.href, docToSave, null, null, 1.83 + docToSave.contentType, false, null, null, 1.84 + docToSave.referrer ? makeURI(docToSave.referrer) : null, 1.85 + docToSave, false, null); 1.86 + } 1.87 + 1.88 + function onTransferComplete(downloadSuccess) { 1.89 + ok(downloadSuccess, "The inner frame should have been downloaded successfully"); 1.90 + 1.91 + // Read the entire saved file. 1.92 + var file = MockFilePicker.returnFiles[0]; 1.93 + var fileContents = readShortFile(file); 1.94 + 1.95 + // Check if outer POST data is found (bug 471962). 1.96 + is(fileContents.indexOf("inputfield=outer"), -1, 1.97 + "The saved inner frame does not contain outer POST data"); 1.98 + 1.99 + // Check if inner POST data is found (bug 485196). 1.100 + isnot(fileContents.indexOf("inputfield=inner"), -1, 1.101 + "The saved inner frame was generated using the correct POST data"); 1.102 + 1.103 + finish(); 1.104 + } 1.105 +} 1.106 + 1.107 +Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.108 + .getService(Ci.mozIJSSubScriptLoader) 1.109 + .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", 1.110 + this); 1.111 + 1.112 +function createTemporarySaveDirectory() { 1.113 + var saveDir = Cc["@mozilla.org/file/directory_service;1"] 1.114 + .getService(Ci.nsIProperties) 1.115 + .get("TmpD", Ci.nsIFile); 1.116 + saveDir.append("testsavedir"); 1.117 + if (!saveDir.exists()) 1.118 + saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); 1.119 + return saveDir; 1.120 +} 1.121 + 1.122 +/** 1.123 + * Reads the contents of the provided short file (up to 1 MiB). 1.124 + * 1.125 + * @param aFile 1.126 + * nsIFile object pointing to the file to be read. 1.127 + * 1.128 + * @return 1.129 + * String containing the raw octets read from the file. 1.130 + */ 1.131 +function readShortFile(aFile) { 1.132 + var inputStream = Cc["@mozilla.org/network/file-input-stream;1"] 1.133 + .createInstance(Ci.nsIFileInputStream); 1.134 + inputStream.init(aFile, -1, 0, 0); 1.135 + try { 1.136 + var scrInputStream = Cc["@mozilla.org/scriptableinputstream;1"] 1.137 + .createInstance(Ci.nsIScriptableInputStream); 1.138 + scrInputStream.init(inputStream); 1.139 + try { 1.140 + // Assume that the file is much shorter than 1 MiB. 1.141 + return scrInputStream.read(1048576); 1.142 + } 1.143 + finally { 1.144 + // Close the scriptable stream after reading, even if the operation 1.145 + // failed. 1.146 + scrInputStream.close(); 1.147 + } 1.148 + } 1.149 + finally { 1.150 + // Close the stream after reading, if it is still open, even if the read 1.151 + // operation failed. 1.152 + inputStream.close(); 1.153 + } 1.154 +}