Skip to content

Commit

Permalink
fix: filter out API requests (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamGaash authored Dec 23, 2024
1 parent 7e44859 commit 0782b39
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/utils/serveFromHar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export async function findEntry(
matcher: Matcher = defaultMatcher
) {
// score each entry
const entriesWithScore = await Promise.all(har.log.entries.map(async (entry) => ({ entry, score: await matcher(request, entry) })));
const entriesWithScore = await Promise.all(har.log.entries
.filter(entry => !entry._apiRequest)
.map(async (entry) => ({ entry, score: await matcher(request, entry) }))
);

// filter out entries with negative scores
const goodEntries = entriesWithScore.filter(({ score }) => score >= 0);
Expand Down
17 changes: 16 additions & 1 deletion tests/setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,25 @@ test("record test with a joke and postprocess", async ({ page, advancedRouteFrom
await page.close();
});


test("record attached (not embedded)", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/not-embedded.har", {
update: true,
});
await page.goto("https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit");
});

test("record without sensitive data", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/no-sensitive-data.har", {
update: true,
updateContent: "embed",
updateMode: "minimal",
matcher: {
postProcess(entry) {
entry.response.content.text = entry.response.content.text?.replace("top secret", "public");
return entry;
}
},
});

await page.goto("https://dummyjson.com/http/200/top%20secret");
});
13 changes: 13 additions & 0 deletions tests/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,17 @@ test("test a postprocess that change only part of the output", async ({ page, ad
await page.close();
});

test("sensitive data", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/no-sensitive-data.har");
await page.goto("https://dummyjson.com/http/200/top%20secret");
await expect(page.locator("text=public")).toBeVisible();
await expect(page.locator("text=top secret")).not.toBeVisible();
await page.close();
});

// TODO: if minimal mode won't save api requests, this test will pass.
test.fail("sensitive data file should not contain sensitive data", async () => {
const data = await waitForFile("tests/har/temp/no-sensitive-data.har");
expect(data).not.toContain("top secret");
});

0 comments on commit 0782b39

Please sign in to comment.