11cb0ef41Sopenharmony_ci# Class: MockPool
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciExtends: `undici.Pool`
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciA mock Pool class that implements the Pool API and is used by MockAgent to intercept real requests and return mocked responses.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci## `new MockPool(origin, [options])`
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciArguments:
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci* **origin** `string` - It should only include the **protocol, hostname, and port**.
121cb0ef41Sopenharmony_ci* **options** `MockPoolOptions` - It extends the `Pool` options.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciReturns: `MockPool`
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci### Parameter: `MockPoolOptions`
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciExtends: `PoolOptions`
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci* **agent** `Agent` - the agent to associate this MockPool with.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci### Example - Basic MockPool instantiation
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciWe can use MockAgent to instantiate a MockPool ready to be used to intercept specified requests. It will not do anything until registered as the agent to use and any mock request are registered.
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci```js
271cb0ef41Sopenharmony_ciimport { MockAgent } from 'undici'
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
321cb0ef41Sopenharmony_ci```
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci## Instance Methods
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci### `MockPool.intercept(options)`
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciThis method defines the interception rules for matching against requests for a MockPool or MockPool. We can intercept multiple times on a single instance, but each intercept is only used once. For example if you expect to make 2 requests inside a test, you need to call `intercept()` twice. Assuming you use `disableNetConnect()` you will get `MockNotMatchedError` on the second request when you only call `intercept()` once.
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciWhen defining interception rules, all the rules must pass for a request to be intercepted. If a request is not intercepted, a real request will be attempted.
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci| Matcher type | Condition to pass          |
431cb0ef41Sopenharmony_ci|:------------:| -------------------------- |
441cb0ef41Sopenharmony_ci| `string`     | Exact match against string |
451cb0ef41Sopenharmony_ci| `RegExp`     | Regex must pass            |
461cb0ef41Sopenharmony_ci| `Function`   | Function must return true  |
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciArguments:
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci* **options** `MockPoolInterceptOptions` - Interception options.
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciReturns: `MockInterceptor` corresponding to the input options.
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci### Parameter: `MockPoolInterceptOptions`
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci* **path** `string | RegExp | (path: string) => boolean` - a matcher for the HTTP request path. When a `RegExp` or callback is used, it will match against the request path including all query parameters in alphabetical order. When a `string` is provided, the query parameters can be conveniently specified through the `MockPoolInterceptOptions.query` setting.
571cb0ef41Sopenharmony_ci* **method** `string | RegExp | (method: string) => boolean` - (optional) - a matcher for the HTTP request method. Defaults to `GET`.
581cb0ef41Sopenharmony_ci* **body** `string | RegExp | (body: string) => boolean` - (optional) - a matcher for the HTTP request body.
591cb0ef41Sopenharmony_ci* **headers** `Record<string, string | RegExp | (body: string) => boolean`> - (optional) - a matcher for the HTTP request headers. To be intercepted, a request must match all defined headers. Extra headers not defined here may (or may not) be included in the request and do not affect the interception in any way.
601cb0ef41Sopenharmony_ci* **query** `Record<string, any> | null` - (optional) - a matcher for the HTTP request query string params. Only applies when a `string` was provided for `MockPoolInterceptOptions.path`.
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci### Return: `MockInterceptor`
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciWe can define the behaviour of an intercepted request with the following options.
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci* **reply** `(statusCode: number, replyData: string | Buffer | object | MockInterceptor.MockResponseDataHandler, responseOptions?: MockResponseOptions) => MockScope` - define a reply for a matching request. You can define the replyData as a callback to read incoming request data. Default for `responseOptions` is `{}`.
671cb0ef41Sopenharmony_ci* **reply** `(callback: MockInterceptor.MockReplyOptionsCallback) => MockScope` - define a reply for a matching request, allowing dynamic mocking of all reply options rather than just the data.
681cb0ef41Sopenharmony_ci* **replyWithError** `(error: Error) => MockScope` - define an error for a matching request to throw.
691cb0ef41Sopenharmony_ci* **defaultReplyHeaders** `(headers: Record<string, string>) => MockInterceptor` - define default headers to be included in subsequent replies. These are in addition to headers on a specific reply.
701cb0ef41Sopenharmony_ci* **defaultReplyTrailers** `(trailers: Record<string, string>) => MockInterceptor` - define default trailers to be included in subsequent replies. These are in addition to trailers on a specific reply.
711cb0ef41Sopenharmony_ci* **replyContentLength** `() => MockInterceptor` - define automatically calculated `content-length` headers to be included in subsequent replies.
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciThe reply data of an intercepted request may either be a string, buffer, or JavaScript object. Objects are converted to JSON while strings and buffers are sent as-is.
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciBy default, `reply` and `replyWithError` define the behaviour for the first matching request only. Subsequent requests will not be affected (this can be changed using the returned `MockScope`).
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci### Parameter: `MockResponseOptions`
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci* **headers** `Record<string, string>` - headers to be included on the mocked reply.
801cb0ef41Sopenharmony_ci* **trailers** `Record<string, string>` - trailers to be included on the mocked reply.
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci### Return: `MockScope`
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciA `MockScope` is associated with a single `MockInterceptor`. With this, we can configure the default behaviour of a intercepted reply.
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci* **delay** `(waitInMs: number) => MockScope` - delay the associated reply by a set amount in ms.
871cb0ef41Sopenharmony_ci* **persist** `() => MockScope` - any matching request will always reply with the defined response indefinitely.
881cb0ef41Sopenharmony_ci* **times** `(repeatTimes: number) => MockScope` - any matching request will reply with the defined response a fixed amount of times. This is overridden by **persist**.
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci#### Example - Basic Mocked Request
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci```js
931cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
961cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci// MockPool
991cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
1001cb0ef41Sopenharmony_cimockPool.intercept({ path: '/foo' }).reply(200, 'foo')
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciconst {
1031cb0ef41Sopenharmony_ci  statusCode,
1041cb0ef41Sopenharmony_ci  body
1051cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo')
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_cifor await (const data of body) {
1101cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data foo
1111cb0ef41Sopenharmony_ci}
1121cb0ef41Sopenharmony_ci```
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci#### Example - Mocked request using reply data callbacks
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci```js
1171cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
1201cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_cimockPool.intercept({
1251cb0ef41Sopenharmony_ci  path: '/echo',
1261cb0ef41Sopenharmony_ci  method: 'GET',
1271cb0ef41Sopenharmony_ci  headers: {
1281cb0ef41Sopenharmony_ci    'User-Agent': 'undici',
1291cb0ef41Sopenharmony_ci    Host: 'example.com'
1301cb0ef41Sopenharmony_ci  }
1311cb0ef41Sopenharmony_ci}).reply(200, ({ headers }) => ({ message: headers.get('message') }))
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ciconst { statusCode, body, headers } = await request('http://localhost:3000', {
1341cb0ef41Sopenharmony_ci  headers: {
1351cb0ef41Sopenharmony_ci    message: 'hello world!'
1361cb0ef41Sopenharmony_ci  }
1371cb0ef41Sopenharmony_ci})
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
1401cb0ef41Sopenharmony_ciconsole.log('headers', headers) // { 'content-type': 'application/json' }
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_cifor await (const data of body) {
1431cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // { "message":"hello world!" }
1441cb0ef41Sopenharmony_ci}
1451cb0ef41Sopenharmony_ci```
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci#### Example - Mocked request using reply options callback
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci```js
1501cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
1531cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_cimockPool.intercept({
1581cb0ef41Sopenharmony_ci  path: '/echo',
1591cb0ef41Sopenharmony_ci  method: 'GET',
1601cb0ef41Sopenharmony_ci  headers: {
1611cb0ef41Sopenharmony_ci    'User-Agent': 'undici',
1621cb0ef41Sopenharmony_ci    Host: 'example.com'
1631cb0ef41Sopenharmony_ci  }
1641cb0ef41Sopenharmony_ci}).reply(({ headers }) => ({ statusCode: 200, data: { message: headers.get('message') }})))
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ciconst { statusCode, body, headers } = await request('http://localhost:3000', {
1671cb0ef41Sopenharmony_ci  headers: {
1681cb0ef41Sopenharmony_ci    message: 'hello world!'
1691cb0ef41Sopenharmony_ci  }
1701cb0ef41Sopenharmony_ci})
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
1731cb0ef41Sopenharmony_ciconsole.log('headers', headers) // { 'content-type': 'application/json' }
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_cifor await (const data of body) {
1761cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // { "message":"hello world!" }
1771cb0ef41Sopenharmony_ci}
1781cb0ef41Sopenharmony_ci```
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci#### Example - Basic Mocked requests with multiple intercepts
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci```js
1831cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
1861cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_cimockPool.intercept({
1911cb0ef41Sopenharmony_ci  path: '/foo',
1921cb0ef41Sopenharmony_ci  method: 'GET'
1931cb0ef41Sopenharmony_ci}).reply(200, 'foo')
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_cimockPool.intercept({
1961cb0ef41Sopenharmony_ci  path: '/hello',
1971cb0ef41Sopenharmony_ci  method: 'GET',
1981cb0ef41Sopenharmony_ci}).reply(200, 'hello')
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ciconst result1 = await request('http://localhost:3000/foo')
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ciconsole.log('response received', result1.statusCode) // response received 200
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_cifor await (const data of result1.body) {
2051cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data foo
2061cb0ef41Sopenharmony_ci}
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ciconst result2 = await request('http://localhost:3000/hello')
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ciconsole.log('response received', result2.statusCode) // response received 200
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_cifor await (const data of result2.body) {
2131cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data hello
2141cb0ef41Sopenharmony_ci}
2151cb0ef41Sopenharmony_ci```
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci#### Example - Mocked request with query body, request headers and response headers and trailers
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci```js
2201cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
2231cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_cimockPool.intercept({
2281cb0ef41Sopenharmony_ci  path: '/foo?hello=there&see=ya',
2291cb0ef41Sopenharmony_ci  method: 'POST',
2301cb0ef41Sopenharmony_ci  body: 'form1=data1&form2=data2',
2311cb0ef41Sopenharmony_ci  headers: {
2321cb0ef41Sopenharmony_ci    'User-Agent': 'undici',
2331cb0ef41Sopenharmony_ci    Host: 'example.com'
2341cb0ef41Sopenharmony_ci  }
2351cb0ef41Sopenharmony_ci}).reply(200, { foo: 'bar' }, {
2361cb0ef41Sopenharmony_ci  headers: { 'content-type': 'application/json' },
2371cb0ef41Sopenharmony_ci  trailers: { 'Content-MD5': 'test' }
2381cb0ef41Sopenharmony_ci})
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ciconst {
2411cb0ef41Sopenharmony_ci  statusCode,
2421cb0ef41Sopenharmony_ci  headers,
2431cb0ef41Sopenharmony_ci  trailers,
2441cb0ef41Sopenharmony_ci  body
2451cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo?hello=there&see=ya', {
2461cb0ef41Sopenharmony_ci    method: 'POST',
2471cb0ef41Sopenharmony_ci    body: 'form1=data1&form2=data2',
2481cb0ef41Sopenharmony_ci    headers: {
2491cb0ef41Sopenharmony_ci      foo: 'bar',
2501cb0ef41Sopenharmony_ci      'User-Agent': 'undici',
2511cb0ef41Sopenharmony_ci      Host: 'example.com'
2521cb0ef41Sopenharmony_ci    }
2531cb0ef41Sopenharmony_ci  })
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
2561cb0ef41Sopenharmony_ciconsole.log('headers', headers) // { 'content-type': 'application/json' }
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_cifor await (const data of body) {
2591cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // '{"foo":"bar"}'
2601cb0ef41Sopenharmony_ci}
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_ciconsole.log('trailers', trailers) // { 'content-md5': 'test' }
2631cb0ef41Sopenharmony_ci```
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci#### Example - Mocked request using different matchers
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci```js
2681cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
2711cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_cimockPool.intercept({
2761cb0ef41Sopenharmony_ci  path: '/foo',
2771cb0ef41Sopenharmony_ci  method: /^GET$/,
2781cb0ef41Sopenharmony_ci  body: (value) => value === 'form=data',
2791cb0ef41Sopenharmony_ci  headers: {
2801cb0ef41Sopenharmony_ci    'User-Agent': 'undici',
2811cb0ef41Sopenharmony_ci    Host: /^example.com$/
2821cb0ef41Sopenharmony_ci  }
2831cb0ef41Sopenharmony_ci}).reply(200, 'foo')
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ciconst {
2861cb0ef41Sopenharmony_ci  statusCode,
2871cb0ef41Sopenharmony_ci  body
2881cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo', {
2891cb0ef41Sopenharmony_ci  method: 'GET',
2901cb0ef41Sopenharmony_ci  body: 'form=data',
2911cb0ef41Sopenharmony_ci  headers: {
2921cb0ef41Sopenharmony_ci    foo: 'bar',
2931cb0ef41Sopenharmony_ci    'User-Agent': 'undici',
2941cb0ef41Sopenharmony_ci    Host: 'example.com'
2951cb0ef41Sopenharmony_ci  }
2961cb0ef41Sopenharmony_ci})
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_cifor await (const data of body) {
3011cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data foo
3021cb0ef41Sopenharmony_ci}
3031cb0ef41Sopenharmony_ci```
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ci#### Example - Mocked request with reply with a defined error
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_ci```js
3081cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
3111cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_cimockPool.intercept({
3161cb0ef41Sopenharmony_ci  path: '/foo',
3171cb0ef41Sopenharmony_ci  method: 'GET'
3181cb0ef41Sopenharmony_ci}).replyWithError(new Error('kaboom'))
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_citry {
3211cb0ef41Sopenharmony_ci  await request('http://localhost:3000/foo', {
3221cb0ef41Sopenharmony_ci    method: 'GET'
3231cb0ef41Sopenharmony_ci  })
3241cb0ef41Sopenharmony_ci} catch (error) {
3251cb0ef41Sopenharmony_ci  console.error(error) // Error: kaboom
3261cb0ef41Sopenharmony_ci}
3271cb0ef41Sopenharmony_ci```
3281cb0ef41Sopenharmony_ci
3291cb0ef41Sopenharmony_ci#### Example - Mocked request with defaultReplyHeaders
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci```js
3321cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
3351cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_cimockPool.intercept({
3401cb0ef41Sopenharmony_ci  path: '/foo',
3411cb0ef41Sopenharmony_ci  method: 'GET'
3421cb0ef41Sopenharmony_ci}).defaultReplyHeaders({ foo: 'bar' })
3431cb0ef41Sopenharmony_ci  .reply(200, 'foo')
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ciconst { headers } = await request('http://localhost:3000/foo')
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ciconsole.log('headers', headers) // headers { foo: 'bar' }
3481cb0ef41Sopenharmony_ci```
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci#### Example - Mocked request with defaultReplyTrailers
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ci```js
3531cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
3541cb0ef41Sopenharmony_ci
3551cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
3561cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_cimockPool.intercept({
3611cb0ef41Sopenharmony_ci  path: '/foo',
3621cb0ef41Sopenharmony_ci  method: 'GET'
3631cb0ef41Sopenharmony_ci}).defaultReplyTrailers({ foo: 'bar' })
3641cb0ef41Sopenharmony_ci  .reply(200, 'foo')
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ciconst { trailers } = await request('http://localhost:3000/foo')
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ciconsole.log('trailers', trailers) // trailers { foo: 'bar' }
3691cb0ef41Sopenharmony_ci```
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci#### Example - Mocked request with automatic content-length calculation
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci```js
3741cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
3771cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
3781cb0ef41Sopenharmony_ci
3791cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
3801cb0ef41Sopenharmony_ci
3811cb0ef41Sopenharmony_cimockPool.intercept({
3821cb0ef41Sopenharmony_ci  path: '/foo',
3831cb0ef41Sopenharmony_ci  method: 'GET'
3841cb0ef41Sopenharmony_ci}).replyContentLength().reply(200, 'foo')
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ciconst { headers } = await request('http://localhost:3000/foo')
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ciconsole.log('headers', headers) // headers { 'content-length': '3' }
3891cb0ef41Sopenharmony_ci```
3901cb0ef41Sopenharmony_ci
3911cb0ef41Sopenharmony_ci#### Example - Mocked request with automatic content-length calculation on an object
3921cb0ef41Sopenharmony_ci
3931cb0ef41Sopenharmony_ci```js
3941cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
3951cb0ef41Sopenharmony_ci
3961cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
3971cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_cimockPool.intercept({
4021cb0ef41Sopenharmony_ci  path: '/foo',
4031cb0ef41Sopenharmony_ci  method: 'GET'
4041cb0ef41Sopenharmony_ci}).replyContentLength().reply(200, { foo: 'bar' })
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ciconst { headers } = await request('http://localhost:3000/foo')
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ciconsole.log('headers', headers) // headers { 'content-length': '13' }
4091cb0ef41Sopenharmony_ci```
4101cb0ef41Sopenharmony_ci
4111cb0ef41Sopenharmony_ci#### Example - Mocked request with persist enabled
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ci```js
4141cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
4171cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
4201cb0ef41Sopenharmony_ci
4211cb0ef41Sopenharmony_cimockPool.intercept({
4221cb0ef41Sopenharmony_ci  path: '/foo',
4231cb0ef41Sopenharmony_ci  method: 'GET'
4241cb0ef41Sopenharmony_ci}).reply(200, 'foo').persist()
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ciconst result1 = await request('http://localhost:3000/foo')
4271cb0ef41Sopenharmony_ci// Will match and return mocked data
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ciconst result2 = await request('http://localhost:3000/foo')
4301cb0ef41Sopenharmony_ci// Will match and return mocked data
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ci// Etc
4331cb0ef41Sopenharmony_ci```
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci#### Example - Mocked request with times enabled
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci```js
4381cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
4411cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_cimockPool.intercept({
4461cb0ef41Sopenharmony_ci  path: '/foo',
4471cb0ef41Sopenharmony_ci  method: 'GET'
4481cb0ef41Sopenharmony_ci}).reply(200, 'foo').times(2)
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_ciconst result1 = await request('http://localhost:3000/foo')
4511cb0ef41Sopenharmony_ci// Will match and return mocked data
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ciconst result2 = await request('http://localhost:3000/foo')
4541cb0ef41Sopenharmony_ci// Will match and return mocked data
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_ciconst result3 = await request('http://localhost:3000/foo')
4571cb0ef41Sopenharmony_ci// Will not match and make attempt a real request
4581cb0ef41Sopenharmony_ci```
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_ci#### Example - Mocked request with path callback
4611cb0ef41Sopenharmony_ci
4621cb0ef41Sopenharmony_ci```js
4631cb0ef41Sopenharmony_ciimport { MockAgent, setGlobalDispatcher, request } from 'undici'
4641cb0ef41Sopenharmony_ciimport querystring from 'querystring'
4651cb0ef41Sopenharmony_ci
4661cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
4671cb0ef41Sopenharmony_cisetGlobalDispatcher(mockAgent)
4681cb0ef41Sopenharmony_ci
4691cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_ciconst matchPath = requestPath => {
4721cb0ef41Sopenharmony_ci  const [pathname, search] = requestPath.split('?')
4731cb0ef41Sopenharmony_ci  const requestQuery = querystring.parse(search)
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci  if (!pathname.startsWith('/foo')) {
4761cb0ef41Sopenharmony_ci    return false
4771cb0ef41Sopenharmony_ci  }
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ci  if (!Object.keys(requestQuery).includes('foo') || requestQuery.foo !== 'bar') {
4801cb0ef41Sopenharmony_ci    return false
4811cb0ef41Sopenharmony_ci  }
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci  return true
4841cb0ef41Sopenharmony_ci}
4851cb0ef41Sopenharmony_ci
4861cb0ef41Sopenharmony_cimockPool.intercept({
4871cb0ef41Sopenharmony_ci  path: matchPath,
4881cb0ef41Sopenharmony_ci  method: 'GET'
4891cb0ef41Sopenharmony_ci}).reply(200, 'foo')
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_ciconst result = await request('http://localhost:3000/foo?foo=bar')
4921cb0ef41Sopenharmony_ci// Will match and return mocked data
4931cb0ef41Sopenharmony_ci```
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_ci### `MockPool.close()`
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ciCloses the mock pool and de-registers from associated MockAgent.
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_ciReturns: `Promise<void>`
5001cb0ef41Sopenharmony_ci
5011cb0ef41Sopenharmony_ci#### Example - clean up after tests are complete
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ci```js
5041cb0ef41Sopenharmony_ciimport { MockAgent } from 'undici'
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
5071cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ciawait mockPool.close()
5101cb0ef41Sopenharmony_ci```
5111cb0ef41Sopenharmony_ci
5121cb0ef41Sopenharmony_ci### `MockPool.dispatch(options, handlers)`
5131cb0ef41Sopenharmony_ci
5141cb0ef41Sopenharmony_ciImplements [`Dispatcher.dispatch(options, handlers)`](Dispatcher.md#dispatcherdispatchoptions-handler).
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_ci### `MockPool.request(options[, callback])`
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ciSee [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci#### Example - MockPool request
5211cb0ef41Sopenharmony_ci
5221cb0ef41Sopenharmony_ci```js
5231cb0ef41Sopenharmony_ciimport { MockAgent } from 'undici'
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent()
5261cb0ef41Sopenharmony_ci
5271cb0ef41Sopenharmony_ciconst mockPool = mockAgent.get('http://localhost:3000')
5281cb0ef41Sopenharmony_cimockPool.intercept({
5291cb0ef41Sopenharmony_ci  path: '/foo',
5301cb0ef41Sopenharmony_ci  method: 'GET',
5311cb0ef41Sopenharmony_ci}).reply(200, 'foo')
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_ciconst {
5341cb0ef41Sopenharmony_ci  statusCode,
5351cb0ef41Sopenharmony_ci  body
5361cb0ef41Sopenharmony_ci} = await mockPool.request({
5371cb0ef41Sopenharmony_ci  origin: 'http://localhost:3000',
5381cb0ef41Sopenharmony_ci  path: '/foo',
5391cb0ef41Sopenharmony_ci  method: 'GET'
5401cb0ef41Sopenharmony_ci})
5411cb0ef41Sopenharmony_ci
5421cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_cifor await (const data of body) {
5451cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data foo
5461cb0ef41Sopenharmony_ci}
5471cb0ef41Sopenharmony_ci```
548