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

test: HAR recorded in "attach" mode should have content in the entries #21

Merged
merged 2 commits into from
May 14, 2024
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { defaultMatcher } from "./utils/matchers/defaultMatcher";
export { Matcher, AdvancedRouteFromHAR } from "./utils/types";
export { defaultMatcher } from "./utils/matchers/defaultMatcher";
export { customMatcher } from "./utils/matchers/customMatcher";
export { parseContent } from "./utils/serveFromHar";
import * as path from "path";

export const test = base.extend<{
Expand Down
8 changes: 4 additions & 4 deletions src/utils/serveFromHar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export async function serveFromHar(
async () => {
await page.route(options.url ?? /.*/, async (route) => {
let entry = typeof options.matcher === "function" ?
findEntry(har, route.request(), options!.matcher) :
(options.matcher.findEntry ?? findEntry)(har, route.request(), options!.matcher.matchFunction);
await findEntry(har, route.request(), options!.matcher) :
await (options.matcher.findEntry ?? findEntry)(har, route.request(), options!.matcher.matchFunction);
if("postProcess" in options.matcher && options.matcher.postProcess) {
entry = options.matcher.postProcess(entry, route);
}
Expand All @@ -49,13 +49,13 @@ export async function serveFromHar(
);
}

export function findEntry(
export async function findEntry(
har: Har,
request: Request,
matcher: Matcher = defaultMatcher
) {
// score each entry
const entriesWithScore = har.log.entries.map((entry) => ({ entry, score: matcher(request, entry) }));
const entriesWithScore = await Promise.all(har.log.entries.map(async (entry) => ({ entry, score: await matcher(request, entry) })));

// filter out entries with negative scores
const goodEntries = entriesWithScore.filter(({ score }) => score >= 0);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { APIResponse, Request, Route, Cookie } from "@playwright/test";
import type { Entry } from "har-format";
import type { findEntry } from "./serveFromHar";

export type Matcher = (request: Request, entry: Entry) => number;
export type Matcher = (request: Request, entry: Entry) => number | Promise<number>;

export type AdvancedMatcher = {
findEntry?: typeof findEntry;
Expand Down
8 changes: 8 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ test("record test with a joke and postprocess", async ({ page, advancedRouteFrom
await page.waitForSelector("text=This is a joke");
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");
});
20 changes: 16 additions & 4 deletions tests/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from "@playwright/test";
import { defaultMatcher, test } from "../lib/index";
import fs from "fs";
import { parseContent } from "../lib/index";

test("sanity", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/demo-todo-app.har");
Expand Down Expand Up @@ -104,7 +105,6 @@ test("ignore port number", async ({ page, advancedRouteFromHAR }) => {
) {
return 1;
}
console.log("no match", reqUrl.toString(), entryUrl.toString());
return -1;
},
});
Expand All @@ -126,7 +126,6 @@ test("ignore search params", async ({ page, advancedRouteFromHAR }) => {
) {
return 1;
}
console.log("no match", reqUrl.toString(), entryUrl.toString());
return -1;
},
});
Expand Down Expand Up @@ -171,6 +170,21 @@ async function waitForFile(path: string) {
throw "can't read file";
}

test("attached content", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/not-embedded.har", {
matcher: async (request, entry) => {
let content = await parseContent(entry.response.content, "tests/har/temp");
if(typeof content === "object")
content = content.toString();

expect(content).toBeTruthy();
expect(content).toContain("category");
return defaultMatcher(request, entry);
}
});
await page.goto("https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit");
});

test("test a joke recording with postprocess", async ({ page, advancedRouteFromHAR }) => {
await advancedRouteFromHAR("tests/har/temp/joke-postprocess.har", {
update: true,
Expand Down Expand Up @@ -210,8 +224,6 @@ test("test a postprocess that change only part of the output", async ({ page, ad
matcher: {
postProcess(entry) {
const json = JSON.parse(entry.response.content.text ?? "{}");
console.log(json);
console.log(entry.response.content.text);
json.flags.custom = true;
entry.response.content.text = JSON.stringify(json);
return entry;
Expand Down
Loading