deps/undici/src/docs/docs/api/MockCallHistoryLog.md
Stability: 2 - Stable
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()
MockCallHistoryLogRepresents the configuration of one intercepted request.
new MockCallHistoryLog(requestInit)requestInit {DispatchOptions} The dispatch options of the intercepted
request. Default: {}.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.
mockCallHistoryLog.bodyThe body of the intercepted request, exactly as supplied to the dispatcher.
mockCallHistoryLog.headersThe headers of the intercepted request.
mockCallHistoryLog.methodThe HTTP method of the intercepted request, for example 'GET' or 'PUT'.
mockCallHistoryLog.fullUrlThe full URL of the intercepted request, including protocol, host, path, search parameters, and hash.
mockCallHistoryLog.originThe origin of the intercepted request, comprising the protocol and host, for
example 'https://localhost:4000'.
mockCallHistoryLog.pathThe pathname of the intercepted request. It always starts with / and never
contains search parameters.
mockCallHistoryLog.searchParamsThe search parameters of the intercepted request as a plain object of key/value pairs.
mockCallHistoryLog.protocolThe protocol of the intercepted request, including the trailing colon, for
example 'https:' or 'http:'.
mockCallHistoryLog.hostThe host of the intercepted request, including the port when present.
mockCallHistoryLog.portThe port of the intercepted request. It is an empty string when no explicit port was used, otherwise a string of digits.
mockCallHistoryLog.hashThe hash fragment of the intercepted request. It is an empty string when absent,
otherwise a string starting with #.
mockCallHistoryLog.toMap()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()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"}