@webdecoy/node 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ import { c as WebDecoyConfig, J as RequestMetadata, W as WebDecoy, H as ProtectResult, N as RuleState } from './sdk-DjeCBrHu.mjs';
2
+
3
+ /**
4
+ * Testing utilities, for the application's test suite rather than ours.
5
+ *
6
+ * WHY THIS EXISTS
7
+ *
8
+ * The SDK has hundreds of tests and a customer had none of them. There was no
9
+ * supported way to write "assert this request would be denied" against your own
10
+ * rules, so the first time anyone learned what the middleware does to their
11
+ * traffic was in production — which is also why `mode: 'monitor'` had to become
12
+ * the default.
13
+ *
14
+ * Three things make that test hard to write by hand, and each is solved here:
15
+ *
16
+ * - **Assembling `RequestMetadata`** is seven fields of boilerplate per case.
17
+ * - **Rate-limit tests would have to sleep.** A window is real time, and a test
18
+ * that waits sixty seconds is a test nobody runs.
19
+ * - **An API key in the environment turns a unit test into a network call**, and
20
+ * worse, files test traffic as real detections in the customer's dashboard.
21
+ *
22
+ * Import from `@webdecoy/node/testing` so none of it reaches production bundles.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { createTestHarness, get, expectDenied } from '@webdecoy/node/testing';
27
+ *
28
+ * const wd = createTestHarness({ rules: [tripwire()] });
29
+ *
30
+ * expectDenied(await wd.protect(get('/.env')), { rule: 'tripwire' });
31
+ * expectAllowed(await wd.protect(get('/')));
32
+ * ```
33
+ */
34
+
35
+ /** Build request metadata without seven lines of boilerplate. */
36
+ declare function request(over?: Partial<RequestMetadata>): RequestMetadata;
37
+ /** `GET path`, with an optional query string taken from the path. */
38
+ declare function get(path: string, over?: Partial<RequestMetadata>): RequestMetadata;
39
+ /** `POST path`, with an optional text body. */
40
+ declare function post(path: string, body?: string, over?: Partial<RequestMetadata>): RequestMetadata;
41
+ /** A request that presents itself as a named bot. */
42
+ declare function botRequest(userAgent: string, over?: Partial<RequestMetadata>): RequestMetadata;
43
+ interface TestHarnessOptions extends WebDecoyConfig {
44
+ /**
45
+ * Allow the harness to reach the network. Off by default.
46
+ *
47
+ * A `WEBDECOY_API_KEY` in the environment would otherwise turn a unit test
48
+ * into a live call — slow, flaky, and it files test traffic as real detections
49
+ * in the customer's dashboard.
50
+ */
51
+ allowNetwork?: boolean;
52
+ }
53
+ /**
54
+ * An SDK wired for tests: offline, silent, and with its own rule state.
55
+ *
56
+ * Each harness gets fresh in-memory stores, so rate-limit counters do not leak
57
+ * between test cases the way a shared module-level SDK's would.
58
+ */
59
+ declare function createTestHarness(options?: TestHarnessOptions): WebDecoy;
60
+ interface DeniedExpectation {
61
+ /** The rule that must have denied, e.g. `'tripwire'`. */
62
+ rule?: string;
63
+ /** A substring or pattern the reason must match. */
64
+ reason?: string | RegExp;
65
+ }
66
+ /**
67
+ * Assert the request was denied.
68
+ *
69
+ * The failure message prints every rule and its state, because "expected false
70
+ * to be true" tells you nothing about which of six rules was supposed to fire.
71
+ */
72
+ declare function expectDenied(decision: ProtectResult, expected?: DeniedExpectation): void;
73
+ /** Assert the request was allowed. `ERROR` fails: it means no verdict was reached. */
74
+ declare function expectAllowed(decision: ProtectResult): void;
75
+ /** Assert a named rule reached a particular state — `DRY_RUN`, `NOT_RUN`, `CACHED`. */
76
+ declare function expectRuleState(decision: ProtectResult, rule: string, state: RuleState): void;
77
+ /**
78
+ * Run `count` requests and return their conclusions.
79
+ *
80
+ * For rate-limit tests, which are otherwise a loop everyone writes slightly
81
+ * differently.
82
+ */
83
+ declare function protectMany(sdk: WebDecoy, metadata: RequestMetadata | (() => RequestMetadata), count: number): Promise<ProtectResult[]>;
84
+
85
+ export { type DeniedExpectation, type TestHarnessOptions, botRequest, createTestHarness, expectAllowed, expectDenied, expectRuleState, get, post, protectMany, request };
@@ -0,0 +1,85 @@
1
+ import { c as WebDecoyConfig, J as RequestMetadata, W as WebDecoy, H as ProtectResult, N as RuleState } from './sdk-DjeCBrHu.js';
2
+
3
+ /**
4
+ * Testing utilities, for the application's test suite rather than ours.
5
+ *
6
+ * WHY THIS EXISTS
7
+ *
8
+ * The SDK has hundreds of tests and a customer had none of them. There was no
9
+ * supported way to write "assert this request would be denied" against your own
10
+ * rules, so the first time anyone learned what the middleware does to their
11
+ * traffic was in production — which is also why `mode: 'monitor'` had to become
12
+ * the default.
13
+ *
14
+ * Three things make that test hard to write by hand, and each is solved here:
15
+ *
16
+ * - **Assembling `RequestMetadata`** is seven fields of boilerplate per case.
17
+ * - **Rate-limit tests would have to sleep.** A window is real time, and a test
18
+ * that waits sixty seconds is a test nobody runs.
19
+ * - **An API key in the environment turns a unit test into a network call**, and
20
+ * worse, files test traffic as real detections in the customer's dashboard.
21
+ *
22
+ * Import from `@webdecoy/node/testing` so none of it reaches production bundles.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { createTestHarness, get, expectDenied } from '@webdecoy/node/testing';
27
+ *
28
+ * const wd = createTestHarness({ rules: [tripwire()] });
29
+ *
30
+ * expectDenied(await wd.protect(get('/.env')), { rule: 'tripwire' });
31
+ * expectAllowed(await wd.protect(get('/')));
32
+ * ```
33
+ */
34
+
35
+ /** Build request metadata without seven lines of boilerplate. */
36
+ declare function request(over?: Partial<RequestMetadata>): RequestMetadata;
37
+ /** `GET path`, with an optional query string taken from the path. */
38
+ declare function get(path: string, over?: Partial<RequestMetadata>): RequestMetadata;
39
+ /** `POST path`, with an optional text body. */
40
+ declare function post(path: string, body?: string, over?: Partial<RequestMetadata>): RequestMetadata;
41
+ /** A request that presents itself as a named bot. */
42
+ declare function botRequest(userAgent: string, over?: Partial<RequestMetadata>): RequestMetadata;
43
+ interface TestHarnessOptions extends WebDecoyConfig {
44
+ /**
45
+ * Allow the harness to reach the network. Off by default.
46
+ *
47
+ * A `WEBDECOY_API_KEY` in the environment would otherwise turn a unit test
48
+ * into a live call — slow, flaky, and it files test traffic as real detections
49
+ * in the customer's dashboard.
50
+ */
51
+ allowNetwork?: boolean;
52
+ }
53
+ /**
54
+ * An SDK wired for tests: offline, silent, and with its own rule state.
55
+ *
56
+ * Each harness gets fresh in-memory stores, so rate-limit counters do not leak
57
+ * between test cases the way a shared module-level SDK's would.
58
+ */
59
+ declare function createTestHarness(options?: TestHarnessOptions): WebDecoy;
60
+ interface DeniedExpectation {
61
+ /** The rule that must have denied, e.g. `'tripwire'`. */
62
+ rule?: string;
63
+ /** A substring or pattern the reason must match. */
64
+ reason?: string | RegExp;
65
+ }
66
+ /**
67
+ * Assert the request was denied.
68
+ *
69
+ * The failure message prints every rule and its state, because "expected false
70
+ * to be true" tells you nothing about which of six rules was supposed to fire.
71
+ */
72
+ declare function expectDenied(decision: ProtectResult, expected?: DeniedExpectation): void;
73
+ /** Assert the request was allowed. `ERROR` fails: it means no verdict was reached. */
74
+ declare function expectAllowed(decision: ProtectResult): void;
75
+ /** Assert a named rule reached a particular state — `DRY_RUN`, `NOT_RUN`, `CACHED`. */
76
+ declare function expectRuleState(decision: ProtectResult, rule: string, state: RuleState): void;
77
+ /**
78
+ * Run `count` requests and return their conclusions.
79
+ *
80
+ * For rate-limit tests, which are otherwise a loop everyone writes slightly
81
+ * differently.
82
+ */
83
+ declare function protectMany(sdk: WebDecoy, metadata: RequestMetadata | (() => RequestMetadata), count: number): Promise<ProtectResult[]>;
84
+
85
+ export { type DeniedExpectation, type TestHarnessOptions, botRequest, createTestHarness, expectAllowed, expectDenied, expectRuleState, get, post, protectMany, request };