test_request-utils-parseJSON.js (3032B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test devtools/client/netmonitor/src/utils/request-utils.js function 5 // |parseJSON| ensure that it correctly handles plain JSON, Base 64 6 // encoded JSON, and JSON that has XSSI protection prepended to it 7 8 "use strict"; 9 10 const { require } = ChromeUtils.importESModule( 11 "resource://devtools/shared/loader/Loader.sys.mjs" 12 ); 13 const { 14 parseJSON, 15 } = require("resource://devtools/client/netmonitor/src/utils/request-utils.js"); 16 17 function run_test() { 18 const validJSON = '{"item":{"subitem":true},"seconditem":"bar"}'; 19 const base64JSON = btoa(validJSON); 20 const parsedJSON = { item: { subitem: true }, seconditem: "bar" }; 21 const googleStyleXSSI = ")]}'\n"; 22 const facebookStyleXSSI = "for (;;);"; 23 const notRealXSSIPrevention = "sdgijsdjg"; 24 const while1XSSIPrevention = "while(1)"; 25 26 const parsedValidJSON = parseJSON(validJSON); 27 info(JSON.stringify(parsedValidJSON)); 28 ok( 29 parsedValidJSON.json.item.subitem == parsedJSON.item.subitem && 30 parsedValidJSON.json.seconditem == parsedJSON.seconditem, 31 "plain JSON is parsed correctly" 32 ); 33 34 const parsedBase64JSON = parseJSON(base64JSON); 35 ok( 36 parsedBase64JSON.json.item.subitem === parsedJSON.item.subitem && 37 parsedBase64JSON.json.seconditem === parsedJSON.seconditem, 38 "base64 encoded JSON is parsed correctly" 39 ); 40 41 const parsedGoogleStyleXSSIJSON = parseJSON(googleStyleXSSI + validJSON); 42 ok( 43 parsedGoogleStyleXSSIJSON.strippedChars === googleStyleXSSI && 44 parsedGoogleStyleXSSIJSON.error === void 0 && 45 parsedGoogleStyleXSSIJSON.json.item.subitem === parsedJSON.item.subitem && 46 parsedGoogleStyleXSSIJSON.json.seconditem === parsedJSON.seconditem, 47 "Google style XSSI sequence correctly removed and returned" 48 ); 49 50 const parsedFacebookStyleXSSIJSON = parseJSON(facebookStyleXSSI + validJSON); 51 ok( 52 parsedFacebookStyleXSSIJSON.strippedChars === facebookStyleXSSI && 53 parsedFacebookStyleXSSIJSON.error === void 0 && 54 parsedFacebookStyleXSSIJSON.json.item.subitem === 55 parsedJSON.item.subitem && 56 parsedFacebookStyleXSSIJSON.json.seconditem === parsedJSON.seconditem, 57 "Facebook style XSSI sequence correctly removed and returned" 58 ); 59 60 const parsedWhileXSSIJSON = parseJSON(while1XSSIPrevention + validJSON); 61 ok( 62 parsedWhileXSSIJSON.strippedChars === while1XSSIPrevention && 63 parsedWhileXSSIJSON.error === void 0 && 64 parsedWhileXSSIJSON.json.item.subitem === parsedJSON.item.subitem && 65 parsedWhileXSSIJSON.json.seconditem === parsedJSON.seconditem, 66 "While XSSI sequence correctly removed and returned" 67 ); 68 const parsedInvalidJson = parseJSON(notRealXSSIPrevention + validJSON); 69 ok( 70 !parsedInvalidJson.json && !parsedInvalidJson.strippedChars, 71 "Parsed invalid JSON does not return a data object or strippedChars" 72 ); 73 equal( 74 parsedInvalidJson.error.name, 75 "SyntaxError", 76 "Parsing invalid JSON yeilds a SyntaxError" 77 ); 78 }