Skip to content

Commit

Permalink
Add test for clearing cache in context mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Jul 25, 2024
1 parent a58e3f5 commit 03f1836
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion library/agent/Context.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import * as t from "tap";
import { extractStringsFromUserInputCached } from "../helpers/extractStringsFromUserInputCached";
import {
type Context,
getContext,
runWithContext,
bindContext,
updateContext,
} from "./Context";

const sampleContext: Context = {
remoteAddress: "::1",
method: "POST",
url: "http://localhost:4000",
query: {},
query: {
abc: "def",
},
headers: {},
body: undefined,
cookies: {},
Expand Down Expand Up @@ -98,3 +102,33 @@ t.test("Get context does work with bindContext", async (t) => {

emitter.emit("event");
});

t.test("it clears cache when context is mutated", async (t) => {
const context = { ...sampleContext };

runWithContext(context, () => {
t.same(extractStringsFromUserInputCached(getContext(), "body"), undefined);
t.same(
extractStringsFromUserInputCached(getContext(), "query"),
new Map(Object.entries({ abc: ".", def: ".abc" }))
);

updateContext(getContext(), "query", {});
t.same(extractStringsFromUserInputCached(getContext(), "body"), undefined);
t.same(
extractStringsFromUserInputCached(getContext(), "query"),
new Map(Object.entries({}))
);

runWithContext({ ...context, body: { a: "z" }, query: { b: "y" } }, () => {
t.same(
extractStringsFromUserInputCached(getContext(), "body"),
new Map(Object.entries({ a: ".", z: ".a" }))
);
t.same(
extractStringsFromUserInputCached(getContext(), "query"),
new Map(Object.entries({ b: ".", y: ".b" }))
);
});
});
});

0 comments on commit 03f1836

Please sign in to comment.