Skip to content

Commit 03f1836

Browse files
committed
Add test for clearing cache in context mutations
1 parent a58e3f5 commit 03f1836

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

library/agent/Context.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import * as t from "tap";
2+
import { extractStringsFromUserInputCached } from "../helpers/extractStringsFromUserInputCached";
23
import {
34
type Context,
45
getContext,
56
runWithContext,
67
bindContext,
8+
updateContext,
79
} from "./Context";
810

911
const sampleContext: Context = {
1012
remoteAddress: "::1",
1113
method: "POST",
1214
url: "http://localhost:4000",
13-
query: {},
15+
query: {
16+
abc: "def",
17+
},
1418
headers: {},
1519
body: undefined,
1620
cookies: {},
@@ -98,3 +102,33 @@ t.test("Get context does work with bindContext", async (t) => {
98102

99103
emitter.emit("event");
100104
});
105+
106+
t.test("it clears cache when context is mutated", async (t) => {
107+
const context = { ...sampleContext };
108+
109+
runWithContext(context, () => {
110+
t.same(extractStringsFromUserInputCached(getContext(), "body"), undefined);
111+
t.same(
112+
extractStringsFromUserInputCached(getContext(), "query"),
113+
new Map(Object.entries({ abc: ".", def: ".abc" }))
114+
);
115+
116+
updateContext(getContext(), "query", {});
117+
t.same(extractStringsFromUserInputCached(getContext(), "body"), undefined);
118+
t.same(
119+
extractStringsFromUserInputCached(getContext(), "query"),
120+
new Map(Object.entries({}))
121+
);
122+
123+
runWithContext({ ...context, body: { a: "z" }, query: { b: "y" } }, () => {
124+
t.same(
125+
extractStringsFromUserInputCached(getContext(), "body"),
126+
new Map(Object.entries({ a: ".", z: ".a" }))
127+
);
128+
t.same(
129+
extractStringsFromUserInputCached(getContext(), "query"),
130+
new Map(Object.entries({ b: ".", y: ".b" }))
131+
);
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)