Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix links continually reported #82

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions source/types/ReportedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 37 additions & 4 deletions test/ContentScript/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function integrationTests(
): void {
let server: http.Server;
let httpServer: http.Server;
const actualData = new Set<string>();
const actualData = new Array<string>();
let driver: ChromeDriver | FirefoxDriver;

beforeEach(async () => {
actualData.clear();
actualData.length = 0;
if (browserName === BROWSERNAME.FIREFOX) {
driver = new FirefoxDriver();
} else {
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
});
}
}

Expand Down
14 changes: 9 additions & 5 deletions test/ContentScript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ export function getStaticHttpServer(): http.Server {
}

export function getFakeZapServer(
actualData: Set<string>,
JSONPORT: number
actualData: Array<string>,
JSONPORT: number,
incZapEvents = false
): http.Server {
const app = JsonServer.create();

Expand All @@ -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);
});

Expand Down