{
  "type": "module",
  "source": "doc/api/api-mockclient.md",
  "modules": [
    {
      "textRaw": "MockClient",
      "name": "mockclient",
      "introduced_in": "v4.0.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>A <code>MockClient</code> is a <a href=\"Client.html#class-client\"><code>Client</code></a> that intercepts requests matching registered\nroutes and replies with mocked responses instead of contacting the network.\nIt is created by a <a href=\"MockAgent.html#class-mockagent\"><code>MockAgent</code></a> when the agent is configured with a single\nconnection, and it exposes the same interception API as <a href=\"MockPool.html#class-mockpool\"><code>MockPool</code></a>.</p>\n<p>A <code>MockClient</code> is not constructed directly in most cases; instead it is obtained\nthrough [<code>mockAgent.get(origin)</code>][] once the agent has <code>connections</code> set to <code>1</code>.</p>\n<pre><code class=\"language-mjs\">import { MockAgent } from 'undici'\n\n// `connections: 1` makes the agent hand out `MockClient` instances.\nconst mockAgent = new MockAgent({ connections: 1 })\nconst mockClient = mockAgent.get('http://localhost:3000')\n</code></pre>\n<pre><code class=\"language-cjs\">const { MockAgent } = require('undici')\n\nconst mockAgent = new MockAgent({ connections: 1 })\nconst mockClient = mockAgent.get('http://localhost:3000')\n</code></pre>",
      "classes": [
        {
          "textRaw": "Class: `MockClient`",
          "name": "MockClient",
          "type": "class",
          "meta": {
            "added": [
              "v4.0.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a></li>\n</ul>\n<p>Extends <a href=\"Client.html#class-client\"><code>Client</code></a> and implements the <code>Interceptable</code> interface, allowing\nrequests made through it to be matched against registered mocks.</p>",
          "signatures": [
            {
              "textRaw": "`new MockClient(origin[, options])`",
              "name": "MockClient",
              "type": "ctor",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`origin` {string} The origin to associate this mock client with. It should only include the protocol, hostname, and port.",
                  "name": "origin",
                  "type": "string",
                  "desc": "The origin to associate this mock client with. It should only include the protocol, hostname, and port."
                },
                {
                  "textRaw": "`options` {MockClientOptions} Extends the `Client` options.",
                  "name": "options",
                  "type": "MockClientOptions",
                  "desc": "Extends the `Client` options.",
                  "optional": true
                }
              ],
              "return": {
                "textRaw": "Returns: {MockClient}",
                "name": "return",
                "type": "MockClient"
              },
              "desc": "<p>The <code>agent</code> option is required and must implement the <code>Agent</code> interface;\notherwise an <code>InvalidArgumentError</code> is thrown.</p>",
              "modules": [
                {
                  "textRaw": "Parameter: `MockClientOptions`",
                  "name": "parameter:_`mockclientoptions`",
                  "type": "module",
                  "desc": "<p>Extends: <code>ClientOptions</code></p>\n<ul>\n<li><code>agent</code> <a href=\"MockAgent.md#class-mockagent\"><code>&#x3C;MockAgent></code></a> The agent to associate this mock client with.</li>\n<li><code>ignoreTrailingSlash</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#boolean_type\"><code>&#x3C;boolean></code></a> Whether trailing slashes should be ignored\nwhen matching the path of intercepted requests. <strong>Default:</strong> <code>false</code>.</li>\n</ul>",
                  "displayName": "Parameter: `MockClientOptions`"
                }
              ]
            }
          ],
          "methods": [
            {
              "textRaw": "`mockClient.intercept(options)`",
              "name": "intercept",
              "type": "method",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`options` {MockInterceptor.Options} The matching criteria for the requests to intercept.",
                      "name": "options",
                      "type": "MockInterceptor.Options",
                      "desc": "The matching criteria for the requests to intercept.",
                      "options": [
                        {
                          "textRaw": "`path` {string|RegExp|Function} The path to match against. A function receives the request path as a `string` and returns a `boolean`.",
                          "name": "path",
                          "type": "string|RegExp|Function",
                          "desc": "The path to match against. A function receives the request path as a `string` and returns a `boolean`."
                        },
                        {
                          "textRaw": "`method` {string|RegExp|Function} The method to match against. A function receives the request method as a `string` and returns a `boolean`. **Default:** `'GET'`.",
                          "name": "method",
                          "type": "string|RegExp|Function",
                          "default": "`'GET'`",
                          "desc": "The method to match against. A function receives the request method as a `string` and returns a `boolean`."
                        },
                        {
                          "textRaw": "`body` {string|RegExp|Function} The body to match against. A function receives the request body as a `string` and returns a `boolean`.",
                          "name": "body",
                          "type": "string|RegExp|Function",
                          "desc": "The body to match against. A function receives the request body as a `string` and returns a `boolean`."
                        },
                        {
                          "textRaw": "`headers` {Object|Function} The headers to match against, as a map of header name to a `string`, `RegExp`, or matching function, or a single function that receives all headers and returns a `boolean`.",
                          "name": "headers",
                          "type": "Object|Function",
                          "desc": "The headers to match against, as a map of header name to a `string`, `RegExp`, or matching function, or a single function that receives all headers and returns a `boolean`."
                        },
                        {
                          "textRaw": "`query` {Object} The query parameters to match against.",
                          "name": "query",
                          "type": "Object",
                          "desc": "The query parameters to match against."
                        },
                        {
                          "textRaw": "`ignoreTrailingSlash` {boolean} Whether a trailing slash on `path` is ignored when matching. Inherited from the mock client's `ignoreTrailingSlash` option when not set.",
                          "name": "ignoreTrailingSlash",
                          "type": "boolean",
                          "desc": "Whether a trailing slash on `path` is ignored when matching. Inherited from the mock client's `ignoreTrailingSlash` option when not set."
                        }
                      ]
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {MockInterceptor} The interceptor used to define the mocked reply.",
                    "name": "return",
                    "type": "MockInterceptor",
                    "desc": "The interceptor used to define the mocked reply."
                  }
                }
              ],
              "desc": "<p>Registers a route on the mock client and returns a <a href=\"MockPool.html#return-mockinterceptor\"><code>MockInterceptor</code></a> for any\nmatching request that uses the same origin as this mock client. The interceptor\nis then used to define the reply, for example with <code>reply()</code> or\n<code>replyWithError()</code>.</p>\n<pre><code class=\"language-mjs\">import { MockAgent } from 'undici'\n\nconst mockAgent = new MockAgent({ connections: 1 })\nconst mockClient = mockAgent.get('http://localhost:3000')\n\nmockClient.intercept({ path: '/foo', method: 'GET' }).reply(200, 'foo')\n</code></pre>"
            },
            {
              "textRaw": "`mockClient.cleanMocks()`",
              "name": "cleanMocks",
              "type": "method",
              "meta": {
                "added": [
                  "v7.11.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": []
                }
              ],
              "desc": "<p>Removes all registered interceptors from the mock client. Pending mocks defined\nbefore this call no longer match incoming requests.</p>\n<pre><code class=\"language-mjs\">import { MockAgent } from 'undici'\n\nconst mockAgent = new MockAgent({ connections: 1 })\nconst mockClient = mockAgent.get('http://localhost:3000')\n\nmockClient.intercept({ path: '/foo' }).reply(200, 'foo')\nmockClient.cleanMocks()\n</code></pre>"
            },
            {
              "textRaw": "`mockClient.close()`",
              "name": "close",
              "type": "method",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [],
                  "return": {
                    "textRaw": "Returns: {Promise} Fulfills with `undefined` once the mock client is closed.",
                    "name": "return",
                    "type": "Promise",
                    "desc": "Fulfills with `undefined` once the mock client is closed."
                  }
                }
              ],
              "desc": "<p>Closes the mock client, gracefully waiting for any enqueued requests to\ncomplete, and removes it from the associated <a href=\"MockAgent.html#class-mockagent\"><code>MockAgent</code></a>.</p>\n<pre><code class=\"language-mjs\">import { MockAgent } from 'undici'\n\nconst mockAgent = new MockAgent({ connections: 1 })\nconst mockClient = mockAgent.get('http://localhost:3000')\n\nawait mockClient.close()\n</code></pre>"
            },
            {
              "textRaw": "`mockClient.dispatch(options, handlers)`",
              "name": "dispatch",
              "type": "method",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`options` {DispatchOptions} The request options.",
                      "name": "options",
                      "type": "DispatchOptions",
                      "desc": "The request options."
                    },
                    {
                      "textRaw": "`handlers` {DispatchHandler} The handlers invoked over the request lifecycle.",
                      "name": "handlers",
                      "type": "DispatchHandler",
                      "desc": "The handlers invoked over the request lifecycle."
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {boolean} `false` if the dispatcher is busy and the caller should wait before dispatching further requests, otherwise `true`.",
                    "name": "return",
                    "type": "boolean",
                    "desc": "`false` if the dispatcher is busy and the caller should wait before dispatching further requests, otherwise `true`."
                  }
                }
              ],
              "desc": "<p>Dispatches a request, matching it against the registered mocks. This override of\n<a href=\"Dispatcher.html#dispatcherdispatchoptions-handler\"><code>dispatcher.dispatch(options, handlers)</code></a> is what drives the mocking\nbehaviour for every higher-level method such as <code>request</code>.</p>"
            },
            {
              "textRaw": "`mockClient.request(options[, callback])`",
              "name": "request",
              "type": "method",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`options` {DispatchOptions}",
                      "name": "options",
                      "type": "DispatchOptions"
                    },
                    {
                      "textRaw": "`callback` {Function} (optional) Invoked with the response when no `Promise` is requested.",
                      "name": "callback",
                      "type": "Function",
                      "desc": "(optional) Invoked with the response when no `Promise` is requested.",
                      "optional": true
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {Promise} Fulfills with the mocked response when no `callback` is provided.",
                    "name": "return",
                    "type": "Promise",
                    "desc": "Fulfills with the mocked response when no `callback` is provided."
                  }
                }
              ],
              "desc": "<p>Performs a request and resolves it against the registered mocks. Inherited from\n<a href=\"Client.html#class-client\"><code>Client</code></a>; see [<code>dispatcher.request(options[, callback])</code>][] for the full\nparameter and return value documentation.</p>\n<pre><code class=\"language-mjs\">import { MockAgent } from 'undici'\n\nconst mockAgent = new MockAgent({ connections: 1 })\nconst mockClient = mockAgent.get('http://localhost:3000')\nmockClient.intercept({ path: '/foo' }).reply(200, 'foo')\n\nconst { statusCode, body } = await mockClient.request({\n  origin: 'http://localhost:3000',\n  path: '/foo',\n  method: 'GET'\n})\n\nconsole.log('response received', statusCode) // response received 200\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')) // data foo\n}\n</code></pre>\n<p>[<code>dispatcher.request(options[, callback])</code>]: Dispatcher.md#dispatcherrequestoptions-callback\n[<code>mockAgent.get(origin)</code>]: MockAgent.md#mockagentgetorigin</p>"
            }
          ]
        }
      ]
    }
  ]
}