A MockCallHistoryLog is an immutable snapshot of the configuration of a single
request that was intercepted by a MockAgent. Each property mirrors a part
of the request, with the URL components already parsed by the WHATWG URL parser.
Instances are not created directly. They are produced by a MockCallHistory
when call history is enabled on a MockAgent, and can be retrieved through
the call history helpers such as firstCall(), lastCall(), nthCall(),
calls(), and filterCalls().
import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ enableCallHistory: true })
// after some requests have been intercepted:
const log = mockAgent.getCallHistory()?.firstCall()Represents the configuration of one intercepted request.
new MockCallHistoryLog(requestInit?): void<DispatchOptions>{}
.Creates a new MockCallHistoryLog. The body, headers, and method
properties are copied directly from requestInit, while the URL-derived
properties (fullUrl, origin, path, searchParams, protocol, host,
port, and hash) are computed from requestInit.path and requestInit.origin
using the WHATWG URL parser.
This constructor is internal-facing: instances are created by
MockCallHistory when a request is intercepted. It is documented because the
class is part of the public API surface.
<string>
|
<null>
|
<undefined>The body of the intercepted request, exactly as supplied to the dispatcher.
The headers of the intercepted request.
<string>The HTTP method of the intercepted request, for example 'GET' or 'PUT'.
<string>The full URL of the intercepted request, including protocol, host, path, search parameters, and hash.
<string>The origin of the intercepted request, comprising the protocol and host, for
example 'https://localhost:4000'.
<string>The pathname of the intercepted request. It always starts with / and never
contains search parameters.
The search parameters of the intercepted request as a plain object of key/value pairs.
<string>The protocol of the intercepted request, including the trailing colon, for
example 'https:' or 'http:'.
<string>The host of the intercepted request, including the port when present.
<string>The port of the intercepted request. It is an empty string when no explicit port was used, otherwise a string of digits.
<string>The hash fragment of the intercepted request. It is an empty string when absent,
otherwise a string starting with #.
mockCallHistoryLog.toMap(): Map<Map>Map
whose keys are the property names
protocol
,
host
,
port
,
origin
,
path
,
hash
,
searchParams
,
fullUrl
,
method
,
body
,
and
headers
, each mapped to the corresponding value of this log.Returns the log as a Map of property name to value.
mockAgent.getCallHistory()?.firstCall()?.toMap().get('hash')
// '#hash'mockCallHistoryLog.toString(): string<string>Returns a string built from every property name and value pair. Each pair is
joined with -> and pairs are separated with |. Object values such as
searchParams and headers are serialized with JSON.stringify().
mockAgent.getCallHistory()?.firstCall()?.toString()
// protocol->https:|host->localhost:4000|port->4000|origin->https://localhost:4000|path->/endpoint|hash->#here|searchParams->{"query":"value"}|fullUrl->https://localhost:4000/endpoint?query=value#here|method->PUT|body->"{ "data": "hello" }"|headers->{"content-type":"application/json"}