|
1 | 1 | import * as t from "tap";
|
| 2 | +import { extractStringsFromUserInputCached } from "../helpers/extractStringsFromUserInputCached"; |
2 | 3 | import {
|
3 | 4 | type Context,
|
4 | 5 | getContext,
|
5 | 6 | runWithContext,
|
6 | 7 | bindContext,
|
| 8 | + updateContext, |
7 | 9 | } from "./Context";
|
8 | 10 |
|
9 | 11 | const sampleContext: Context = {
|
10 | 12 | remoteAddress: "::1",
|
11 | 13 | method: "POST",
|
12 | 14 | url: "http://localhost:4000",
|
13 |
| - query: {}, |
| 15 | + query: { |
| 16 | + abc: "def", |
| 17 | + }, |
14 | 18 | headers: {},
|
15 | 19 | body: undefined,
|
16 | 20 | cookies: {},
|
@@ -98,3 +102,33 @@ t.test("Get context does work with bindContext", async (t) => {
|
98 | 102 |
|
99 | 103 | emitter.emit("event");
|
100 | 104 | });
|
| 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