forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue-1447.js
More file actions
41 lines (32 loc) · 977 Bytes
/
issue-1447.js
File metadata and controls
41 lines (32 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict'
const { test } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const undici = require('../..')
const { fetch: theoreticalGlobalFetch } = require('../../undici-fetch')
test('Mocking works with both fetches', async (t) => {
const { strictEqual } = tspl(t, { plan: 3 })
const mockAgent = new undici.MockAgent()
const body = JSON.stringify({ foo: 'bar' })
mockAgent.disableNetConnect()
undici.setGlobalDispatcher(mockAgent)
const pool = mockAgent.get('https://example.com')
pool.intercept({
path: '/path',
method: 'POST',
body (bodyString) {
strictEqual(bodyString, body)
return true
}
}).reply(200, { ok: 1 }).times(2)
const url = new URL('https://example.com/path').href
// undici fetch from node_modules
await undici.fetch(url, {
method: 'POST',
body
})
// the global fetch bundled with esbuild
await theoreticalGlobalFetch(url, {
method: 'POST',
body
})
})