@noodleseed/one 0.22.0 → 0.23.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.
- package/dist/commands/author-loop.d.ts.map +1 -1
- package/dist/commands/author-loop.js +2 -2
- package/dist/commands/author-loop.js.map +1 -1
- package/dist/commands/catalog-dispatch.d.ts.map +1 -1
- package/dist/commands/catalog-dispatch.js +21 -2
- package/dist/commands/catalog-dispatch.js.map +1 -1
- package/dist/commands/deploy-ops.d.ts.map +1 -1
- package/dist/commands/deploy-ops.js +28 -11
- package/dist/commands/deploy-ops.js.map +1 -1
- package/dist/first-run.d.ts +11 -0
- package/dist/first-run.d.ts.map +1 -1
- package/dist/first-run.js +19 -4
- package/dist/first-run.js.map +1 -1
- package/dist/project-scaffold-templates.d.ts +11 -0
- package/dist/project-scaffold-templates.d.ts.map +1 -0
- package/dist/project-scaffold-templates.js +359 -0
- package/dist/project-scaffold-templates.js.map +1 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +1 -323
- package/dist/project.js.map +1 -1
- package/dist/react.d.ts +2 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +1 -1
- package/dist/react.js.map +1 -1
- package/dist/validate.d.ts +12 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +11 -0
- package/dist/validate.js.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/skill-content.d.ts.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/skill-content.js +6 -9
- package/node_modules/@noodle-borg/agent-kit/dist/skill-content.js.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/package.json +1 -1
- package/node_modules/@noodle-borg/authoring/dist/index.d.ts +3 -3
- package/node_modules/@noodle-borg/authoring/dist/index.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/index.js +1 -1
- package/node_modules/@noodle-borg/authoring/dist/index.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/components.d.ts +302 -7
- package/node_modules/@noodle-borg/authoring/dist/react/components.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/components.js +39 -7
- package/node_modules/@noodle-borg/authoring/dist/react/components.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/index.d.ts +4 -2
- package/node_modules/@noodle-borg/authoring/dist/react/index.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/index.js +2 -1
- package/node_modules/@noodle-borg/authoring/dist/react/index.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.d.ts +2618 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.d.ts.map +1 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.js +110 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.js.map +1 -0
- package/node_modules/@noodle-borg/authoring/dist/react.d.ts +2 -2
- package/node_modules/@noodle-borg/authoring/dist/react.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react.js +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.d.ts +1 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.js +14 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/server.d.ts +25 -2
- package/node_modules/@noodle-borg/authoring/dist/server.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/server.js +7 -3
- package/node_modules/@noodle-borg/authoring/dist/server.js.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/compile.d.ts.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/compile.js +20 -8
- package/node_modules/@noodle-borg/compiler/dist/compile.js.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.d.ts +2 -0
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.d.ts.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.js +6 -0
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.js.map +1 -1
- package/node_modules/@noodle-borg/connector-http/package.json +1 -1
- package/node_modules/@noodle-borg/protocol/dist/mapping.d.ts.map +1 -1
- package/node_modules/@noodle-borg/protocol/dist/mapping.js +8 -0
- package/node_modules/@noodle-borg/protocol/dist/mapping.js.map +1 -1
- package/node_modules/hono/dist/adapter/aws-lambda/handler.js +15 -21
- package/node_modules/hono/dist/adapter/bun/websocket.js +3 -1
- package/node_modules/hono/dist/adapter/lambda-edge/handler.js +8 -2
- package/node_modules/hono/dist/cjs/adapter/aws-lambda/handler.js +15 -21
- package/node_modules/hono/dist/cjs/adapter/bun/websocket.js +3 -1
- package/node_modules/hono/dist/cjs/adapter/lambda-edge/handler.js +8 -2
- package/node_modules/hono/dist/cjs/helper/css/common.js +3 -1
- package/node_modules/hono/dist/cjs/helper/css/index.js +9 -1
- package/node_modules/hono/dist/cjs/jsx/base.js +8 -14
- package/node_modules/hono/dist/cjs/jsx/components.js +41 -21
- package/node_modules/hono/dist/cjs/jsx/context.js +131 -5
- package/node_modules/hono/dist/cjs/jsx/streaming.js +9 -7
- package/node_modules/hono/dist/cjs/middleware/cors/index.js +2 -5
- package/node_modules/hono/dist/cjs/middleware/serve-static/index.js +2 -2
- package/node_modules/hono/dist/cjs/utils/body.js +12 -4
- package/node_modules/hono/dist/cjs/utils/buffer.js +2 -1
- package/node_modules/hono/dist/cjs/validator/validator.js +3 -3
- package/node_modules/hono/dist/helper/css/common.js +3 -1
- package/node_modules/hono/dist/helper/css/index.js +9 -1
- package/node_modules/hono/dist/jsx/base.js +15 -15
- package/node_modules/hono/dist/jsx/components.js +42 -22
- package/node_modules/hono/dist/jsx/context.js +129 -5
- package/node_modules/hono/dist/jsx/streaming.js +10 -8
- package/node_modules/hono/dist/middleware/cors/index.js +2 -5
- package/node_modules/hono/dist/middleware/serve-static/index.js +2 -2
- package/node_modules/hono/dist/types/adapter/aws-lambda/handler.d.ts +1 -1
- package/node_modules/hono/dist/types/jsx/base.d.ts +1 -3
- package/node_modules/hono/dist/types/jsx/context.d.ts +39 -0
- package/node_modules/hono/dist/types/middleware/context-storage/index.d.ts +2 -2
- package/node_modules/hono/dist/types/utils/body.d.ts +1 -1
- package/node_modules/hono/dist/utils/body.js +12 -4
- package/node_modules/hono/dist/utils/buffer.js +2 -1
- package/node_modules/hono/dist/validator/validator.js +3 -3
- package/node_modules/hono/package.json +9 -10
- package/node_modules/tailwindcss/dist/chunk-HMCCH6MG.mjs +1 -0
- package/node_modules/tailwindcss/dist/default-theme.mjs +1 -1
- package/node_modules/tailwindcss/dist/lib.js +13 -13
- package/node_modules/tailwindcss/dist/lib.mjs +18 -18
- package/node_modules/tailwindcss/package.json +2 -2
- package/node_modules/undici/README.md +3 -2
- package/node_modules/undici/docs/docs/api/Agent.md +248 -44
- package/node_modules/undici/docs/docs/api/BalancedPool.md +246 -48
- package/node_modules/undici/docs/docs/api/CacheStorage.md +171 -13
- package/node_modules/undici/docs/docs/api/CacheStore.md +294 -98
- package/node_modules/undici/docs/docs/api/Client.md +365 -178
- package/node_modules/undici/docs/docs/api/ClientStats.md +80 -14
- package/node_modules/undici/docs/docs/api/Connector.md +118 -36
- package/node_modules/undici/docs/docs/api/ContentType.md +65 -25
- package/node_modules/undici/docs/docs/api/Cookies.md +125 -47
- package/node_modules/undici/docs/docs/api/Debug.md +34 -18
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +364 -164
- package/node_modules/undici/docs/docs/api/Dispatcher.md +479 -1093
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +101 -87
- package/node_modules/undici/docs/docs/api/Errors.md +503 -40
- package/node_modules/undici/docs/docs/api/EventSource.md +294 -32
- package/node_modules/undici/docs/docs/api/Fetch.md +680 -31
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +78 -98
- package/node_modules/undici/docs/docs/api/H2CClient.md +298 -176
- package/node_modules/undici/docs/docs/api/Interceptors.md +355 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +314 -322
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +272 -98
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +189 -26
- package/node_modules/undici/docs/docs/api/MockClient.md +151 -33
- package/node_modules/undici/docs/docs/api/MockErrors.md +56 -5
- package/node_modules/undici/docs/docs/api/MockPool.md +299 -263
- package/node_modules/undici/docs/docs/api/Pool.md +235 -43
- package/node_modules/undici/docs/docs/api/PoolStats.md +119 -13
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +179 -160
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +238 -63
- package/node_modules/undici/docs/docs/api/RetryAgent.md +113 -26
- package/node_modules/undici/docs/docs/api/RetryHandler.md +161 -77
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +166 -72
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +264 -542
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +162 -186
- package/node_modules/undici/docs/docs/api/Util.md +68 -11
- package/node_modules/undici/docs/docs/api/WebSocket.md +475 -80
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +100 -32
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +2 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +3 -1
- package/node_modules/undici/docs/docs/best-practices/migrating-from-v7-to-v8.md +6 -4
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +9 -7
- package/node_modules/undici/docs/docs/best-practices/proxy.md +3 -1
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +10 -8
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +2 -0
- package/node_modules/undici/docs/docs/{GettingStarted.md → getting-started.md} +22 -19
- package/node_modules/undici/docs/docs/index.md +779 -0
- package/node_modules/undici/docs/docs/site.json +125 -0
- package/node_modules/undici/docs/docs/type-map.json +79 -0
- package/node_modules/undici/lib/api/api-request.js +7 -1
- package/node_modules/undici/lib/api/readable.js +47 -2
- package/node_modules/undici/lib/core/errors.js +20 -0
- package/node_modules/undici/lib/core/request.js +1 -1
- package/node_modules/undici/lib/core/util.js +24 -1
- package/node_modules/undici/lib/dispatcher/client-h1.js +92 -1
- package/node_modules/undici/lib/dispatcher/client-h2.js +322 -118
- package/node_modules/undici/lib/dispatcher/client.js +6 -2
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +1 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +42 -7
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +4 -2
- package/node_modules/undici/lib/handler/redirect-handler.js +1 -0
- package/node_modules/undici/lib/handler/retry-handler.js +57 -19
- package/node_modules/undici/lib/util/cache.js +8 -2
- package/node_modules/undici/lib/web/cookies/parse.js +20 -27
- package/node_modules/undici/lib/web/cookies/util.js +1 -1
- package/node_modules/undici/lib/web/eventsource/eventsource.js +7 -18
- package/node_modules/undici/lib/web/eventsource/util.js +32 -1
- package/node_modules/undici/lib/web/fetch/body.js +43 -0
- package/node_modules/undici/lib/web/fetch/constants.js +1 -1
- package/node_modules/undici/lib/web/fetch/index.js +11 -2
- package/node_modules/undici/lib/web/fetch/request.js +1 -0
- package/node_modules/undici/lib/web/fetch/util.js +4 -1
- package/node_modules/undici/lib/web/websocket/receiver.js +20 -3
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +8 -1
- package/node_modules/undici/lib/web/websocket/websocket.js +3 -1
- package/node_modules/undici/package.json +1 -1
- package/node_modules/undici/types/client.d.ts +6 -1
- package/node_modules/undici/types/cookies.d.ts +1 -1
- package/node_modules/undici/types/errors.d.ts +10 -0
- package/node_modules/undici/types/fetch.d.ts +1 -0
- package/node_modules/undici/types/handlers.d.ts +2 -0
- package/node_modules/undici/types/proxy-agent.d.ts +7 -0
- package/package.json +4 -4
- package/react/styles.css +510 -5
- package/react/styles.source.css +850 -0
- package/node_modules/hono/dist/tsconfig.build.tsbuildinfo +0 -1
- package/node_modules/tailwindcss/dist/chunk-XVOTX5KS.mjs +0 -1
|
@@ -1,40 +1,77 @@
|
|
|
1
|
-
#
|
|
1
|
+
# MockAgent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!--introduced_in=v4.0.0-->
|
|
4
|
+
<!--type=module-->
|
|
5
|
+
<!-- source_link=lib/mock/mock-agent.js -->
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
> Stability: 2 - Stable
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
A `MockAgent` is a {Dispatcher} that intercepts HTTP requests made through
|
|
10
|
+
undici and replies with programmed mock responses instead of contacting the
|
|
11
|
+
network. It is useful for testing code that performs HTTP requests without
|
|
12
|
+
relying on a live server.
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
A `MockAgent` does not intercept requests on its own. Mock responses are
|
|
15
|
+
registered on the {MockClient} or {MockPool} instances returned by
|
|
16
|
+
[`mockAgent.get(origin)`][], and requests are routed through them once the
|
|
17
|
+
`MockAgent` is set as the dispatcher (for example through
|
|
18
|
+
[`setGlobalDispatcher()`][] or a per-request `dispatcher` option).
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
```mjs
|
|
21
|
+
import { MockAgent } from 'undici'
|
|
22
|
+
|
|
23
|
+
const mockAgent = new MockAgent()
|
|
24
|
+
```
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
## Class: `MockAgent`
|
|
14
27
|
|
|
15
|
-
|
|
28
|
+
<!-- YAML
|
|
29
|
+
added: v4.0.0
|
|
30
|
+
-->
|
|
16
31
|
|
|
17
|
-
Extends:
|
|
32
|
+
* Extends: {Dispatcher}
|
|
18
33
|
|
|
19
|
-
|
|
34
|
+
Intercepts HTTP requests made through undici and returns mocked responses.
|
|
20
35
|
|
|
21
|
-
|
|
36
|
+
### `new MockAgent([options])`
|
|
22
37
|
|
|
23
|
-
|
|
38
|
+
<!-- YAML
|
|
39
|
+
added: v4.0.0
|
|
40
|
+
changes:
|
|
41
|
+
- version: v7.16.0
|
|
42
|
+
pr-url: https://github.com/nodejs/undici/pull/4479
|
|
43
|
+
description: Mock options are always normalized to an object internally.
|
|
44
|
+
-->
|
|
24
45
|
|
|
25
|
-
|
|
46
|
+
* `options` {MockAgentOptions} (optional)
|
|
47
|
+
* Returns: {MockAgent}
|
|
26
48
|
|
|
27
|
-
|
|
49
|
+
When instantiated, a `MockAgent` is automatically activated. It does not
|
|
50
|
+
intercept any request until mock interceptors are registered on the dispatchers
|
|
51
|
+
returned by [`mockAgent.get(origin)`][].
|
|
28
52
|
|
|
29
|
-
|
|
53
|
+
#### Parameter: `MockAgentOptions`
|
|
54
|
+
|
|
55
|
+
* Extends: {AgentOptions}
|
|
56
|
+
* `agent` {Dispatcher} (optional) A custom agent to be encapsulated by the
|
|
57
|
+
`MockAgent`. It must implement the `Agent` API (that is, expose a `dispatch`
|
|
58
|
+
function). **Default:** a new {Agent} constructed from `options`.
|
|
59
|
+
* `ignoreTrailingSlash` {boolean} (optional) Whether trailing slashes in the
|
|
60
|
+
request path are ignored when matching interceptors. **Default:** `false`.
|
|
61
|
+
* `acceptNonStandardSearchParameters` {boolean} (optional) Whether the matcher
|
|
62
|
+
also accepts URLs using non-standard search-parameter syntaxes, such as
|
|
63
|
+
multi-value items written with `[]` (for example `param[]=1¶m[]=2`) or
|
|
64
|
+
comma-separated values (for example `param=1,2,3`). **Default:** `false`.
|
|
65
|
+
* `enableCallHistory` {boolean} (optional) Whether call history recording is
|
|
66
|
+
enabled. See [`mockAgent.getCallHistory()`][]. **Default:** `false`.
|
|
67
|
+
|
|
68
|
+
```mjs displayName="Basic instantiation"
|
|
30
69
|
import { MockAgent } from 'undici'
|
|
31
70
|
|
|
32
71
|
const mockAgent = new MockAgent()
|
|
33
72
|
```
|
|
34
73
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
```js
|
|
74
|
+
```mjs displayName="Instantiation with a custom agent"
|
|
38
75
|
import { Agent, MockAgent } from 'undici'
|
|
39
76
|
|
|
40
77
|
const agent = new Agent()
|
|
@@ -42,34 +79,30 @@ const agent = new Agent()
|
|
|
42
79
|
const mockAgent = new MockAgent({ agent })
|
|
43
80
|
```
|
|
44
81
|
|
|
45
|
-
|
|
82
|
+
### `mockAgent.get(origin)`
|
|
46
83
|
|
|
47
|
-
|
|
84
|
+
<!-- YAML
|
|
85
|
+
added: v4.0.0
|
|
86
|
+
-->
|
|
48
87
|
|
|
49
|
-
|
|
88
|
+
* `origin` {string|RegExp|Function} A matcher for the origin to retrieve. The
|
|
89
|
+
function form has the signature `(origin) => boolean`.
|
|
90
|
+
* Returns: {MockClient|MockPool}
|
|
50
91
|
|
|
51
|
-
|
|
92
|
+
Creates and retrieves the mock dispatcher used to intercept requests for the
|
|
93
|
+
given origin. When the `connections` option of the `MockAgent` is `1`, a
|
|
94
|
+
{MockClient} is returned; otherwise a {MockPool} is returned. Subsequent calls
|
|
95
|
+
with the same origin return the same instance.
|
|
52
96
|
|
|
53
|
-
|
|
97
|
+
The way `origin` is matched against incoming requests depends on its type:
|
|
54
98
|
|
|
55
|
-
|
|
99
|
+
| Matcher type | Condition to pass |
|
|
100
|
+
| :----------: | ---------------------------- |
|
|
101
|
+
| `string` | Exact match against the value |
|
|
102
|
+
| `RegExp` | The regular expression matches |
|
|
103
|
+
| `Function` | The function returns `true` |
|
|
56
104
|
|
|
57
|
-
|
|
58
|
-
|:------------:| -------------------------- |
|
|
59
|
-
| `string` | Exact match against string |
|
|
60
|
-
| `RegExp` | Regex must pass |
|
|
61
|
-
| `Function` | Function must return true |
|
|
62
|
-
|
|
63
|
-
Returns: `MockClient | MockPool`.
|
|
64
|
-
|
|
65
|
-
| `MockAgentOptions` | Mock instance returned |
|
|
66
|
-
| -------------------- | ---------------------- |
|
|
67
|
-
| `connections === 1` | `MockClient` |
|
|
68
|
-
| `connections` > `1` | `MockPool` |
|
|
69
|
-
|
|
70
|
-
#### Example - Basic Mocked Request
|
|
71
|
-
|
|
72
|
-
```js
|
|
105
|
+
```mjs displayName="Basic mocked request"
|
|
73
106
|
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
74
107
|
|
|
75
108
|
const mockAgent = new MockAgent()
|
|
@@ -87,9 +120,7 @@ for await (const data of body) {
|
|
|
87
120
|
}
|
|
88
121
|
```
|
|
89
122
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
```js
|
|
123
|
+
```mjs displayName="Mocked request using a local dispatcher"
|
|
93
124
|
import { MockAgent, request } from 'undici'
|
|
94
125
|
|
|
95
126
|
const mockAgent = new MockAgent()
|
|
@@ -97,32 +128,9 @@ const mockAgent = new MockAgent()
|
|
|
97
128
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
98
129
|
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
99
130
|
|
|
100
|
-
const {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
} = await request('http://localhost:3000/foo', { dispatcher: mockAgent })
|
|
104
|
-
|
|
105
|
-
console.log('response received', statusCode) // response received 200
|
|
106
|
-
|
|
107
|
-
for await (const data of body) {
|
|
108
|
-
console.log('data', data.toString('utf8')) // data foo
|
|
109
|
-
}
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
#### Example - Basic Mocked Request with local mock pool dispatcher
|
|
113
|
-
|
|
114
|
-
```js
|
|
115
|
-
import { MockAgent, request } from 'undici'
|
|
116
|
-
|
|
117
|
-
const mockAgent = new MockAgent()
|
|
118
|
-
|
|
119
|
-
const mockPool = mockAgent.get('http://localhost:3000')
|
|
120
|
-
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
121
|
-
|
|
122
|
-
const {
|
|
123
|
-
statusCode,
|
|
124
|
-
body
|
|
125
|
-
} = await request('http://localhost:3000/foo', { dispatcher: mockPool })
|
|
131
|
+
const { statusCode, body } = await request('http://localhost:3000/foo', {
|
|
132
|
+
dispatcher: mockAgent
|
|
133
|
+
})
|
|
126
134
|
|
|
127
135
|
console.log('response received', statusCode) // response received 200
|
|
128
136
|
|
|
@@ -131,9 +139,7 @@ for await (const data of body) {
|
|
|
131
139
|
}
|
|
132
140
|
```
|
|
133
141
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
```js
|
|
142
|
+
```mjs displayName="Returning a MockClient"
|
|
137
143
|
import { MockAgent, request } from 'undici'
|
|
138
144
|
|
|
139
145
|
const mockAgent = new MockAgent({ connections: 1 })
|
|
@@ -141,10 +147,9 @@ const mockAgent = new MockAgent({ connections: 1 })
|
|
|
141
147
|
const mockClient = mockAgent.get('http://localhost:3000')
|
|
142
148
|
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
143
149
|
|
|
144
|
-
const {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
} = await request('http://localhost:3000/foo', { dispatcher: mockClient })
|
|
150
|
+
const { statusCode, body } = await request('http://localhost:3000/foo', {
|
|
151
|
+
dispatcher: mockClient
|
|
152
|
+
})
|
|
148
153
|
|
|
149
154
|
console.log('response received', statusCode) // response received 200
|
|
150
155
|
|
|
@@ -153,131 +158,64 @@ for await (const data of body) {
|
|
|
153
158
|
}
|
|
154
159
|
```
|
|
155
160
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
```js
|
|
161
|
+
```mjs displayName="Matching the origin with a regular expression"
|
|
159
162
|
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
160
163
|
|
|
161
164
|
const mockAgent = new MockAgent()
|
|
162
165
|
setGlobalDispatcher(mockAgent)
|
|
163
166
|
|
|
164
|
-
const mockPool = mockAgent.get('http://localhost:3000')
|
|
167
|
+
const mockPool = mockAgent.get(new RegExp('http://localhost:3000'))
|
|
165
168
|
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
166
|
-
mockPool.intercept({ path: '/hello'}).reply(200, 'hello')
|
|
167
|
-
|
|
168
|
-
const result1 = await request('http://localhost:3000/foo')
|
|
169
|
-
|
|
170
|
-
console.log('response received', result1.statusCode) // response received 200
|
|
171
|
-
|
|
172
|
-
for await (const data of result1.body) {
|
|
173
|
-
console.log('data', data.toString('utf8')) // data foo
|
|
174
|
-
}
|
|
175
169
|
|
|
176
|
-
const
|
|
170
|
+
const { statusCode } = await request('http://localhost:3000/foo')
|
|
177
171
|
|
|
178
|
-
console.log('response received',
|
|
179
|
-
|
|
180
|
-
for await (const data of result2.body) {
|
|
181
|
-
console.log('data', data.toString('utf8')) // data hello
|
|
182
|
-
}
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
#### Example - Mock different requests within the same file
|
|
186
|
-
|
|
187
|
-
```js
|
|
188
|
-
const { MockAgent, setGlobalDispatcher } = require('undici');
|
|
189
|
-
const agent = new MockAgent();
|
|
190
|
-
agent.disableNetConnect();
|
|
191
|
-
setGlobalDispatcher(agent);
|
|
192
|
-
describe('Test', () => {
|
|
193
|
-
it('200', async () => {
|
|
194
|
-
const mockAgent = agent.get('http://test.com');
|
|
195
|
-
// your test
|
|
196
|
-
});
|
|
197
|
-
it('200', async () => {
|
|
198
|
-
const mockAgent = agent.get('http://testing.com');
|
|
199
|
-
// your test
|
|
200
|
-
});
|
|
201
|
-
});
|
|
172
|
+
console.log('response received', statusCode) // response received 200
|
|
202
173
|
```
|
|
203
174
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
```js
|
|
175
|
+
```mjs displayName="Matching the origin with a function"
|
|
207
176
|
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
208
177
|
|
|
209
178
|
const mockAgent = new MockAgent()
|
|
210
179
|
setGlobalDispatcher(mockAgent)
|
|
211
180
|
|
|
212
|
-
const mockPool = mockAgent.get('http://localhost:3000')
|
|
213
|
-
|
|
214
|
-
mockPool.intercept({
|
|
215
|
-
path: '/foo?hello=there&see=ya',
|
|
216
|
-
method: 'POST',
|
|
217
|
-
body: 'form1=data1&form2=data2'
|
|
218
|
-
}).reply(200, { foo: 'bar' }, {
|
|
219
|
-
headers: { 'content-type': 'application/json' },
|
|
220
|
-
trailers: { 'Content-MD5': 'test' }
|
|
221
|
-
})
|
|
181
|
+
const mockPool = mockAgent.get((origin) => origin === 'http://localhost:3000')
|
|
182
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
222
183
|
|
|
223
|
-
const {
|
|
224
|
-
statusCode,
|
|
225
|
-
headers,
|
|
226
|
-
trailers,
|
|
227
|
-
body
|
|
228
|
-
} = await request('http://localhost:3000/foo?hello=there&see=ya', {
|
|
229
|
-
method: 'POST',
|
|
230
|
-
body: 'form1=data1&form2=data2'
|
|
231
|
-
})
|
|
184
|
+
const { statusCode } = await request('http://localhost:3000/foo')
|
|
232
185
|
|
|
233
186
|
console.log('response received', statusCode) // response received 200
|
|
234
|
-
console.log('headers', headers) // { 'content-type': 'application/json' }
|
|
235
|
-
|
|
236
|
-
for await (const data of body) {
|
|
237
|
-
console.log('data', data.toString('utf8')) // '{"foo":"bar"}'
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
console.log('trailers', trailers) // { 'content-md5': 'test' }
|
|
241
187
|
```
|
|
242
188
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
```js
|
|
246
|
-
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
247
|
-
|
|
248
|
-
const mockAgent = new MockAgent()
|
|
249
|
-
setGlobalDispatcher(mockAgent)
|
|
250
|
-
|
|
251
|
-
const mockPool = mockAgent.get(new RegExp('http://localhost:3000'))
|
|
252
|
-
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
253
|
-
|
|
254
|
-
const {
|
|
255
|
-
statusCode,
|
|
256
|
-
body
|
|
257
|
-
} = await request('http://localhost:3000/foo')
|
|
189
|
+
### `mockAgent.dispatch(options, handler)`
|
|
258
190
|
|
|
259
|
-
|
|
191
|
+
<!-- YAML
|
|
192
|
+
added: v4.0.0
|
|
193
|
+
-->
|
|
260
194
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
```
|
|
195
|
+
* `options` {AgentDispatchOptions}
|
|
196
|
+
* `handler` {DispatchHandler}
|
|
197
|
+
* Returns: {boolean}
|
|
265
198
|
|
|
266
|
-
|
|
199
|
+
Dispatches a mocked request. This implements
|
|
200
|
+
[`Dispatcher.dispatch()`][] for the encapsulated agent: it ensures the mock
|
|
201
|
+
dispatcher for `options.origin` exists, records the call in the call history
|
|
202
|
+
when enabled, and forwards the request to the underlying agent. It is normally
|
|
203
|
+
invoked indirectly through higher-level methods such as
|
|
204
|
+
[`mockAgent.request()`][].
|
|
267
205
|
|
|
268
|
-
```
|
|
269
|
-
import { MockAgent
|
|
206
|
+
```mjs displayName="Using mockAgent.request()"
|
|
207
|
+
import { MockAgent } from 'undici'
|
|
270
208
|
|
|
271
209
|
const mockAgent = new MockAgent()
|
|
272
|
-
setGlobalDispatcher(mockAgent)
|
|
273
210
|
|
|
274
|
-
const mockPool = mockAgent.get(
|
|
211
|
+
const mockPool = mockAgent.get('http://localhost:3000')
|
|
275
212
|
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
276
213
|
|
|
277
|
-
const {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
214
|
+
const { statusCode, body } = await mockAgent.request({
|
|
215
|
+
origin: 'http://localhost:3000',
|
|
216
|
+
path: '/foo',
|
|
217
|
+
method: 'GET'
|
|
218
|
+
})
|
|
281
219
|
|
|
282
220
|
console.log('response received', statusCode) // response received 200
|
|
283
221
|
|
|
@@ -286,15 +224,19 @@ for await (const data of body) {
|
|
|
286
224
|
}
|
|
287
225
|
```
|
|
288
226
|
|
|
289
|
-
### `
|
|
227
|
+
### `mockAgent.close()`
|
|
290
228
|
|
|
291
|
-
|
|
229
|
+
<!-- YAML
|
|
230
|
+
added: v4.0.0
|
|
231
|
+
-->
|
|
292
232
|
|
|
293
|
-
Returns:
|
|
233
|
+
* Returns: {Promise<void>} Resolves once the agent and its registered mock pools
|
|
234
|
+
and clients have closed.
|
|
294
235
|
|
|
295
|
-
|
|
236
|
+
Clears the call history, closes the encapsulated agent, and waits for all
|
|
237
|
+
registered mock pools and clients to close.
|
|
296
238
|
|
|
297
|
-
```
|
|
239
|
+
```mjs displayName="Clean up after tests"
|
|
298
240
|
import { MockAgent, setGlobalDispatcher } from 'undici'
|
|
299
241
|
|
|
300
242
|
const mockAgent = new MockAgent()
|
|
@@ -303,49 +245,18 @@ setGlobalDispatcher(mockAgent)
|
|
|
303
245
|
await mockAgent.close()
|
|
304
246
|
```
|
|
305
247
|
|
|
306
|
-
### `
|
|
307
|
-
|
|
308
|
-
Implements [`Agent.dispatch(options, handlers)`](/docs/docs/api/Agent.md#parameter-agentdispatchoptions).
|
|
309
|
-
|
|
310
|
-
### `MockAgent.request(options[, callback])`
|
|
311
|
-
|
|
312
|
-
See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback).
|
|
313
|
-
|
|
314
|
-
#### Example - MockAgent request
|
|
315
|
-
|
|
316
|
-
```js
|
|
317
|
-
import { MockAgent } from 'undici'
|
|
318
|
-
|
|
319
|
-
const mockAgent = new MockAgent()
|
|
320
|
-
|
|
321
|
-
const mockPool = mockAgent.get('http://localhost:3000')
|
|
322
|
-
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
323
|
-
|
|
324
|
-
const {
|
|
325
|
-
statusCode,
|
|
326
|
-
body
|
|
327
|
-
} = await mockAgent.request({
|
|
328
|
-
origin: 'http://localhost:3000',
|
|
329
|
-
path: '/foo',
|
|
330
|
-
method: 'GET'
|
|
331
|
-
})
|
|
332
|
-
|
|
333
|
-
console.log('response received', statusCode) // response received 200
|
|
334
|
-
|
|
335
|
-
for await (const data of body) {
|
|
336
|
-
console.log('data', data.toString('utf8')) // data foo
|
|
337
|
-
}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### `MockAgent.deactivate()`
|
|
248
|
+
### `mockAgent.deactivate()`
|
|
341
249
|
|
|
342
|
-
|
|
250
|
+
<!-- YAML
|
|
251
|
+
added: v4.0.0
|
|
252
|
+
-->
|
|
343
253
|
|
|
344
|
-
Returns:
|
|
254
|
+
* Returns: {undefined}
|
|
345
255
|
|
|
346
|
-
|
|
256
|
+
Disables mocking on the `MockAgent`. While deactivated, requests are no longer
|
|
257
|
+
intercepted.
|
|
347
258
|
|
|
348
|
-
```
|
|
259
|
+
```mjs displayName="Deactivate mocking"
|
|
349
260
|
import { MockAgent, setGlobalDispatcher } from 'undici'
|
|
350
261
|
|
|
351
262
|
const mockAgent = new MockAgent()
|
|
@@ -354,15 +265,19 @@ setGlobalDispatcher(mockAgent)
|
|
|
354
265
|
mockAgent.deactivate()
|
|
355
266
|
```
|
|
356
267
|
|
|
357
|
-
### `
|
|
268
|
+
### `mockAgent.activate()`
|
|
358
269
|
|
|
359
|
-
|
|
270
|
+
<!-- YAML
|
|
271
|
+
added: v4.0.0
|
|
272
|
+
-->
|
|
360
273
|
|
|
361
|
-
Returns:
|
|
274
|
+
* Returns: {undefined}
|
|
362
275
|
|
|
363
|
-
|
|
276
|
+
Enables mocking on the `MockAgent`. A `MockAgent` is activated automatically
|
|
277
|
+
when instantiated, so this method is only required after
|
|
278
|
+
[`mockAgent.deactivate()`][] has been called.
|
|
364
279
|
|
|
365
|
-
```
|
|
280
|
+
```mjs displayName="Re-activate mocking"
|
|
366
281
|
import { MockAgent, setGlobalDispatcher } from 'undici'
|
|
367
282
|
|
|
368
283
|
const mockAgent = new MockAgent()
|
|
@@ -375,21 +290,23 @@ mockAgent.deactivate()
|
|
|
375
290
|
mockAgent.activate()
|
|
376
291
|
```
|
|
377
292
|
|
|
378
|
-
### `
|
|
293
|
+
### `mockAgent.enableNetConnect([matcher])`
|
|
379
294
|
|
|
380
|
-
|
|
295
|
+
<!-- YAML
|
|
296
|
+
added: v4.0.0
|
|
297
|
+
-->
|
|
381
298
|
|
|
382
|
-
|
|
299
|
+
* `matcher` {string|RegExp|Function} (optional) A host matcher. When a string is
|
|
300
|
+
used it should only contain the hostname and, optionally, the port. The
|
|
301
|
+
function form has the signature `(host) => boolean`. When omitted, all
|
|
302
|
+
non-matching requests are allowed to perform a real request.
|
|
303
|
+
* Returns: {undefined}
|
|
383
304
|
|
|
384
|
-
|
|
305
|
+
Defines host matchers so that requests that are not intercepted by a mock
|
|
306
|
+
dispatcher are allowed to perform a real HTTP request. Calling this method
|
|
307
|
+
multiple times with a string appends each value to the list of allowed hosts.
|
|
385
308
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
Returns: `void`
|
|
389
|
-
|
|
390
|
-
#### Example - Allow all non-matching urls to be dispatched in a real HTTP request
|
|
391
|
-
|
|
392
|
-
```js
|
|
309
|
+
```mjs displayName="Allow all non-matching requests"
|
|
393
310
|
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
394
311
|
|
|
395
312
|
const mockAgent = new MockAgent()
|
|
@@ -401,9 +318,7 @@ await request('http://example.com')
|
|
|
401
318
|
// A real request is made
|
|
402
319
|
```
|
|
403
320
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
```js
|
|
321
|
+
```mjs displayName="Allow requests matching specific hosts"
|
|
407
322
|
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
408
323
|
|
|
409
324
|
const mockAgent = new MockAgent()
|
|
@@ -422,75 +337,167 @@ await request('http://example-3.com')
|
|
|
422
337
|
// Will throw
|
|
423
338
|
```
|
|
424
339
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
```js
|
|
340
|
+
```mjs displayName="Allow requests matching a regular expression or function"
|
|
428
341
|
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
429
342
|
|
|
430
343
|
const mockAgent = new MockAgent()
|
|
431
344
|
setGlobalDispatcher(mockAgent)
|
|
432
345
|
|
|
433
346
|
mockAgent.enableNetConnect(new RegExp('example.com'))
|
|
347
|
+
mockAgent.enableNetConnect((host) => host === 'example.org')
|
|
434
348
|
|
|
435
349
|
await request('http://example.com')
|
|
436
350
|
// A real request is made
|
|
437
351
|
```
|
|
438
352
|
|
|
439
|
-
|
|
353
|
+
### `mockAgent.disableNetConnect()`
|
|
440
354
|
|
|
441
|
-
|
|
442
|
-
|
|
355
|
+
<!-- YAML
|
|
356
|
+
added: v4.0.0
|
|
357
|
+
-->
|
|
358
|
+
|
|
359
|
+
* Returns: {undefined}
|
|
360
|
+
|
|
361
|
+
Causes every request that is not matched by a mock interceptor to throw,
|
|
362
|
+
disallowing real HTTP requests.
|
|
363
|
+
|
|
364
|
+
```mjs displayName="Throw on every non-matching request"
|
|
365
|
+
import { MockAgent, request } from 'undici'
|
|
443
366
|
|
|
444
367
|
const mockAgent = new MockAgent()
|
|
445
|
-
setGlobalDispatcher(mockAgent)
|
|
446
368
|
|
|
447
|
-
mockAgent.
|
|
369
|
+
mockAgent.disableNetConnect()
|
|
448
370
|
|
|
449
371
|
await request('http://example.com')
|
|
450
|
-
//
|
|
372
|
+
// Will throw
|
|
451
373
|
```
|
|
452
374
|
|
|
453
|
-
### `
|
|
375
|
+
### `mockAgent.enableCallHistory()`
|
|
454
376
|
|
|
455
|
-
|
|
377
|
+
<!-- YAML
|
|
378
|
+
added: v7.5.0
|
|
379
|
+
-->
|
|
456
380
|
|
|
457
|
-
Returns: `
|
|
381
|
+
* Returns: {MockAgent} The same `MockAgent` instance, for chaining.
|
|
458
382
|
|
|
459
|
-
|
|
383
|
+
Enables call history recording. Once enabled, subsequent calls are registered
|
|
384
|
+
and can be retrieved through [`mockAgent.getCallHistory()`][]. Call history can
|
|
385
|
+
also be enabled at construction time with the `enableCallHistory` option.
|
|
460
386
|
|
|
461
|
-
```
|
|
462
|
-
import { MockAgent
|
|
387
|
+
```mjs
|
|
388
|
+
import { MockAgent } from 'undici'
|
|
463
389
|
|
|
464
390
|
const mockAgent = new MockAgent()
|
|
465
391
|
|
|
466
|
-
mockAgent.
|
|
392
|
+
mockAgent.enableCallHistory()
|
|
393
|
+
```
|
|
467
394
|
|
|
468
|
-
|
|
469
|
-
|
|
395
|
+
### `mockAgent.disableCallHistory()`
|
|
396
|
+
|
|
397
|
+
<!-- YAML
|
|
398
|
+
added: v7.5.0
|
|
399
|
+
-->
|
|
400
|
+
|
|
401
|
+
* Returns: {MockAgent} The same `MockAgent` instance, for chaining.
|
|
402
|
+
|
|
403
|
+
Disables call history recording. Subsequent calls are no longer registered.
|
|
404
|
+
|
|
405
|
+
```mjs
|
|
406
|
+
import { MockAgent } from 'undici'
|
|
407
|
+
|
|
408
|
+
const mockAgent = new MockAgent({ enableCallHistory: true })
|
|
409
|
+
|
|
410
|
+
mockAgent.disableCallHistory()
|
|
470
411
|
```
|
|
471
412
|
|
|
472
|
-
### `
|
|
413
|
+
### `mockAgent.getCallHistory()`
|
|
414
|
+
|
|
415
|
+
<!-- YAML
|
|
416
|
+
added: v7.5.0
|
|
417
|
+
-->
|
|
418
|
+
|
|
419
|
+
* Returns: {MockCallHistory|undefined} The {MockCallHistory} instance, or
|
|
420
|
+
`undefined` when call history is not enabled.
|
|
473
421
|
|
|
474
|
-
|
|
422
|
+
Returns the call history instance, which records every request made through the
|
|
423
|
+
`MockAgent` (whether intercepted or not). Call history is not enabled by
|
|
424
|
+
default; enable it with the `enableCallHistory` option or
|
|
425
|
+
[`mockAgent.enableCallHistory()`][].
|
|
475
426
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
427
|
+
```mjs displayName="Inspect recorded calls"
|
|
428
|
+
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
429
|
+
|
|
430
|
+
const mockAgent = new MockAgent({ enableCallHistory: true })
|
|
431
|
+
setGlobalDispatcher(mockAgent)
|
|
432
|
+
|
|
433
|
+
await request('http://example.com', { query: { item: 1 } })
|
|
434
|
+
|
|
435
|
+
mockAgent.getCallHistory()?.firstCall()
|
|
436
|
+
// Returns
|
|
437
|
+
// MockCallHistoryLog {
|
|
438
|
+
// body: undefined,
|
|
439
|
+
// headers: undefined,
|
|
440
|
+
// method: 'GET',
|
|
441
|
+
// origin: 'http://example.com',
|
|
442
|
+
// fullUrl: 'http://example.com/?item=1',
|
|
443
|
+
// path: '/',
|
|
444
|
+
// searchParams: { item: '1' },
|
|
445
|
+
// protocol: 'http:',
|
|
446
|
+
// host: 'example.com',
|
|
447
|
+
// port: ''
|
|
448
|
+
// }
|
|
449
|
+
```
|
|
479
450
|
|
|
480
|
-
|
|
451
|
+
### `mockAgent.clearCallHistory()`
|
|
481
452
|
|
|
482
|
-
|
|
453
|
+
<!-- YAML
|
|
454
|
+
added: v7.5.0
|
|
455
|
+
-->
|
|
483
456
|
|
|
484
|
-
|
|
485
|
-
const agent = new MockAgent()
|
|
486
|
-
agent.disableNetConnect()
|
|
457
|
+
* Returns: {undefined}
|
|
487
458
|
|
|
488
|
-
|
|
459
|
+
Clears the call history, deleting every recorded {MockCallHistoryLog} on the
|
|
460
|
+
{MockCallHistory} instance. It is a no-op when call history has never been
|
|
461
|
+
enabled.
|
|
462
|
+
|
|
463
|
+
```mjs
|
|
464
|
+
import { MockAgent } from 'undici'
|
|
465
|
+
|
|
466
|
+
const mockAgent = new MockAgent({ enableCallHistory: true })
|
|
467
|
+
|
|
468
|
+
mockAgent.clearCallHistory()
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
### `mockAgent.pendingInterceptors()`
|
|
472
|
+
|
|
473
|
+
<!-- YAML
|
|
474
|
+
added: v5.1.0
|
|
475
|
+
-->
|
|
476
|
+
|
|
477
|
+
* Returns: {PendingInterceptor[]} A `PendingInterceptor` is a `MockDispatch`
|
|
478
|
+
with an additional `origin` {string} property.
|
|
479
|
+
|
|
480
|
+
Returns the interceptors registered on the `MockAgent` that are still pending.
|
|
481
|
+
An interceptor is pending when it meets one of the following criteria:
|
|
482
|
+
|
|
483
|
+
* It is registered with neither `.times(<number>)` nor `.persist()` and has not
|
|
484
|
+
been invoked.
|
|
485
|
+
* It is persistent (registered with `.persist()`) and has not been invoked.
|
|
486
|
+
* It is registered with `.times(<number>)` and has not been invoked `<number>`
|
|
487
|
+
times.
|
|
488
|
+
|
|
489
|
+
```mjs displayName="List pending interceptors"
|
|
490
|
+
import { MockAgent } from 'undici'
|
|
491
|
+
|
|
492
|
+
const mockAgent = new MockAgent()
|
|
493
|
+
mockAgent.disableNetConnect()
|
|
494
|
+
|
|
495
|
+
mockAgent
|
|
489
496
|
.get('https://example.com')
|
|
490
497
|
.intercept({ method: 'GET', path: '/' })
|
|
491
498
|
.reply(200)
|
|
492
499
|
|
|
493
|
-
const pendingInterceptors =
|
|
500
|
+
const pendingInterceptors = mockAgent.pendingInterceptors()
|
|
494
501
|
// Returns [
|
|
495
502
|
// {
|
|
496
503
|
// timesInvoked: 0,
|
|
@@ -514,26 +521,35 @@ const pendingInterceptors = agent.pendingInterceptors()
|
|
|
514
521
|
// ]
|
|
515
522
|
```
|
|
516
523
|
|
|
517
|
-
### `
|
|
524
|
+
### `mockAgent.assertNoPendingInterceptors([options])`
|
|
525
|
+
|
|
526
|
+
<!-- YAML
|
|
527
|
+
added: v5.1.0
|
|
528
|
+
-->
|
|
518
529
|
|
|
519
|
-
|
|
530
|
+
* `options` {Object} (optional)
|
|
531
|
+
* `pendingInterceptorsFormatter` {Object} An object exposing a
|
|
532
|
+
`format(pendingInterceptors)` method used to render the pending interceptors
|
|
533
|
+
in the thrown error message. **Default:** a built-in formatter that prints a
|
|
534
|
+
table.
|
|
535
|
+
* Returns: {undefined}
|
|
520
536
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
537
|
+
Throws an {UndiciError} when the `MockAgent` has any pending interceptors. The
|
|
538
|
+
criteria for an interceptor being pending are the same as for
|
|
539
|
+
[`mockAgent.pendingInterceptors()`][].
|
|
524
540
|
|
|
525
|
-
|
|
541
|
+
```mjs displayName="Assert there are no pending interceptors"
|
|
542
|
+
import { MockAgent } from 'undici'
|
|
526
543
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
agent.disableNetConnect()
|
|
544
|
+
const mockAgent = new MockAgent()
|
|
545
|
+
mockAgent.disableNetConnect()
|
|
530
546
|
|
|
531
|
-
|
|
547
|
+
mockAgent
|
|
532
548
|
.get('https://example.com')
|
|
533
549
|
.intercept({ method: 'GET', path: '/' })
|
|
534
550
|
.reply(200)
|
|
535
551
|
|
|
536
|
-
|
|
552
|
+
mockAgent.assertNoPendingInterceptors()
|
|
537
553
|
// Throws an UndiciError with the following message:
|
|
538
554
|
//
|
|
539
555
|
// 1 interceptor is pending:
|
|
@@ -545,59 +561,35 @@ agent.assertNoPendingInterceptors()
|
|
|
545
561
|
// └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘
|
|
546
562
|
```
|
|
547
563
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
You can register every call made within a MockAgent to be able to retrieve the body, headers and so on.
|
|
551
|
-
|
|
552
|
-
This is not enabled by default.
|
|
553
|
-
|
|
554
|
-
```js
|
|
555
|
-
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
564
|
+
### `mockAgent.isMockActive`
|
|
556
565
|
|
|
557
|
-
|
|
558
|
-
|
|
566
|
+
<!-- YAML
|
|
567
|
+
added: v5.3.0
|
|
568
|
+
-->
|
|
559
569
|
|
|
560
|
-
|
|
570
|
+
* Type: {boolean} `true` while mocking is active, `false` after
|
|
571
|
+
[`mockAgent.deactivate()`][] has been called.
|
|
561
572
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
// MockCallHistoryLog {
|
|
565
|
-
// body: undefined,
|
|
566
|
-
// headers: undefined,
|
|
567
|
-
// method: 'GET',
|
|
568
|
-
// origin: 'http://example.com',
|
|
569
|
-
// fullUrl: 'http://example.com/?item=1',
|
|
570
|
-
// path: '/',
|
|
571
|
-
// searchParams: { item: '1' },
|
|
572
|
-
// protocol: 'http:',
|
|
573
|
-
// host: 'example.com',
|
|
574
|
-
// port: ''
|
|
575
|
-
// }
|
|
576
|
-
```
|
|
573
|
+
A read-only property indicating whether mocking is currently active on the
|
|
574
|
+
`MockAgent`.
|
|
577
575
|
|
|
578
|
-
|
|
576
|
+
```mjs
|
|
577
|
+
import { MockAgent } from 'undici'
|
|
579
578
|
|
|
580
|
-
```js
|
|
581
579
|
const mockAgent = new MockAgent()
|
|
582
580
|
|
|
583
|
-
mockAgent.
|
|
584
|
-
```
|
|
585
|
-
|
|
586
|
-
#### Example - call history instance class method
|
|
581
|
+
console.log(mockAgent.isMockActive) // true
|
|
587
582
|
|
|
588
|
-
|
|
589
|
-
const mockAgent = new MockAgent()
|
|
583
|
+
mockAgent.deactivate()
|
|
590
584
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
mockAgentHistory?.calls() // returns an array of MockCallHistoryLogs
|
|
594
|
-
mockAgentHistory?.firstCall() // returns the first MockCallHistoryLogs or undefined
|
|
595
|
-
mockAgentHistory?.lastCall() // returns the last MockCallHistoryLogs or undefined
|
|
596
|
-
mockAgentHistory?.nthCall(3) // returns the third MockCallHistoryLogs or undefined
|
|
597
|
-
mockAgentHistory?.filterCalls({ path: '/endpoint', hash: '#hash-value' }) // returns an Array of MockCallHistoryLogs WHERE path === /endpoint OR hash === #hash-value
|
|
598
|
-
mockAgentHistory?.filterCalls({ path: '/endpoint', hash: '#hash-value' }, { operator: 'AND' }) // returns an Array of MockCallHistoryLogs WHERE path === /endpoint AND hash === #hash-value
|
|
599
|
-
mockAgentHistory?.filterCalls(/"data": "{}"/) // returns an Array of MockCallHistoryLogs where any value match regexp
|
|
600
|
-
mockAgentHistory?.filterCalls('application/json') // returns an Array of MockCallHistoryLogs where any value === 'application/json'
|
|
601
|
-
mockAgentHistory?.filterCalls((log) => log.path === '/endpoint') // returns an Array of MockCallHistoryLogs when given function returns true
|
|
602
|
-
mockAgentHistory?.clear() // clear the history
|
|
585
|
+
console.log(mockAgent.isMockActive) // false
|
|
603
586
|
```
|
|
587
|
+
|
|
588
|
+
[`Dispatcher.dispatch()`]: Dispatcher.md#dispatcherdispatchoptions-handler
|
|
589
|
+
[`mockAgent.deactivate()`]: #mockagentdeactivate
|
|
590
|
+
[`mockAgent.enableCallHistory()`]: #mockagentenablecallhistory
|
|
591
|
+
[`mockAgent.get(origin)`]: #mockagentgetorigin
|
|
592
|
+
[`mockAgent.getCallHistory()`]: #mockagentgetcallhistory
|
|
593
|
+
[`mockAgent.pendingInterceptors()`]: #mockagentpendinginterceptors
|
|
594
|
+
[`mockAgent.request()`]: Dispatcher.md#dispatcherrequestoptions-callback
|
|
595
|
+
[`setGlobalDispatcher()`]: Dispatcher.md#setglobaldispatcherdispatcher
|