Skip to content

deps: update undici to 7.5.0 #57427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions deps/undici/src/docs/docs/api/MockAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ for await (const data of result2.body) {
console.log('data', data.toString('utf8')) // data hello
}
```

#### Example - Mock different requests within the same file

```js
const { MockAgent, setGlobalDispatcher } = require('undici');
const agent = new MockAgent();
Expand Down Expand Up @@ -540,3 +542,60 @@ agent.assertNoPendingInterceptors()
// │ 0 │ 'GET' │ 'https://example.com' │ '/' │ 200 │ '❌' │ 0 │ 1 │
// └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘
```

#### Example - access call history on MockAgent

You can register every call made within a MockAgent to be able to retrieve the body, headers and so on.

This is not enabled by default.

```js
import { MockAgent, setGlobalDispatcher, request } from 'undici'

const mockAgent = new MockAgent({ enableCallHistory: true })
setGlobalDispatcher(mockAgent)

await request('http://example.com', { query: { item: 1 }})

mockAgent.getCallHistory()?.firstCall()
// Returns
// MockCallHistoryLog {
// body: undefined,
// headers: undefined,
// method: 'GET',
// origin: 'http://example.com',
// fullUrl: 'http://example.com/?item=1',
// path: '/',
// searchParams: { item: '1' },
// protocol: 'http:',
// host: 'example.com',
// port: ''
// }
```

#### Example - clear call history

```js
const mockAgent = new MockAgent()

mockAgent.clearAllCallHistory()
```

#### Example - call history instance class method

```js
const mockAgent = new MockAgent()

const mockAgentHistory = mockAgent.getCallHistory()

mockAgentHistory?.calls() // returns an array of MockCallHistoryLogs
mockAgentHistory?.firstCall() // returns the first MockCallHistoryLogs or undefined
mockAgentHistory?.lastCall() // returns the last MockCallHistoryLogs or undefined
mockAgentHistory?.nthCall(3) // returns the third MockCallHistoryLogs or undefined
mockAgentHistory?.filterCalls({ path: '/endpoint', hash: '#hash-value' }) // returns an Array of MockCallHistoryLogs WHERE path === /endpoint OR hash === #hash-value
mockAgentHistory?.filterCalls({ path: '/endpoint', hash: '#hash-value' }, { operator: 'AND' }) // returns an Array of MockCallHistoryLogs WHERE path === /endpoint AND hash === #hash-value
mockAgentHistory?.filterCalls(/"data": "{}"/) // returns an Array of MockCallHistoryLogs where any value match regexp
mockAgentHistory?.filterCalls('application/json') // returns an Array of MockCallHistoryLogs where any value === 'application/json'
mockAgentHistory?.filterCalls((log) => log.path === '/endpoint') // returns an Array of MockCallHistoryLogs when given function returns true
mockAgentHistory?.clear() // clear the history
```
197 changes: 197 additions & 0 deletions deps/undici/src/docs/docs/api/MockCallHistory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
# Class: MockCallHistory

Access to an instance with :

```js
const mockAgent = new MockAgent({ enableCallHistory: true })
mockAgent.getCallHistory()

// or
const mockAgent = new MockAgent()
mockAgent.enableMockHistory()
mockAgent.getCallHistory()

```

a MockCallHistory instance implements a **Symbol.iterator** letting you iterate on registered logs :

```ts
for (const log of mockAgent.getCallHistory()) {
//...
}

const array: Array<MockCallHistoryLog> = [...mockAgent.getCallHistory()]
const set: Set<MockCallHistoryLog> = new Set(mockAgent.getCallHistory())
```

## class methods

### clear

Clear all MockCallHistoryLog registered. This is automatically done when calling `mockAgent.close()`

```js
mockAgent.clearCallHistory()
// same as
mockAgent.getCallHistory()?.clear()
```

### calls

Get all MockCallHistoryLog registered as an array

```js
mockAgent.getCallHistory()?.calls()
```

### firstCall

Get the first MockCallHistoryLog registered or undefined

```js
mockAgent.getCallHistory()?.firstCall()
```

### lastCall

Get the last MockCallHistoryLog registered or undefined

```js
mockAgent.getCallHistory()?.lastCall()
```

### nthCall

Get the nth MockCallHistoryLog registered or undefined

```js
mockAgent.getCallHistory()?.nthCall(3) // the third MockCallHistoryLog registered
```

### filterCallsByProtocol

Filter MockCallHistoryLog by protocol.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByProtocol(/https/)
mockAgent.getCallHistory()?.filterCallsByProtocol('https:')
```

### filterCallsByHost

Filter MockCallHistoryLog by host.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByHost(/localhost/)
mockAgent.getCallHistory()?.filterCallsByHost('localhost:3000')
```

### filterCallsByPort

Filter MockCallHistoryLog by port.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByPort(/3000/)
mockAgent.getCallHistory()?.filterCallsByPort('3000')
mockAgent.getCallHistory()?.filterCallsByPort('')
```

### filterCallsByOrigin

Filter MockCallHistoryLog by origin.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByOrigin(/http:\/\/localhost:3000/)
mockAgent.getCallHistory()?.filterCallsByOrigin('http://localhost:3000')
```

### filterCallsByPath

Filter MockCallHistoryLog by path.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByPath(/api\/v1\/graphql/)
mockAgent.getCallHistory()?.filterCallsByPath('/api/v1/graphql')
```

### filterCallsByHash

Filter MockCallHistoryLog by hash.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByPath(/hash/)
mockAgent.getCallHistory()?.filterCallsByPath('#hash')
```

### filterCallsByFullUrl

Filter MockCallHistoryLog by fullUrl. fullUrl contains protocol, host, port, path, hash, and query params

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByFullUrl(/https:\/\/localhost:3000\/\?query=value#hash/)
mockAgent.getCallHistory()?.filterCallsByFullUrl('https://localhost:3000/?query=value#hash')
```

### filterCallsByMethod

Filter MockCallHistoryLog by method.

> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter)

```js
mockAgent.getCallHistory()?.filterCallsByMethod(/POST/)
mockAgent.getCallHistory()?.filterCallsByMethod('POST')
```

### filterCalls

This class method is a meta function / alias to apply complex filtering in a single way.

Parameters :

- criteria : the first parameter. a function, regexp or object.
- function : filter MockCallHistoryLog when the function returns false
- regexp : filter MockCallHistoryLog when the regexp does not match on MockCallHistoryLog.toString() ([see](./MockCallHistoryLog.md#to-string))
- object : an object with MockCallHistoryLog properties as keys to apply multiple filters. each values are a [filter parameter](/docs/docs/api/MockCallHistory.md#filter-parameter)
- options : the second parameter. an object.
- options.operator : `'AND'` or `'OR'` (default `'OR'`). Used only if criteria is an object. see below

```js
mockAgent.getCallHistory()?.filterCalls((log) => log.hash === value && log.headers?.['authorization'] !== undefined)
mockAgent.getCallHistory()?.filterCalls(/"data": "{ "errors": "wrong body" }"/)

// returns an Array of MockCallHistoryLog which all have
// - a hash containing my-hash
// - OR
// - a path equal to /endpoint
mockAgent.getCallHistory()?.filterCalls({ hash: /my-hash/, path: '/endpoint' })

// returns an Array of MockCallHistoryLog which all have
// - a hash containing my-hash
// - AND
// - a path equal to /endpoint
mockAgent.getCallHistory()?.filterCalls({ hash: /my-hash/, path: '/endpoint' }, { operator: 'AND' })
```

## filter parameter

Can be :

- string. MockCallHistoryLog filtered if `value !== parameterValue`
- null. MockCallHistoryLog filtered if `value !== parameterValue`
- undefined. MockCallHistoryLog filtered if `value !== parameterValue`
- regexp. MockCallHistoryLog filtered if `!parameterValue.test(value)`
43 changes: 43 additions & 0 deletions deps/undici/src/docs/docs/api/MockCallHistoryLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Class: MockCallHistoryLog

Access to an instance with :

```js
const mockAgent = new MockAgent({ enableCallHistory: true })
mockAgent.getCallHistory()?.firstCall()
```

## class properties

- body `mockAgent.getCallHistory()?.firstCall()?.body`
- headers `mockAgent.getCallHistory()?.firstCall()?.headers` an object
- method `mockAgent.getCallHistory()?.firstCall()?.method` a string
- fullUrl `mockAgent.getCallHistory()?.firstCall()?.fullUrl` a string containing the protocol, origin, path, query and hash
- origin `mockAgent.getCallHistory()?.firstCall()?.origin` a string containing the protocol and the host
- headers `mockAgent.getCallHistory()?.firstCall()?.headers` an object
- path `mockAgent.getCallHistory()?.firstCall()?.path` a string always starting with `/`
- searchParams `mockAgent.getCallHistory()?.firstCall()?.searchParams` an object
- protocol `mockAgent.getCallHistory()?.firstCall()?.protocol` a string (`https:`)
- host `mockAgent.getCallHistory()?.firstCall()?.host` a string
- port `mockAgent.getCallHistory()?.firstCall()?.port` an empty string or a string containing numbers
- hash `mockAgent.getCallHistory()?.firstCall()?.hash` an empty string or a string starting with `#`

## class methods

### toMap

Returns a Map instance

```js
mockAgent.getCallHistory()?.firstCall()?.toMap()?.get('hash')
// #hash
```

### toString

Returns a string computed with any class property name and value pair

```js
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"}
```
56 changes: 55 additions & 1 deletion deps/undici/src/docs/docs/best-practices/mocking-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ And this is what the test file looks like:

```js
// index.test.mjs
import { strict as assert } from 'assert'
import { strict as assert } from 'node:assert'
import { MockAgent, setGlobalDispatcher, } from 'undici'
import { bankTransfer } from './bank.mjs'

Expand Down Expand Up @@ -75,6 +75,60 @@ assert.deepEqual(badRequest, { message: 'bank account not found' })

Explore other MockAgent functionality [here](/docs/docs/api/MockAgent.md)

## Access agent call history

Using a MockAgent also allows you to make assertions on the configuration used to make your request in your application.

Here is an example :

```js
// index.test.mjs
import { strict as assert } from 'node:assert'
import { MockAgent, setGlobalDispatcher, fetch } from 'undici'
import { app } from './app.mjs'

// given an application server running on http://localhost:3000
await app.start()

// enable call history at instantiation
const mockAgent = new MockAgent({ enableCallHistory: true })
// or after instantiation
mockAgent.enableCallHistory()

setGlobalDispatcher(mockAgent)

// this call is made (not intercepted)
await fetch(`http://localhost:3000/endpoint?query='hello'`, {
method: 'POST',
headers: { 'content-type': 'application/json' }
body: JSON.stringify({ data: '' })
})

// access to the call history of the MockAgent (which register every call made intercepted or not)
assert.ok(mockAgent.getCallHistory()?.calls().length === 1)
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.fullUrl, `http://localhost:3000/endpoint?query='hello'`)
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.body, JSON.stringify({ data: '' }))
assert.deepStrictEqual(mockAgent.getCallHistory()?.firstCall()?.searchParams, { query: 'hello' })
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.port, '3000')
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.host, 'localhost:3000')
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.method, 'POST')
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.path, '/endpoint')
assert.deepStrictEqual(mockAgent.getCallHistory()?.firstCall()?.headers, { 'content-type': 'application/json' })

// clear all call history logs
mockAgent.clearCallHistory()

assert.ok(mockAgent.getCallHistory()?.calls().length === 0)
```

Calling `mockAgent.close()` will automatically clear and delete every call history for you.

Explore other MockAgent functionality [here](/docs/docs/api/MockAgent.md)

Explore other MockCallHistory functionality [here](/docs/docs/api/MockCallHistory.md)

Explore other MockCallHistoryLog functionality [here](/docs/docs/api/MockCallHistoryLog.md)

## Debug Mock Value

When the interceptor and the request options are not the same, undici will automatically make a real HTTP request. To prevent real requests from being made, use `mockAgent.disableNetConnect()`:
Expand Down
3 changes: 3 additions & 0 deletions deps/undici/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { InvalidArgumentError } = errors
const api = require('./lib/api')
const buildConnector = require('./lib/core/connect')
const MockClient = require('./lib/mock/mock-client')
const { MockCallHistory, MockCallHistoryLog } = require('./lib/mock/mock-call-history')
const MockAgent = require('./lib/mock/mock-agent')
const MockPool = require('./lib/mock/mock-pool')
const mockErrors = require('./lib/mock/mock-errors')
Expand Down Expand Up @@ -169,6 +170,8 @@ module.exports.connect = makeDispatcher(api.connect)
module.exports.upgrade = makeDispatcher(api.upgrade)

module.exports.MockClient = MockClient
module.exports.MockCallHistory = MockCallHistory
module.exports.MockCallHistoryLog = MockCallHistoryLog
module.exports.MockPool = MockPool
module.exports.MockAgent = MockAgent
module.exports.mockErrors = mockErrors
Expand Down
Loading
Loading