11cb0ef41Sopenharmony_ci# Class: MockClient 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciExtends: `undici.Client` 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciA mock client class that implements the same api as [MockPool](MockPool.md). 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci## `new MockClient(origin, [options])` 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciArguments: 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci* **origin** `string` - It should only include the **protocol, hostname, and port**. 121cb0ef41Sopenharmony_ci* **options** `MockClientOptions` - It extends the `Client` options. 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciReturns: `MockClient` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci### Parameter: `MockClientOptions` 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciExtends: `ClientOptions` 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci* **agent** `Agent` - the agent to associate this MockClient with. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci### Example - Basic MockClient instantiation 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciWe can use MockAgent to instantiate a MockClient 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_ci// Connections must be set to 1 to return a MockClient instance 301cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent({ connections: 1 }) 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst mockClient = mockAgent.get('http://localhost:3000') 331cb0ef41Sopenharmony_ci``` 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci## Instance Methods 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci### `MockClient.intercept(options)` 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciImplements: [`MockPool.intercept(options)`](MockPool.md#mockpoolinterceptoptions) 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci### `MockClient.close()` 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciImplements: [`MockPool.close()`](MockPool.md#mockpoolclose) 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci### `MockClient.dispatch(options, handlers)` 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciImplements [`Dispatcher.dispatch(options, handlers)`](Dispatcher.md#dispatcherdispatchoptions-handler). 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci### `MockClient.request(options[, callback])` 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciSee [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback). 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci#### Example - MockClient request 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci```js 561cb0ef41Sopenharmony_ciimport { MockAgent } from 'undici' 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciconst mockAgent = new MockAgent({ connections: 1 }) 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciconst mockClient = mockAgent.get('http://localhost:3000') 611cb0ef41Sopenharmony_cimockClient.intercept({ path: '/foo' }).reply(200, 'foo') 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ciconst { 641cb0ef41Sopenharmony_ci statusCode, 651cb0ef41Sopenharmony_ci body 661cb0ef41Sopenharmony_ci} = await mockClient.request({ 671cb0ef41Sopenharmony_ci origin: 'http://localhost:3000', 681cb0ef41Sopenharmony_ci path: '/foo', 691cb0ef41Sopenharmony_ci method: 'GET' 701cb0ef41Sopenharmony_ci}) 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_cifor await (const data of body) { 751cb0ef41Sopenharmony_ci console.log('data', data.toString('utf8')) // data foo 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci``` 78