|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 var MockFilePicker = SpecialPowers.MockFilePicker; |
|
6 MockFilePicker.init(window); |
|
7 |
|
8 /** |
|
9 * Test for bug 471962 <https://bugzilla.mozilla.org/show_bug.cgi?id=471962>: |
|
10 * When saving an inner frame as file only, the POST data of the outer page is |
|
11 * sent to the address of the inner page. |
|
12 * |
|
13 * Test for bug 485196 <https://bugzilla.mozilla.org/show_bug.cgi?id=485196>: |
|
14 * Web page generated by POST is retried as GET when Save Frame As used, and the |
|
15 * page is no longer in the cache. |
|
16 */ |
|
17 function test() { |
|
18 waitForExplicitFinish(); |
|
19 |
|
20 gBrowser.selectedTab = gBrowser.addTab(); |
|
21 |
|
22 gBrowser.loadURI("http://mochi.test:8888/browser/toolkit/content/tests/browser/data/post_form_outer.sjs"); |
|
23 |
|
24 registerCleanupFunction(function () { |
|
25 gBrowser.removeCurrentTab(); |
|
26 }); |
|
27 |
|
28 gBrowser.addEventListener("pageshow", function pageShown(event) { |
|
29 if (event.target.location == "about:blank") |
|
30 return; |
|
31 gBrowser.removeEventListener("pageshow", pageShown); |
|
32 |
|
33 // Submit the form in the outer page, then wait for both the outer |
|
34 // document and the inner frame to be loaded again. |
|
35 gBrowser.addEventListener("DOMContentLoaded", handleOuterSubmit); |
|
36 gBrowser.contentDocument.getElementById("postForm").submit(); |
|
37 }); |
|
38 |
|
39 var framesLoaded = 0; |
|
40 var innerFrame; |
|
41 |
|
42 function handleOuterSubmit() { |
|
43 if (++framesLoaded < 2) |
|
44 return; |
|
45 |
|
46 gBrowser.removeEventListener("DOMContentLoaded", handleOuterSubmit); |
|
47 |
|
48 innerFrame = gBrowser.contentDocument.getElementById("innerFrame"); |
|
49 |
|
50 // Submit the form in the inner page. |
|
51 gBrowser.addEventListener("DOMContentLoaded", handleInnerSubmit); |
|
52 innerFrame.contentDocument.getElementById("postForm").submit(); |
|
53 } |
|
54 |
|
55 function handleInnerSubmit() { |
|
56 gBrowser.removeEventListener("DOMContentLoaded", handleInnerSubmit); |
|
57 |
|
58 // Create the folder the page will be saved into. |
|
59 var destDir = createTemporarySaveDirectory(); |
|
60 var file = destDir.clone(); |
|
61 file.append("no_default_file_name"); |
|
62 MockFilePicker.returnFiles = [file]; |
|
63 MockFilePicker.showCallback = function(fp) { |
|
64 MockFilePicker.filterIndex = 1; // kSaveAsType_URL |
|
65 }; |
|
66 |
|
67 mockTransferCallback = onTransferComplete; |
|
68 mockTransferRegisterer.register(); |
|
69 |
|
70 registerCleanupFunction(function () { |
|
71 mockTransferRegisterer.unregister(); |
|
72 MockFilePicker.cleanup(); |
|
73 destDir.remove(true); |
|
74 }); |
|
75 |
|
76 var docToSave = innerFrame.contentDocument; |
|
77 // We call internalSave instead of saveDocument to bypass the history |
|
78 // cache. |
|
79 internalSave(docToSave.location.href, docToSave, null, null, |
|
80 docToSave.contentType, false, null, null, |
|
81 docToSave.referrer ? makeURI(docToSave.referrer) : null, |
|
82 docToSave, false, null); |
|
83 } |
|
84 |
|
85 function onTransferComplete(downloadSuccess) { |
|
86 ok(downloadSuccess, "The inner frame should have been downloaded successfully"); |
|
87 |
|
88 // Read the entire saved file. |
|
89 var file = MockFilePicker.returnFiles[0]; |
|
90 var fileContents = readShortFile(file); |
|
91 |
|
92 // Check if outer POST data is found (bug 471962). |
|
93 is(fileContents.indexOf("inputfield=outer"), -1, |
|
94 "The saved inner frame does not contain outer POST data"); |
|
95 |
|
96 // Check if inner POST data is found (bug 485196). |
|
97 isnot(fileContents.indexOf("inputfield=inner"), -1, |
|
98 "The saved inner frame was generated using the correct POST data"); |
|
99 |
|
100 finish(); |
|
101 } |
|
102 } |
|
103 |
|
104 Cc["@mozilla.org/moz/jssubscript-loader;1"] |
|
105 .getService(Ci.mozIJSSubScriptLoader) |
|
106 .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js", |
|
107 this); |
|
108 |
|
109 function createTemporarySaveDirectory() { |
|
110 var saveDir = Cc["@mozilla.org/file/directory_service;1"] |
|
111 .getService(Ci.nsIProperties) |
|
112 .get("TmpD", Ci.nsIFile); |
|
113 saveDir.append("testsavedir"); |
|
114 if (!saveDir.exists()) |
|
115 saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755); |
|
116 return saveDir; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Reads the contents of the provided short file (up to 1 MiB). |
|
121 * |
|
122 * @param aFile |
|
123 * nsIFile object pointing to the file to be read. |
|
124 * |
|
125 * @return |
|
126 * String containing the raw octets read from the file. |
|
127 */ |
|
128 function readShortFile(aFile) { |
|
129 var inputStream = Cc["@mozilla.org/network/file-input-stream;1"] |
|
130 .createInstance(Ci.nsIFileInputStream); |
|
131 inputStream.init(aFile, -1, 0, 0); |
|
132 try { |
|
133 var scrInputStream = Cc["@mozilla.org/scriptableinputstream;1"] |
|
134 .createInstance(Ci.nsIScriptableInputStream); |
|
135 scrInputStream.init(inputStream); |
|
136 try { |
|
137 // Assume that the file is much shorter than 1 MiB. |
|
138 return scrInputStream.read(1048576); |
|
139 } |
|
140 finally { |
|
141 // Close the scriptable stream after reading, even if the operation |
|
142 // failed. |
|
143 scrInputStream.close(); |
|
144 } |
|
145 } |
|
146 finally { |
|
147 // Close the stream after reading, if it is still open, even if the read |
|
148 // operation failed. |
|
149 inputStream.close(); |
|
150 } |
|
151 } |