A MockClient is a Client that intercepts requests matching registered
routes and replies with mocked responses instead of contacting the network.
It is created by a MockAgent when the agent is configured with a single
connection, and it exposes the same interception API as MockPool.
A MockClient is not constructed directly in most cases; instead it is obtained
through [mockAgent.get(origin)][] once the agent has connections set to 1.
import { MockAgent } from 'undici'
// `connections: 1` makes the agent hand out `MockClient` instances.
const mockAgent = new MockAgent({ connections: 1 })
const mockClient = mockAgent.get('http://localhost:3000')class MockClient extends ClientExtends Client and implements the Interceptable interface, allowing
requests made through it to be matched against registered mocks.
new MockClient(origin, options?): MockClient<string><MockClientOptions>Client
options.<MockClient>The agent option is required and must implement the Agent interface;
otherwise an InvalidArgumentError is thrown.
Extends: ClientOptions
<MockAgent><boolean>false
.mockClient.intercept(options): MockInterceptor<MockInterceptor.Options><string>
|
<RegExp>
|
<Function>string
and returns a
boolean
.<string>
|
<RegExp>
|
<Function>string
and returns a
boolean
.
Default:
'GET'
.<string>
|
<RegExp>
|
<Function>string
and returns a
boolean
.<Object>
|
<Function>string
,
RegExp
, or matching function, or a single function
that receives all headers and returns a
boolean
.<Object><boolean>path
is
ignored when matching. Inherited from the mock client's
ignoreTrailingSlash
option when not set.<MockInterceptor>Registers a route on the mock client and returns a MockInterceptor for any
matching request that uses the same origin as this mock client. The interceptor
is then used to define the reply, for example with reply() or
replyWithError().
import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })
const mockClient = mockAgent.get('http://localhost:3000')
mockClient.intercept({ path: '/foo', method: 'GET' }).reply(200, 'foo')mockClient.cleanMocks(): voidRemoves all registered interceptors from the mock client. Pending mocks defined before this call no longer match incoming requests.
import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })
const mockClient = mockAgent.get('http://localhost:3000')
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')
mockClient.cleanMocks()mockClient.close(): Promise<Promise>undefined
once the mock client is closed.Closes the mock client, gracefully waiting for any enqueued requests to
complete, and removes it from the associated MockAgent.
import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })
const mockClient = mockAgent.get('http://localhost:3000')
await mockClient.close()mockClient.dispatch(options, handlers): boolean<DispatchOptions><DispatchHandler><boolean>false
if the dispatcher is busy and the caller should
wait before dispatching further requests, otherwise
true
.Dispatches a request, matching it against the registered mocks. This override of
dispatcher.dispatch(options, handlers) is what drives the mocking
behaviour for every higher-level method such as request.
mockClient.request(options, callback?): Promise<DispatchOptions><Function>Promise
is requested.<Promise>callback
is
provided.Performs a request and resolves it against the registered mocks. Inherited from
Client; see [dispatcher.request(options[, callback])][] for the full
parameter and return value documentation.
import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })
const mockClient = mockAgent.get('http://localhost:3000')
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')
const { statusCode, body } = await mockClient.request({
origin: 'http://localhost:3000',
path: '/foo',
method: 'GET'
})
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}[dispatcher.request(options[, callback])]: Dispatcher.md#dispatcherrequestoptions-callback
[mockAgent.get(origin)]: MockAgent.md#mockagentgetorigin