test_fetch-http.js (2128B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 "use strict"; 4 5 // Tests for DevToolsUtils.fetch on http:// URI's. 6 7 const { HttpServer } = ChromeUtils.importESModule( 8 "resource://testing-common/httpd.sys.mjs" 9 ); 10 11 const server = new HttpServer(); 12 server.registerDirectory("/", do_get_cwd()); 13 server.registerPathHandler("/cached.json", cacheRequestHandler); 14 server.start(-1); 15 16 const port = server.identity.primaryPort; 17 const serverURL = "http://localhost:" + port; 18 const CACHED_URL = serverURL + "/cached.json"; 19 const NORMAL_URL = serverURL + "/test_fetch-http.js"; 20 21 function cacheRequestHandler(request, response) { 22 info("Got request for " + request.path); 23 response.setHeader("Cache-Control", "max-age=10000", false); 24 response.setStatusLine(request.httpVersion, 200, "OK"); 25 response.setHeader("Content-Type", "application/json", false); 26 27 const body = "[" + Math.random() + "]"; 28 response.bodyOutputStream.write(body, body.length); 29 } 30 31 do_get_profile(); 32 33 registerCleanupFunction(() => { 34 return new Promise(resolve => server.stop(resolve)); 35 }); 36 37 add_task(async function test_normal() { 38 await DevToolsUtils.fetch(NORMAL_URL).then(({ content }) => { 39 ok( 40 content.includes("The content looks correct."), 41 "The content looks correct." 42 ); 43 }); 44 }); 45 46 add_task(async function test_caching() { 47 let initialContent = null; 48 49 info("Performing the first request."); 50 await DevToolsUtils.fetch(CACHED_URL).then(({ content }) => { 51 info("Got the first response: " + content); 52 initialContent = content; 53 }); 54 55 info("Performing another request, expecting to get cached response."); 56 await DevToolsUtils.fetch(CACHED_URL).then(({ content }) => { 57 deepEqual(content, initialContent, "The content was loaded from cache."); 58 }); 59 60 info("Performing a third request with cache bypassed."); 61 const opts = { loadFromCache: false }; 62 await DevToolsUtils.fetch(CACHED_URL, opts).then(({ content }) => { 63 notDeepEqual( 64 content, 65 initialContent, 66 "The URL wasn't loaded from cache with loadFromCache: false." 67 ); 68 }); 69 });