head.js (3035B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { XPCOMUtils } = ChromeUtils.importESModule( 5 "resource://gre/modules/XPCOMUtils.sys.mjs" 6 ); 7 8 ChromeUtils.defineESModuleGetters(this, { 9 PageDataSchema: 10 "moz-src:///browser/components/pagedata/PageDataSchema.sys.mjs", 11 }); 12 13 const { HttpServer } = ChromeUtils.importESModule( 14 "resource://testing-common/httpd.sys.mjs" 15 ); 16 17 const server = new HttpServer(); 18 server.start(-1); 19 20 const SERVER_PORT = server.identity.primaryPort; 21 const BASE_URL = "http://localhost:" + SERVER_PORT; 22 const DEFAULT_PATH = "/document.html"; 23 const TEST_URL = BASE_URL + DEFAULT_PATH; 24 25 registerCleanupFunction(() => { 26 server.stop(); 27 }); 28 29 do_get_profile(); 30 Services.prefs.setBoolPref("browser.pagedata.log", true); 31 32 /** 33 * Given a string parses it as HTML into a DOM Document object. 34 * 35 * @param {string} str 36 * The string to parse. 37 * @param {string} path 38 * The path for the document on the server, defaults to "/document.html" 39 * @returns {Promise<Document>} the HTML DOM Document object. 40 */ 41 function parseDocument(str, path = DEFAULT_PATH) { 42 server.registerPathHandler(path, (request, response) => { 43 response.setHeader("Content-Type", "text/html;charset=utf-8"); 44 45 let converter = Cc[ 46 "@mozilla.org/intl/converter-output-stream;1" 47 ].createInstance(Ci.nsIConverterOutputStream); 48 converter.init(response.bodyOutputStream, "utf-8"); 49 converter.writeString(str); 50 }); 51 52 return new Promise((resolve, reject) => { 53 let request = new XMLHttpRequest(); 54 request.responseType = "document"; 55 request.open("GET", BASE_URL + path, true); 56 57 request.addEventListener("error", reject); 58 request.addEventListener("abort", reject); 59 60 request.addEventListener("load", function () { 61 resolve(request.responseXML); 62 }); 63 64 request.send(); 65 }); 66 } 67 68 /** 69 * Parses page data from a HTML string. 70 * 71 * @param {string} str 72 * The HTML string to parse. 73 * @param {string} path 74 * The path for the document on the server, defaults to "/document.html" 75 * @returns {Promise<PageData>} A promise that resolves to the page data found. 76 */ 77 async function parsePageData(str, path) { 78 let doc = await parseDocument(str, path); 79 return PageDataSchema.collectPageData(doc); 80 } 81 82 /** 83 * Verifies that the HTML string given parses to the expected page data. 84 * 85 * @param {string} str 86 * The HTML string to parse. 87 * @param {PageData} expected 88 * The expected pagedata excluding the date and url properties. 89 * @param {string} path 90 * The path for the document on the server, defaults to "/document.html" 91 * @returns {Promise<PageData>} A promise that resolves to the page data found. 92 */ 93 async function verifyPageData(str, expected, path = DEFAULT_PATH) { 94 let pageData = await parsePageData(str, path); 95 96 delete pageData.date; 97 98 Assert.equal(pageData.url, BASE_URL + path); 99 delete pageData.url; 100 101 Assert.deepEqual( 102 pageData, 103 expected, 104 "Should have seen the expected page data." 105 ); 106 }