browser_net_save_response_as.js (2056B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 var MockFilePicker = SpecialPowers.MockFilePicker; 7 MockFilePicker.init(window.browsingContext); 8 9 /** 10 * Tests if saving a response to a file works.. 11 */ 12 13 add_task(async function () { 14 const { tab, monitor } = await initNetMonitor( 15 CONTENT_TYPE_WITHOUT_CACHE_URL, 16 { requestCount: 1 } 17 ); 18 info("Starting test... "); 19 20 const { document } = monitor.panelWin; 21 22 // Execute requests. 23 await performRequests(monitor, tab, CONTENT_TYPE_WITHOUT_CACHE_REQUESTS); 24 25 // Create the folder the gzip file will be saved into 26 const destDir = createTemporarySaveDirectory(); 27 let destFile; 28 29 MockFilePicker.displayDirectory = destDir; 30 const saveDialogClosedPromise = new Promise(resolve => { 31 MockFilePicker.showCallback = function (fp) { 32 info("MockFilePicker showCallback"); 33 const fileName = fp.defaultString; 34 destFile = destDir.clone(); 35 destFile.append(fileName); 36 MockFilePicker.setFiles([destFile]); 37 38 resolve(destFile.path); 39 }; 40 }); 41 42 registerCleanupFunction(function () { 43 MockFilePicker.cleanup(); 44 }); 45 46 // Select gzip request. 47 48 await triggerSaveResponseAs( 49 monitor, 50 document.querySelectorAll(".request-list-item")[6] 51 ); 52 53 info("Wait for the save dialog to close"); 54 const savedPath = await saveDialogClosedPromise; 55 56 const expectedFile = destDir.clone(); 57 expectedFile.append("sjs_content-type-test-server.sjs"); 58 59 is(savedPath, expectedFile.path, "Response was saved to correct path"); 60 61 await waitForFileSavedToDisk(savedPath); 62 63 const buffer = await IOUtils.read(savedPath); 64 const savedFileContent = new TextDecoder().decode(buffer); 65 66 // The content is set by https://searchfox.org/mozilla-central/source/devtools/client/netmonitor/test/sjs_content-type-test-server.sjs#360 67 // (the "gzip" case) 68 is( 69 savedFileContent, 70 new Array(1000).join("Hello gzip!"), 71 "Saved response has the correct text" 72 ); 73 74 await teardown(monitor); 75 });