[d24f17c] | 1 | # Class: MockClient
|
---|
| 2 |
|
---|
| 3 | Extends: `undici.Client`
|
---|
| 4 |
|
---|
| 5 | A mock client class that implements the same api as [MockPool](MockPool.md).
|
---|
| 6 |
|
---|
| 7 | ## `new MockClient(origin, [options])`
|
---|
| 8 |
|
---|
| 9 | Arguments:
|
---|
| 10 |
|
---|
| 11 | * **origin** `string` - It should only include the **protocol, hostname, and port**.
|
---|
| 12 | * **options** `MockClientOptions` - It extends the `Client` options.
|
---|
| 13 |
|
---|
| 14 | Returns: `MockClient`
|
---|
| 15 |
|
---|
| 16 | ### Parameter: `MockClientOptions`
|
---|
| 17 |
|
---|
| 18 | Extends: `ClientOptions`
|
---|
| 19 |
|
---|
| 20 | * **agent** `Agent` - the agent to associate this MockClient with.
|
---|
| 21 |
|
---|
| 22 | ### Example - Basic MockClient instantiation
|
---|
| 23 |
|
---|
| 24 | We 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.
|
---|
| 25 |
|
---|
| 26 | ```js
|
---|
| 27 | import { MockAgent } from 'undici'
|
---|
| 28 |
|
---|
| 29 | // Connections must be set to 1 to return a MockClient instance
|
---|
| 30 | const mockAgent = new MockAgent({ connections: 1 })
|
---|
| 31 |
|
---|
| 32 | const mockClient = mockAgent.get('http://localhost:3000')
|
---|
| 33 | ```
|
---|
| 34 |
|
---|
| 35 | ## Instance Methods
|
---|
| 36 |
|
---|
| 37 | ### `MockClient.intercept(options)`
|
---|
| 38 |
|
---|
| 39 | Implements: [`MockPool.intercept(options)`](MockPool.md#mockpoolinterceptoptions)
|
---|
| 40 |
|
---|
| 41 | ### `MockClient.close()`
|
---|
| 42 |
|
---|
| 43 | Implements: [`MockPool.close()`](MockPool.md#mockpoolclose)
|
---|
| 44 |
|
---|
| 45 | ### `MockClient.dispatch(options, handlers)`
|
---|
| 46 |
|
---|
| 47 | Implements [`Dispatcher.dispatch(options, handlers)`](Dispatcher.md#dispatcherdispatchoptions-handler).
|
---|
| 48 |
|
---|
| 49 | ### `MockClient.request(options[, callback])`
|
---|
| 50 |
|
---|
| 51 | See [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
|
---|
| 52 |
|
---|
| 53 | #### Example - MockClient request
|
---|
| 54 |
|
---|
| 55 | ```js
|
---|
| 56 | import { MockAgent } from 'undici'
|
---|
| 57 |
|
---|
| 58 | const mockAgent = new MockAgent({ connections: 1 })
|
---|
| 59 |
|
---|
| 60 | const mockClient = mockAgent.get('http://localhost:3000')
|
---|
| 61 | mockClient.intercept({ path: '/foo' }).reply(200, 'foo')
|
---|
| 62 |
|
---|
| 63 | const {
|
---|
| 64 | statusCode,
|
---|
| 65 | body
|
---|
| 66 | } = await mockClient.request({
|
---|
| 67 | origin: 'http://localhost:3000',
|
---|
| 68 | path: '/foo',
|
---|
| 69 | method: 'GET'
|
---|
| 70 | })
|
---|
| 71 |
|
---|
| 72 | console.log('response received', statusCode) // response received 200
|
---|
| 73 |
|
---|
| 74 | for await (const data of body) {
|
---|
| 75 | console.log('data', data.toString('utf8')) // data foo
|
---|
| 76 | }
|
---|
| 77 | ```
|
---|