/** Tests that various methods succeed or fail before and after HAR population. */ @Test public void harPopulationState() throws Exception { HarObject har = new HarObject(); try { JSONObject harJson = har.getHar(); fail(); } catch (IllegalStateException e) { /* Exception is expected since HAR has not been populated yet. */ } try { JSONObject pageTimings = har.getPageTimings(); fail(); } catch (IllegalStateException e) { /* Exception is expected since HAR has not been populated yet. */ } TestUtils.populateTestHar(devtoolsMessageFactory, har, DEVTOOLS_MESSAGES_JSON); har.createHarFromMessages(); // These should now succeed JSONObject harJson = har.getHar(); JSONObject pageTimings = har.getPageTimings(); try { har.addResourceContentMessage(synthesizeResourceContentResponseMessage(), "23255.2"); fail(); } catch (IllegalStateException e) { /* Exception is expected because HAR already populated */ } try { TestUtils.populateTestHar(devtoolsMessageFactory, har, DEVTOOLS_MESSAGES_JSON); fail(); } catch (IllegalStateException e) { /* Exception is expected because HAR already populated */ } try { har.createHarFromMessages(); fail(); } catch (IllegalStateException e) { /* Exception is expected because HAR already populated */ } har.clearData(); try { harJson = har.getHar(); fail(); } catch (IllegalStateException e) { /* Exception is expected since HAR has been cleared */ } }
/** Tests that a synthetic HarObject contains the correct content. */ @Test public void harContent() throws Exception { HarObject har = new HarObject(); TestUtils.populateTestHar(devtoolsMessageFactory, har, DEVTOOLS_MESSAGES_JSON); har.createHarFromMessages(); JSONObject harComparison = TestUtils.loadJsonFromResource(GOLDEN_HAR); JSONObject harObj = har.getHar(); JSONObject harlog = (JSONObject) harObj.get("log"); JSONArray entries = (JSONArray) harlog.get("entries"); JSONObject entry = (JSONObject) entries.get(3); JSONObject response = (JSONObject) entry.get("response"); JSONObject content = (JSONObject) response.get("content"); JSONArray pages = (JSONArray) harlog.get("pages"); JSONObject page = (JSONObject) pages.get(0); JSONObject charlog = (JSONObject) harComparison.get("log"); JSONArray cpages = (JSONArray) charlog.get("pages"); JSONObject cpage = (JSONObject) cpages.get(0); String ctitle = (String) cpage.get("title"); JSONArray cpageTimings = (JSONArray) cpage.get("pageTimings"); JSONObject cpageTiming = (JSONObject) cpageTimings.get(0); Long cnavStartTime = (Long) cpageTiming.get("navigationStartTime"); Long conContentLoad = (Long) cpageTiming.get("onContentLoad"); Long conLoad = (Long) cpageTiming.get("onLoad"); JSONArray devtools = (JSONArray) harlog.get("_chrome_devtools_log"); int devtoolsMessageCount = 0; for (Object msg : devtools) { if (msg instanceof JSONObject) { devtoolsMessageCount++; } } // invariant: bodySize + compression == size long bodySize = (Long) response.get("bodySize"); long size = (Long) content.get("size"); long compression = (Long) content.get("compression"); assertEquals(2208L, bodySize); assertEquals(6929L, size); assertEquals(4721L, compression); assertEquals(size, bodySize + compression); assertEquals(har.getFirstRequest().getDocumentUrl(), ctitle); assertEquals(1314123547541L, har.getPageTimings().get("navigationStartTime")); assertEquals(1021L, har.getPageTimings().get("onContentLoad")); assertEquals(1120L, har.getPageTimings().get("onLoad")); assertEquals(25, devtools.size()); }