{
  "type": "module",
  "source": "doc/api/api-mockpool.md",
  "modules": [
    {
      "textRaw": "MockPool",
      "name": "mockpool",
      "introduced_in": "v4.0.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>A <code>MockPool</code> is a <a href=\"Pool.html#class-pool\"><code>Pool</code></a> that intercepts requests matching registered routes\nand replies with mocked responses instead of contacting the network. It is\ncreated by a <a href=\"MockAgent.html#class-mockagent\"><code>MockAgent</code></a> and exposes the same interception API as\n<a href=\"MockClient.html#class-mockclient\"><code>MockClient</code></a>.</p>\n<p>A <code>MockPool</code> is not constructed directly in most cases; instead it is obtained\nthrough [<code>mockAgent.get(origin)</code>][]. Once registered as the global dispatcher,\nany request whose origin matches the pool is matched against the mocks it holds.</p>\n<pre><code class=\"language-mjs\">import { MockAgent } from 'undici'\n\nconst mockAgent = new MockAgent()\nconst mockPool = mockAgent.get('http://localhost:3000')\n</code></pre>\n<pre><code class=\"language-cjs\">const { MockAgent } = require('undici')\n\nconst mockAgent = new MockAgent()\nconst mockPool = mockAgent.get('http://localhost:3000')\n</code></pre>",
      "classes": [
        {
          "textRaw": "Class: `MockPool`",
          "name": "MockPool",
          "type": "class",
          "meta": {
            "added": [
              "v4.0.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"Pool.md#class-pool\"><code>&#x3C;Pool></code></a></li>\n</ul>\n<p>Extends <a href=\"Pool.html#class-pool\"><code>Pool</code></a> and implements the <code>Interceptable</code> interface, allowing\nrequests made through it to be matched against registered mocks.</p>",
          "signatures": [
            {
              "textRaw": "`new MockPool(origin[, options])`",
              "name": "MockPool",
              "type": "ctor",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`origin` {string} The origin to associate this mock pool with. It should only include the protocol, hostname, and port.",
                  "name": "origin",
                  "type": "string",
                  "desc": "The origin to associate this mock pool with. It should only include the protocol, hostname, and port."
                },
                {
                  "textRaw": "`options` {MockPoolOptions} Extends the `Pool` options.",
                  "name": "options",
                  "type": "MockPoolOptions",
                  "desc": "Extends the `Pool` options.",
                  "optional": true
                }
              ],
              "return": {
                "textRaw": "Returns: {MockPool}",
                "name": "return",
                "type": "MockPool"
              },
              "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: `MockPoolOptions`",
                  "name": "parameter:_`mockpooloptions`",
                  "type": "module",
                  "desc": "<p>Extends: <code>PoolOptions</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 pool 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: `MockPoolOptions`"
                }
              ]
            }
          ],
          "methods": [
            {
              "textRaw": "`mockPool.intercept(options)`",
              "name": "intercept",
              "type": "method",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`options` {MockPoolInterceptOptions} The matching criteria for the requests to intercept.",
                      "name": "options",
                      "type": "MockPoolInterceptOptions",
                      "desc": "The matching criteria for the requests to intercept."
                    }
                  ],
                  "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 pool 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 pool. The interceptor is\nthen used to define the reply, for example with <code>reply()</code> or <code>replyWithError()</code>.</p>\n<p>Each <code>intercept()</code> call is consumed by a single matching request. To match more\nthan one request, call <code>intercept()</code> once per expected request, or use\n<code>persist()</code> or <code>times()</code> on the returned <a href=\"MockPool.html#return-mockscope\"><code>MockScope</code></a>. When no registered\nmock matches a request, a real request is attempted unless network connections\nhave been disabled on the <a href=\"MockAgent.html#class-mockagent\"><code>MockAgent</code></a>, in which case a <code>MockNotMatchedError</code>\nis thrown.</p>\n<p>A request is intercepted only when every defined matcher passes. The accepted\nmatcher forms behave as follows:</p>\n<table>\n<thead>\n<tr>\n<th align=\"center\">Matcher type</th>\n<th>Condition to pass</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"center\"><code>string</code></td>\n<td>Exact match against the value</td>\n</tr>\n<tr>\n<td align=\"center\"><code>RegExp</code></td>\n<td>The expression must match</td>\n</tr>\n<tr>\n<td align=\"center\"><code>Function</code></td>\n<td>The function must return <code>true</code></td>\n</tr>\n</tbody>\n</table>",
              "modules": [
                {
                  "textRaw": "Parameter: `MockPoolInterceptOptions`",
                  "name": "parameter:_`mockpoolinterceptoptions`",
                  "type": "module",
                  "desc": "<ul>\n<li><code>path</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp\"><code>&#x3C;RegExp></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> A matcher for the HTTP request path. The\nfunction form has the signature <code>(path: string) => boolean</code>. When a <code>RegExp</code>\nor function is used, it matches against the request path including all query\nparameters in alphabetical order. When a <code>string</code> is used, query parameters\ncan instead be supplied through <code>query</code>.</li>\n<li><code>method</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp\"><code>&#x3C;RegExp></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> A matcher for the HTTP request method. The\nfunction form has the signature <code>(method: string) => boolean</code>.\n<strong>Default:</strong> <code>'GET'</code>.</li>\n<li><code>body</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp\"><code>&#x3C;RegExp></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> A matcher for the HTTP request body. The\nfunction form has the signature <code>(body: string) => boolean</code>.</li>\n<li><code>headers</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> A matcher for the HTTP request headers, given\neither as a map of header name to a <code>string</code>, <code>RegExp</code>, or\n<code>(value: string) => boolean</code> matcher, or as a single function that receives\nall headers and returns a <code>boolean</code>. When a map is used, the request must\nmatch every defined header; extra headers not listed here do not affect\nmatching.</li>\n<li><code>query</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> A matcher for the HTTP request query string parameters. Only\napplies when a <code>string</code> was provided for <code>path</code>.</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 a trailing slash on <code>path</code> is ignored\nwhen matching. Inherited from the mock pool's <code>ignoreTrailingSlash</code> option\nwhen not set.</li>\n</ul>",
                  "displayName": "Parameter: `MockPoolInterceptOptions`"
                },
                {
                  "textRaw": "Return: `MockInterceptor`",
                  "name": "return:_`mockinterceptor`",
                  "type": "module",
                  "desc": "<p>The reply behaviour of a matching request is defined through the returned\n<code>MockInterceptor</code>.</p>\n<ul>\n<li><code>reply(statusCode[, data[, responseOptions]])</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Defines the reply\nfor a matching request.\n<ul>\n<li><code>statusCode</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> The status code of the mocked reply.</li>\n<li><code>data</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> | <a href=\"https://nodejs.org/api/buffer.html#class-buffer\"><code>&#x3C;Buffer></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> The body of the mocked reply.\nObjects are serialized to JSON; strings and <code>Buffer</code>s are sent as-is. The\nfunction form has the signature\n<code>(opts: MockResponseCallbackOptions) => string | Buffer | Object</code> and is\ninvoked with the incoming request to compute the reply body.</li>\n<li><code>responseOptions</code> <a href=\"MockPool.md#parameter-mockresponseoptions\"><code>&#x3C;MockResponseOptions></code></a> Additional reply options. <strong>Default:</strong> <code>{}</code>.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockscope\"><code>&#x3C;MockScope></code></a></li>\n</ul>\n</li>\n<li><code>reply(callback)</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Defines the reply for a matching request,\ncomputing all reply options dynamically rather than just the body.\n<ul>\n<li><code>callback</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> A <code>(opts: MockResponseCallbackOptions) => { statusCode, data, responseOptions }</code> function invoked with the incoming\nrequest.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockscope\"><code>&#x3C;MockScope></code></a></li>\n</ul>\n</li>\n<li><code>replyWithError(error)</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Defines an error for a matching request to\nthrow.\n<ul>\n<li><code>error</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error\"><code>&#x3C;Error></code></a> The error thrown when a request matches.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockscope\"><code>&#x3C;MockScope></code></a></li>\n</ul>\n</li>\n<li><code>defaultReplyHeaders(headers)</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Sets default headers included on\nevery subsequent reply defined on this interceptor, in addition to any headers\nset on a specific reply.\n<ul>\n<li><code>headers</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> A map of header name to value.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockinterceptor\"><code>&#x3C;MockInterceptor></code></a></li>\n</ul>\n</li>\n<li><code>defaultReplyTrailers(trailers)</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Sets default trailers included on\nevery subsequent reply defined on this interceptor, in addition to any\ntrailers set on a specific reply.\n<ul>\n<li><code>trailers</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> A map of trailer name to value.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockinterceptor\"><code>&#x3C;MockInterceptor></code></a></li>\n</ul>\n</li>\n<li><code>replyContentLength()</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Sets an automatically calculated <code>content-length</code> header on every subsequent reply defined on this interceptor.\n<ul>\n<li>Returns: <a href=\"MockPool.md#return-mockinterceptor\"><code>&#x3C;MockInterceptor></code></a></li>\n</ul>\n</li>\n</ul>\n<p>By default, <code>reply()</code> and <code>replyWithError()</code> define the behaviour for the first\nmatching request only; subsequent requests are not affected unless <code>persist()</code>\nor <code>times()</code> is used on the returned <a href=\"MockPool.html#return-mockscope\"><code>MockScope</code></a>.</p>",
                  "displayName": "Return: `MockInterceptor`"
                },
                {
                  "textRaw": "Parameter: `MockResponseCallbackOptions`",
                  "name": "parameter:_`mockresponsecallbackoptions`",
                  "type": "module",
                  "desc": "<p>The argument passed to the <code>reply()</code> data and options callbacks.</p>\n<ul>\n<li><code>path</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> The path of the intercepted request.</li>\n<li><code>method</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> The method of the intercepted request.</li>\n<li><code>headers</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/API/Headers\"><code>&#x3C;Headers></code></a> The headers of the intercepted request.</li>\n<li><code>origin</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> The origin of the intercepted request.</li>\n<li><code>body</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type\"><code>&#x3C;string></code></a> | <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#null_type\"><code>&#x3C;null></code></a> The body of the intercepted request.</li>\n</ul>",
                  "displayName": "Parameter: `MockResponseCallbackOptions`"
                },
                {
                  "textRaw": "Parameter: `MockResponseOptions`",
                  "name": "parameter:_`mockresponseoptions`",
                  "type": "module",
                  "desc": "<ul>\n<li><code>headers</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> Headers to include on the mocked reply.</li>\n<li><code>trailers</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\"><code>&#x3C;Object></code></a> Trailers to include on the mocked reply.</li>\n</ul>",
                  "displayName": "Parameter: `MockResponseOptions`"
                },
                {
                  "textRaw": "Return: `MockScope`",
                  "name": "return:_`mockscope`",
                  "type": "module",
                  "desc": "<p>A <code>MockScope</code> is associated with a single <a href=\"MockPool.html#return-mockinterceptor\"><code>MockInterceptor</code></a> and configures\nhow many times the defined reply is used.</p>\n<ul>\n<li><code>delay(waitInMs)</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Delays the associated reply by a set amount of\ntime.\n<ul>\n<li><code>waitInMs</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> The delay in milliseconds.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockscope\"><code>&#x3C;MockScope></code></a></li>\n</ul>\n</li>\n<li><code>persist()</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Makes the associated reply match indefinitely, so every\nmatching request receives the defined response.\n<ul>\n<li>Returns: <a href=\"MockPool.md#return-mockscope\"><code>&#x3C;MockScope></code></a></li>\n</ul>\n</li>\n<li><code>times(repeatTimes)</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function\"><code>&#x3C;Function></code></a> Makes the associated reply match a fixed number\nof times. This is overridden by <code>persist()</code>.\n<ul>\n<li><code>repeatTimes</code> <a href=\"https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type\"><code>&#x3C;number></code></a> The number of matching requests the reply applies to.</li>\n<li>Returns: <a href=\"MockPool.md#return-mockscope\"><code>&#x3C;MockScope></code></a></li>\n</ul>\n</li>\n</ul>\n<p>The following examples show common interception patterns.</p>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\nmockPool.intercept({ path: '/foo' }).reply(200, 'foo')\n\nconst { statusCode, body } = await request('http://localhost:3000/foo')\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<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({\n  path: '/echo',\n  method: 'GET'\n}).reply(200, ({ headers }) => ({ message: headers.message }))\n\nconst { statusCode, body } = await request('http://localhost:3000/echo', {\n  headers: { message: 'hello world!' }\n})\n\nconsole.log('response received', statusCode) // response received 200\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')) // {\"message\":\"hello world!\"}\n}\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({\n  path: '/echo',\n  method: 'GET'\n}).reply(({ headers }) => ({\n  statusCode: 200,\n  data: { message: headers.message }\n}))\n\nconst { statusCode, body } = await request('http://localhost:3000/echo', {\n  headers: { message: 'hello world!' }\n})\n\nconsole.log('response received', statusCode) // response received 200\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')) // {\"message\":\"hello world!\"}\n}\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo', method: 'GET' }).reply(200, 'foo')\nmockPool.intercept({ path: '/hello', method: 'GET' }).reply(200, 'hello')\n\nconst fooResult = await request('http://localhost:3000/foo')\nfor await (const data of fooResult.body) {\n  console.log('data', data.toString('utf8')) // data foo\n}\n\nconst helloResult = await request('http://localhost:3000/hello')\nfor await (const data of helloResult.body) {\n  console.log('data', data.toString('utf8')) // data hello\n}\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({\n  path: '/foo?hello=there&#x26;see=ya',\n  method: 'POST',\n  body: 'form1=data1&#x26;form2=data2',\n  headers: {\n    'User-Agent': 'undici',\n    Host: 'example.com'\n  }\n}).reply(200, { foo: 'bar' }, {\n  headers: { 'content-type': 'application/json' },\n  trailers: { 'Content-MD5': 'test' }\n})\n\nconst { statusCode, headers, trailers, body } = await request(\n  'http://localhost:3000/foo?hello=there&#x26;see=ya',\n  {\n    method: 'POST',\n    body: 'form1=data1&#x26;form2=data2',\n    headers: {\n      foo: 'bar',\n      'User-Agent': 'undici',\n      Host: 'example.com'\n    }\n  }\n)\n\nconsole.log('response received', statusCode) // response received 200\nconsole.log('headers', headers) // { 'content-type': 'application/json' }\n\nfor await (const data of body) {\n  console.log('data', data.toString('utf8')) // {\"foo\":\"bar\"}\n}\n\nconsole.log('trailers', trailers) // { 'content-md5': 'test' }\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({\n  path: '/foo',\n  method: /^GET$/,\n  body: (value) => value === 'form=data',\n  headers: {\n    'User-Agent': 'undici',\n    Host: /^example\\.com$/\n  }\n}).reply(200, 'foo')\n\nconst { statusCode, body } = await request('http://localhost:3000/foo', {\n  method: 'GET',\n  body: 'form=data',\n  headers: {\n    foo: 'bar',\n    'User-Agent': 'undici',\n    Host: 'example.com'\n  }\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<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({\n  path: '/foo',\n  method: 'GET'\n}).replyWithError(new Error('kaboom'))\n\ntry {\n  await request('http://localhost:3000/foo', { method: 'GET' })\n} catch (error) {\n  console.error(error.message) // kaboom\n}\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo', method: 'GET' })\n  .defaultReplyHeaders({ foo: 'bar' })\n  .reply(200, 'foo')\n\nconst { headers } = await request('http://localhost:3000/foo')\n\nconsole.log('headers', headers) // headers { foo: 'bar' }\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo', method: 'GET' })\n  .defaultReplyTrailers({ foo: 'bar' })\n  .reply(200, 'foo')\n\nconst { trailers } = await request('http://localhost:3000/foo')\n\nconsole.log('trailers', trailers) // trailers { foo: 'bar' }\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo', method: 'GET' })\n  .replyContentLength()\n  .reply(200, 'foo')\n\nconst { headers } = await request('http://localhost:3000/foo')\n\nconsole.log('headers', headers) // headers { 'content-length': '3' }\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo', method: 'GET' }).reply(200, 'foo').persist()\n\nawait request('http://localhost:3000/foo') // Matches and returns the mock\nawait request('http://localhost:3000/foo') // Matches again, indefinitely\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo', method: 'GET' }).reply(200, 'foo').times(2)\n\nawait request('http://localhost:3000/foo') // Matches and returns the mock\nawait request('http://localhost:3000/foo') // Matches and returns the mock\nawait request('http://localhost:3000/foo') // No match; a real request is attempted\n</code></pre>\n<pre><code class=\"language-mjs\">import { MockAgent, setGlobalDispatcher, request } from 'undici'\nimport querystring from 'node:querystring'\n\nconst mockAgent = new MockAgent()\nsetGlobalDispatcher(mockAgent)\n\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nconst matchPath = (requestPath) => {\n  const [pathname, search] = requestPath.split('?')\n  const requestQuery = querystring.parse(search)\n\n  if (!pathname.startsWith('/foo')) {\n    return false\n  }\n\n  return requestQuery.foo === 'bar'\n}\n\nmockPool.intercept({ path: matchPath, method: 'GET' }).reply(200, 'foo')\n\nawait request('http://localhost:3000/foo?foo=bar') // Matches and returns the mock\n</code></pre>",
                  "displayName": "Return: `MockScope`"
                }
              ]
            },
            {
              "textRaw": "`mockPool.close()`",
              "name": "close",
              "type": "method",
              "meta": {
                "added": [
                  "v4.0.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [],
                  "return": {
                    "textRaw": "Returns: {Promise} Fulfills with `undefined` once the mock pool is closed.",
                    "name": "return",
                    "type": "Promise",
                    "desc": "Fulfills with `undefined` once the mock pool is closed."
                  }
                }
              ],
              "desc": "<p>Closes the mock pool, gracefully waiting for any enqueued requests to complete,\nand 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()\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nawait mockPool.close()\n</code></pre>"
            },
            {
              "textRaw": "`mockPool.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": "`mockPool.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=\"Pool.html#class-pool\"><code>Pool</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()\nconst mockPool = mockAgent.get('http://localhost:3000')\nmockPool.intercept({ path: '/foo', method: 'GET' }).reply(200, 'foo')\n\nconst { statusCode, body } = await mockPool.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>"
            },
            {
              "textRaw": "`mockPool.cleanMocks()`",
              "name": "cleanMocks",
              "type": "method",
              "meta": {
                "added": [
                  "v7.11.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [],
                  "return": {
                    "textRaw": "Returns: {undefined}",
                    "name": "return",
                    "type": "undefined"
                  }
                }
              ],
              "desc": "<p>Removes all registered interceptors from the mock pool. 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()\nconst mockPool = mockAgent.get('http://localhost:3000')\n\nmockPool.intercept({ path: '/foo' }).reply(200, 'foo')\nmockPool.cleanMocks()\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>"
            }
          ]
        }
      ]
    }
  ]
}