11cb0ef41Sopenharmony_ci# Class: MockAgent 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciExtends: `undici.Dispatcher` 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciA mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci## `new MockAgent([options])` 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciArguments: 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci* **options** `MockAgentOptions` (optional) - It extends the `Agent` options. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciReturns: `MockAgent` 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci### Parameter: `MockAgentOptions` 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ciExtends: [`AgentOptions`](Agent.md#parameter-agentoptions) 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci* **agent** `Agent` (optional) - Default: `new Agent([options])` - a custom agent encapsulated by the MockAgent. 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci### Example - Basic MockAgent instantiation 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciThis will instantiate the MockAgent. It will not do anything until registered as the agent to use with requests and mock interceptions are added. 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci```js 261cb0ef41Sopenharmony_ciimport { MockAgent } from 'undici' 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 291cb0ef41Sopenharmony_ci``` 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci### Example - Basic MockAgent instantiation with custom agent 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci```js 341cb0ef41Sopenharmony_ciimport { Agent, MockAgent } from 'undici' 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciconst agent = new Agent() 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent({ agent }) 391cb0ef41Sopenharmony_ci``` 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci## Instance Methods 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci### `MockAgent.get(origin)` 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ciThis method creates and retrieves MockPool or MockClient instances which can then be used to intercept HTTP requests. If the number of connections on the mock agent is set to 1, a MockClient instance is returned. Otherwise a MockPool instance is returned. 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciFor subsequent `MockAgent.get` calls on the same origin, the same mock instance will be returned. 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciArguments: 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci* **origin** `string | RegExp | (value) => boolean` - a matcher for the pool origin to be retrieved from the MockAgent. 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci| Matcher type | Condition to pass | 541cb0ef41Sopenharmony_ci|:------------:| -------------------------- | 551cb0ef41Sopenharmony_ci| `string` | Exact match against string | 561cb0ef41Sopenharmony_ci| `RegExp` | Regex must pass | 571cb0ef41Sopenharmony_ci| `Function` | Function must return true | 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciReturns: `MockClient | MockPool`. 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci| `MockAgentOptions` | Mock instance returned | 621cb0ef41Sopenharmony_ci| -------------------- | ---------------------- | 631cb0ef41Sopenharmony_ci| `connections === 1` | `MockClient` | 641cb0ef41Sopenharmony_ci| `connections` > `1` | `MockPool` | 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci#### Example - Basic Mocked Request 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci```js 691cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 721cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000') 751cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciconst { statusCode, body } = await request('http://localhost:3000/foo') 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_cifor await (const data of body) { 821cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 831cb0ef41Sopenharmony_ci} 841cb0ef41Sopenharmony_ci``` 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci#### Example - Basic Mocked Request with local mock agent dispatcher 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci```js 891cb0ef41Sopenharmony_ciimport { MockAgent, request } from 'undici' 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000') 941cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ciconst { 971cb0ef41Sopenharmony_ci statusCode, 981cb0ef41Sopenharmony_ci body 991cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo', { dispatcher: mockAgent }) 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_cifor await (const data of body) { 1041cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 1051cb0ef41Sopenharmony_ci} 1061cb0ef41Sopenharmony_ci``` 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci#### Example - Basic Mocked Request with local mock pool dispatcher 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci```js 1111cb0ef41Sopenharmony_ciimport { MockAgent, request } from 'undici' 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000') 1161cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ciconst { 1191cb0ef41Sopenharmony_ci statusCode, 1201cb0ef41Sopenharmony_ci body 1211cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo', { dispatcher: mockPool }) 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_cifor await (const data of body) { 1261cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 1271cb0ef41Sopenharmony_ci} 1281cb0ef41Sopenharmony_ci``` 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci#### Example - Basic Mocked Request with local mock client dispatcher 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci```js 1331cb0ef41Sopenharmony_ciimport { MockAgent, request } from 'undici' 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent({ connections: 1 }) 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ciconst mockClient = mockAgent.get('http://localhost:3000') 1381cb0ef41Sopenharmony_cimockClient.intercept({ path: '/foo' }).reply(200, 'foo') 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ciconst { 1411cb0ef41Sopenharmony_ci statusCode, 1421cb0ef41Sopenharmony_ci body 1431cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo', { dispatcher: mockClient }) 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_cifor await (const data of body) { 1481cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 1491cb0ef41Sopenharmony_ci} 1501cb0ef41Sopenharmony_ci``` 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci#### Example - Basic Mocked requests with multiple intercepts 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci```js 1551cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 1581cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000') 1611cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 1621cb0ef41Sopenharmony_cimockPool.intercept({ path: '/hello'}).reply(200, 'hello') 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ciconst result1 = await request('http://localhost:3000/foo') 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciconsole.log('response received', result1.statusCode) // response received 200 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_cifor await (const data of result1.body) { 1691cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 1701cb0ef41Sopenharmony_ci} 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ciconst result2 = await request('http://localhost:3000/hello') 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ciconsole.log('response received', result2.statusCode) // response received 200 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_cifor await (const data of result2.body) { 1771cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data hello 1781cb0ef41Sopenharmony_ci} 1791cb0ef41Sopenharmony_ci``` 1801cb0ef41Sopenharmony_ci#### Example - Mock different requests within the same file 1811cb0ef41Sopenharmony_ci```js 1821cb0ef41Sopenharmony_ciconst { MockAgent, setGlobalDispatcher } = require('undici'); 1831cb0ef41Sopenharmony_ciconst agent = new MockAgent(); 1841cb0ef41Sopenharmony_ciagent.disableNetConnect(); 1851cb0ef41Sopenharmony_cisetGlobalDispatcher(agent); 1861cb0ef41Sopenharmony_cidescribe('Test', () => { 1871cb0ef41Sopenharmony_ci it('200', async () => { 1881cb0ef41Sopenharmony_ci const mockAgent = agent.get('http://test.com'); 1891cb0ef41Sopenharmony_ci // your test 1901cb0ef41Sopenharmony_ci }); 1911cb0ef41Sopenharmony_ci it('200', async () => { 1921cb0ef41Sopenharmony_ci const mockAgent = agent.get('http://testing.com'); 1931cb0ef41Sopenharmony_ci // your test 1941cb0ef41Sopenharmony_ci }); 1951cb0ef41Sopenharmony_ci}); 1961cb0ef41Sopenharmony_ci``` 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ci#### Example - Mocked request with query body, headers and trailers 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ci```js 2011cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 2041cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000') 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_cimockPool.intercept({ 2091cb0ef41Sopenharmony_ci path: '/foo?hello=there&see=ya', 2101cb0ef41Sopenharmony_ci method: 'POST', 2111cb0ef41Sopenharmony_ci body: 'form1=data1&form2=data2' 2121cb0ef41Sopenharmony_ci}).reply(200, { foo: 'bar' }, { 2131cb0ef41Sopenharmony_ci headers: { 'content-type': 'application/json' }, 2141cb0ef41Sopenharmony_ci trailers: { 'Content-MD5': 'test' } 2151cb0ef41Sopenharmony_ci}) 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ciconst { 2181cb0ef41Sopenharmony_ci statusCode, 2191cb0ef41Sopenharmony_ci headers, 2201cb0ef41Sopenharmony_ci trailers, 2211cb0ef41Sopenharmony_ci body 2221cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo?hello=there&see=ya', { 2231cb0ef41Sopenharmony_ci method: 'POST', 2241cb0ef41Sopenharmony_ci body: 'form1=data1&form2=data2' 2251cb0ef41Sopenharmony_ci}) 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 2281cb0ef41Sopenharmony_ciconsole.log('headers', headers) // { 'content-type': 'application/json' } 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_cifor await (const data of body) { 2311cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // '{"foo":"bar"}' 2321cb0ef41Sopenharmony_ci} 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ciconsole.log('trailers', trailers) // { 'content-md5': 'test' } 2351cb0ef41Sopenharmony_ci``` 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci#### Example - Mocked request with origin regex 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci```js 2401cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 2431cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get(new RegExp('http://localhost:3000')) 2461cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ciconst { 2491cb0ef41Sopenharmony_ci statusCode, 2501cb0ef41Sopenharmony_ci body 2511cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo') 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_cifor await (const data of body) { 2561cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 2571cb0ef41Sopenharmony_ci} 2581cb0ef41Sopenharmony_ci``` 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci#### Example - Mocked request with origin function 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci```js 2631cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 2661cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get((origin) => origin === 'http://localhost:3000') 2691cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ciconst { 2721cb0ef41Sopenharmony_ci statusCode, 2731cb0ef41Sopenharmony_ci body 2741cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo') 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_cifor await (const data of body) { 2791cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 2801cb0ef41Sopenharmony_ci} 2811cb0ef41Sopenharmony_ci``` 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_ci### `MockAgent.close()` 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ciCloses the mock agent and waits for registered mock pools and clients to also close before resolving. 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ciReturns: `Promise<void>` 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci#### Example - clean up after tests are complete 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci```js 2921cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher } from 'undici' 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 2951cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_ciawait mockAgent.close() 2981cb0ef41Sopenharmony_ci``` 2991cb0ef41Sopenharmony_ci 3001cb0ef41Sopenharmony_ci### `MockAgent.dispatch(options, handlers)` 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ciImplements [`Agent.dispatch(options, handlers)`](Agent.md#parameter-agentdispatchoptions). 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ci### `MockAgent.request(options[, callback])` 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ciSee [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback). 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci#### Example - MockAgent request 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci```js 3111cb0ef41Sopenharmony_ciimport { MockAgent } from 'undici' 3121cb0ef41Sopenharmony_ci 3131cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000') 3161cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo') 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ciconst { 3191cb0ef41Sopenharmony_ci statusCode, 3201cb0ef41Sopenharmony_ci body 3211cb0ef41Sopenharmony_ci} = await mockAgent.request({ 3221cb0ef41Sopenharmony_ci origin: 'http://localhost:3000', 3231cb0ef41Sopenharmony_ci path: '/foo', 3241cb0ef41Sopenharmony_ci method: 'GET' 3251cb0ef41Sopenharmony_ci}) 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 3281cb0ef41Sopenharmony_ci 3291cb0ef41Sopenharmony_cifor await (const data of body) { 3301cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 3311cb0ef41Sopenharmony_ci} 3321cb0ef41Sopenharmony_ci``` 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ci### `MockAgent.deactivate()` 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ciThis method disables mocking in MockAgent. 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ciReturns: `void` 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci#### Example - Deactivate Mocking 3411cb0ef41Sopenharmony_ci 3421cb0ef41Sopenharmony_ci```js 3431cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher } from 'undici' 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 3461cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_cimockAgent.deactivate() 3491cb0ef41Sopenharmony_ci``` 3501cb0ef41Sopenharmony_ci 3511cb0ef41Sopenharmony_ci### `MockAgent.activate()` 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ciThis method enables mocking in a MockAgent instance. When instantiated, a MockAgent is automatically activated. Therefore, this method is only effective after `MockAgent.deactivate` has been called. 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ciReturns: `void` 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ci#### Example - Activate Mocking 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ci```js 3601cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher } from 'undici' 3611cb0ef41Sopenharmony_ci 3621cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 3631cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_cimockAgent.deactivate() 3661cb0ef41Sopenharmony_ci// No mocking will occur 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ci// Later 3691cb0ef41Sopenharmony_cimockAgent.activate() 3701cb0ef41Sopenharmony_ci``` 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ci### `MockAgent.enableNetConnect([host])` 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ciWhen requests are not matched in a MockAgent intercept, a real HTTP request is attempted. We can control this further through the use of `enableNetConnect`. This is achieved by defining host matchers so only matching requests will be attempted. 3751cb0ef41Sopenharmony_ci 3761cb0ef41Sopenharmony_ciWhen using a string, it should only include the **hostname and optionally, the port**. In addition, calling this method multiple times with a string will allow all HTTP requests that match these values. 3771cb0ef41Sopenharmony_ci 3781cb0ef41Sopenharmony_ciArguments: 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci* **host** `string | RegExp | (value) => boolean` - (optional) 3811cb0ef41Sopenharmony_ci 3821cb0ef41Sopenharmony_ciReturns: `void` 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci#### Example - Allow all non-matching urls to be dispatched in a real HTTP request 3851cb0ef41Sopenharmony_ci 3861cb0ef41Sopenharmony_ci```js 3871cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 3901cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 3911cb0ef41Sopenharmony_ci 3921cb0ef41Sopenharmony_cimockAgent.enableNetConnect() 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ciawait request('http://example.com') 3951cb0ef41Sopenharmony_ci// A real request is made 3961cb0ef41Sopenharmony_ci``` 3971cb0ef41Sopenharmony_ci 3981cb0ef41Sopenharmony_ci#### Example - Allow requests matching a host string to make real requests 3991cb0ef41Sopenharmony_ci 4001cb0ef41Sopenharmony_ci```js 4011cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 4021cb0ef41Sopenharmony_ci 4031cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 4041cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_cimockAgent.enableNetConnect('example-1.com') 4071cb0ef41Sopenharmony_cimockAgent.enableNetConnect('example-2.com:8080') 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ciawait request('http://example-1.com') 4101cb0ef41Sopenharmony_ci// A real request is made 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ciawait request('http://example-2.com:8080') 4131cb0ef41Sopenharmony_ci// A real request is made 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ciawait request('http://example-3.com') 4161cb0ef41Sopenharmony_ci// Will throw 4171cb0ef41Sopenharmony_ci``` 4181cb0ef41Sopenharmony_ci 4191cb0ef41Sopenharmony_ci#### Example - Allow requests matching a host regex to make real requests 4201cb0ef41Sopenharmony_ci 4211cb0ef41Sopenharmony_ci```js 4221cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 4251cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 4261cb0ef41Sopenharmony_ci 4271cb0ef41Sopenharmony_cimockAgent.enableNetConnect(new RegExp('example.com')) 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ciawait request('http://example.com') 4301cb0ef41Sopenharmony_ci// A real request is made 4311cb0ef41Sopenharmony_ci``` 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci#### Example - Allow requests matching a host function to make real requests 4341cb0ef41Sopenharmony_ci 4351cb0ef41Sopenharmony_ci```js 4361cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici' 4371cb0ef41Sopenharmony_ci 4381cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 4391cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent) 4401cb0ef41Sopenharmony_ci 4411cb0ef41Sopenharmony_cimockAgent.enableNetConnect((value) => value === 'example.com') 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ciawait request('http://example.com') 4441cb0ef41Sopenharmony_ci// A real request is made 4451cb0ef41Sopenharmony_ci``` 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_ci### `MockAgent.disableNetConnect()` 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ciThis method causes all requests to throw when requests are not matched in a MockAgent intercept. 4501cb0ef41Sopenharmony_ci 4511cb0ef41Sopenharmony_ciReturns: `void` 4521cb0ef41Sopenharmony_ci 4531cb0ef41Sopenharmony_ci#### Example - Disable all non-matching requests by throwing an error for each 4541cb0ef41Sopenharmony_ci 4551cb0ef41Sopenharmony_ci```js 4561cb0ef41Sopenharmony_ciimport { MockAgent, request } from 'undici' 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent() 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_cimockAgent.disableNetConnect() 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ciawait request('http://example.com') 4631cb0ef41Sopenharmony_ci// Will throw 4641cb0ef41Sopenharmony_ci``` 4651cb0ef41Sopenharmony_ci 4661cb0ef41Sopenharmony_ci### `MockAgent.pendingInterceptors()` 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_ciThis method returns any pending interceptors registered on a mock agent. A pending interceptor meets one of the following criteria: 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ci- Is registered with neither `.times(<number>)` nor `.persist()`, and has not been invoked; 4711cb0ef41Sopenharmony_ci- Is persistent (i.e., registered with `.persist()`) and has not been invoked; 4721cb0ef41Sopenharmony_ci- Is registered with `.times(<number>)` and has not been invoked `<number>` of times. 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ciReturns: `PendingInterceptor[]` (where `PendingInterceptor` is a `MockDispatch` with an additional `origin: string`) 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_ci#### Example - List all pending inteceptors 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci```js 4791cb0ef41Sopenharmony_ciconst agent = new MockAgent() 4801cb0ef41Sopenharmony_ciagent.disableNetConnect() 4811cb0ef41Sopenharmony_ci 4821cb0ef41Sopenharmony_ciagent 4831cb0ef41Sopenharmony_ci .get('https://example.com') 4841cb0ef41Sopenharmony_ci .intercept({ method: 'GET', path: '/' }) 4851cb0ef41Sopenharmony_ci .reply(200) 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ciconst pendingInterceptors = agent.pendingInterceptors() 4881cb0ef41Sopenharmony_ci// Returns [ 4891cb0ef41Sopenharmony_ci// { 4901cb0ef41Sopenharmony_ci// timesInvoked: 0, 4911cb0ef41Sopenharmony_ci// times: 1, 4921cb0ef41Sopenharmony_ci// persist: false, 4931cb0ef41Sopenharmony_ci// consumed: false, 4941cb0ef41Sopenharmony_ci// pending: true, 4951cb0ef41Sopenharmony_ci// path: '/', 4961cb0ef41Sopenharmony_ci// method: 'GET', 4971cb0ef41Sopenharmony_ci// body: undefined, 4981cb0ef41Sopenharmony_ci// headers: undefined, 4991cb0ef41Sopenharmony_ci// data: { 5001cb0ef41Sopenharmony_ci// error: null, 5011cb0ef41Sopenharmony_ci// statusCode: 200, 5021cb0ef41Sopenharmony_ci// data: '', 5031cb0ef41Sopenharmony_ci// headers: {}, 5041cb0ef41Sopenharmony_ci// trailers: {} 5051cb0ef41Sopenharmony_ci// }, 5061cb0ef41Sopenharmony_ci// origin: 'https://example.com' 5071cb0ef41Sopenharmony_ci// } 5081cb0ef41Sopenharmony_ci// ] 5091cb0ef41Sopenharmony_ci``` 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci### `MockAgent.assertNoPendingInterceptors([options])` 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ciThis method throws if the mock agent has any pending interceptors. A pending interceptor meets one of the following criteria: 5141cb0ef41Sopenharmony_ci 5151cb0ef41Sopenharmony_ci- Is registered with neither `.times(<number>)` nor `.persist()`, and has not been invoked; 5161cb0ef41Sopenharmony_ci- Is persistent (i.e., registered with `.persist()`) and has not been invoked; 5171cb0ef41Sopenharmony_ci- Is registered with `.times(<number>)` and has not been invoked `<number>` of times. 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci#### Example - Check that there are no pending interceptors 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ci```js 5221cb0ef41Sopenharmony_ciconst agent = new MockAgent() 5231cb0ef41Sopenharmony_ciagent.disableNetConnect() 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ciagent 5261cb0ef41Sopenharmony_ci .get('https://example.com') 5271cb0ef41Sopenharmony_ci .intercept({ method: 'GET', path: '/' }) 5281cb0ef41Sopenharmony_ci .reply(200) 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ciagent.assertNoPendingInterceptors() 5311cb0ef41Sopenharmony_ci// Throws an UndiciError with the following message: 5321cb0ef41Sopenharmony_ci// 5331cb0ef41Sopenharmony_ci// 1 interceptor is pending: 5341cb0ef41Sopenharmony_ci// 5351cb0ef41Sopenharmony_ci// ┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐ 5361cb0ef41Sopenharmony_ci// │ (index) │ Method │ Origin │ Path │ Status code │ Persistent │ Invocations │ Remaining │ 5371cb0ef41Sopenharmony_ci// ├─────────┼────────┼───────────────────────┼──────┼─────────────┼────────────┼─────────────┼───────────┤ 5381cb0ef41Sopenharmony_ci// │ 0 │ 'GET' │ 'https://example.com' │ '/' │ 200 │ '❌' │ 0 │ 1 │ 5391cb0ef41Sopenharmony_ci// └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘ 5401cb0ef41Sopenharmony_ci``` 541