On this page

M

MockCallHistoryLog

History
Source Code: lib/mock/mock-call-history.js
Stability: 2Stable

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()
C

MockCallHistoryLog

History

Represents the configuration of one intercepted request.

C

MockCallHistoryLog Constructor

History
new MockCallHistoryLog(requestInit?): void
Attributes
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.

P

mockCallHistoryLog.body

History

The body of the intercepted request, exactly as supplied to the dispatcher.

P

mockCallHistoryLog.headers

History

The headers of the intercepted request.

P

mockCallHistoryLog.method

History

The HTTP method of the intercepted request, for example 'GET' or 'PUT'.

P

mockCallHistoryLog.fullUrl

History

The full URL of the intercepted request, including protocol, host, path, search parameters, and hash.

P

mockCallHistoryLog.origin

History

The origin of the intercepted request, comprising the protocol and host, for example 'https://localhost:4000'.

P

mockCallHistoryLog.path

History

The pathname of the intercepted request. It always starts with / and never contains search parameters.

P

mockCallHistoryLog.searchParams

History

The search parameters of the intercepted request as a plain object of key/value pairs.

P

mockCallHistoryLog.protocol

History

The protocol of the intercepted request, including the trailing colon, for example 'https:' or 'http:'.

P

mockCallHistoryLog.host

History

The host of the intercepted request, including the port when present.

P

mockCallHistoryLog.port

History

The port of the intercepted request. It is an empty string when no explicit port was used, otherwise a string of digits.

P

mockCallHistoryLog.hash

History

The hash fragment of the intercepted request. It is an empty string when absent, otherwise a string starting with #.

M

mockCallHistoryLog.toMap

History
mockCallHistoryLog.toMap(): Map
Returns:<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'
M

mockCallHistoryLog.toString

History
mockCallHistoryLog.toString(): string
Returns:<string>
A string representation of the log.

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"}