From d5099b9648e6cb9356d290e7141a0e39bb2b80e7 Mon Sep 17 00:00:00 2001 From: Simon Bennetts Date: Mon, 23 Oct 2023 13:59:24 +0100 Subject: [PATCH] Fix links continually reported Fixes #81 Signed-off-by: Simon Bennetts --- CHANGELOG.md | 5 ++- source/types/ReportedModel.ts | 10 +++++ test/ContentScript/integrationTests.test.ts | 41 +++++++++++++++++++-- test/ContentScript/utils.ts | 14 ++++--- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e2bef1..c653b8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased ### Changed - - Init ZAP URL to http://zap/ instead of http://localhost:8080/ + +### Fixed +- Same links continually reported on domMutation events (Issue 81). + ## 0.0.6 - 2023-09-19 ### Fixed diff --git a/source/types/ReportedModel.ts b/source/types/ReportedModel.ts index df539a6..f97afd0 100644 --- a/source/types/ReportedModel.ts +++ b/source/types/ReportedModel.ts @@ -113,6 +113,16 @@ class ReportedElement extends ReportedObject { this.href = element.getAttribute('href'); } } + + public toShortString(): string { + return JSON.stringify(this, function replacer(k: string, v: string) { + if (k === 'timestamp') { + // No point reporting the same element lots of times + return undefined; + } + return v; + }); + } } class ReportedEvent { diff --git a/test/ContentScript/integrationTests.test.ts b/test/ContentScript/integrationTests.test.ts index f3e1d64..76b7a46 100644 --- a/test/ContentScript/integrationTests.test.ts +++ b/test/ContentScript/integrationTests.test.ts @@ -30,11 +30,11 @@ function integrationTests( ): void { let server: http.Server; let httpServer: http.Server; - const actualData = new Set(); + const actualData = new Array(); let driver: ChromeDriver | FirefoxDriver; beforeEach(async () => { - actualData.clear(); + actualData.length = 0; if (browserName === BROWSERNAME.FIREFOX) { driver = new FirefoxDriver(); } else { @@ -54,7 +54,7 @@ function integrationTests( test('Should load extension into browser', async () => { // Given / When - server = getFakeZapServer(actualData, _JSONPORT); + server = getFakeZapServer(actualData, _JSONPORT, true); const context = await driver.getContext(_JSONPORT); const page = await context.newPage(); await page.goto( @@ -289,7 +289,7 @@ function integrationTests( test('Should record set localStorage', async () => { // Given / When - server = getFakeZapServer(actualData, _JSONPORT); + server = getFakeZapServer(actualData, _JSONPORT, true); const context = await driver.getContext(_JSONPORT); const page = await context.newPage(); await page.goto( @@ -322,6 +322,39 @@ function integrationTests( '"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"sessionStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorage.html\\",\\"text\\":\\"sessionData\\"}\\",\\"apikey\\":\\"not set\\"}}"]'; expect(JSON.stringify(Array.from(actualData))).toBe(expectedData); }); + + test('Should record dup added link once ', async () => { + // Given / When + server = getFakeZapServer(actualData, _JSONPORT); + const context = await driver.getContext(_JSONPORT); + const page = await context.newPage(); + await page.goto( + `http://localhost:${_HTTPPORT}/webpages/integrationTest.html` + ); + await page.waitForLoadState('networkidle'); + + await page.evaluate(() => { + // eslint-disable-next-line func-names + const addLink = function (): void { + const aTag = document.createElement('a'); + aTag.innerHTML = 'Test link'; + aTag.href = 'https://www.example.com'; + aTag.title = 'Test link'; + document.body.appendChild(aTag); + }; + addLink(); + addLink(); + }); + await page.waitForTimeout(1000); + await page.close(); + // Then + const expectedData = + '["{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"pageLoad\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' + + '"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"nodeAdded\\",\\"tagName\\":\\"A\\",\\"id\\":\\"\\",\\"nodeName\\":\\"A\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"href\\":\\"http://localhost:1801/webpages/integrationTest.html#test\\",\\"text\\":\\"Link\\"}\\",\\"apikey\\":\\"not set\\"}}",' + + '"{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"domMutation\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' + + '"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"nodeAdded\\",\\"tagName\\":\\"A\\",\\"id\\":\\"\\",\\"nodeName\\":\\"A\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"href\\":\\"https://www.example.com/\\",\\"text\\":\\"Test link\\"}\\",\\"apikey\\":\\"not set\\"}}"]'; + expect(JSON.stringify(Array.from(actualData))).toBe(expectedData); + }); } } diff --git a/test/ContentScript/utils.ts b/test/ContentScript/utils.ts index 2fb453f..cb3f750 100644 --- a/test/ContentScript/utils.ts +++ b/test/ContentScript/utils.ts @@ -43,8 +43,9 @@ export function getStaticHttpServer(): http.Server { } export function getFakeZapServer( - actualData: Set, - JSONPORT: number + actualData: Array, + JSONPORT: number, + incZapEvents = false ): http.Server { const app = JsonServer.create(); @@ -53,9 +54,12 @@ export function getFakeZapServer( const action = req.params; const {body} = req; const msg = JSON.stringify({action, body}); - actualData.add( - msg.replace(/\\"timestamp\\":\d+/g, 'TIMESTAMP').replace(/[\\]/g, '') - ); + if (incZapEvents || msg.indexOf('localzap') === -1) { + // Ignore localzap events + actualData.push( + msg.replace(/\\"timestamp\\":\d+/g, 'TIMESTAMP').replace(/[\\]/g, '') + ); + } res.sendStatus(200); });