blob: b6519d9da14f0fa3f9865235e73e140d3fa600e7 (
plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
'use strict'
const { Dispatcher, setGlobalDispatcher, MockAgent } = require('../..')
/* global expect, it */
class MiniflareDispatcher extends Dispatcher {
constructor (inner, options) {
super(options)
this.inner = inner
}
dispatch (options, handler) {
return this.inner.dispatch(options, handler)
}
close (...args) {
return this.inner.close(...args)
}
destroy (...args) {
return this.inner.destroy(...args)
}
}
const runIf = (condition) => condition ? it : it.skip
const nodeMajor = Number(process.versions.node.split('.', 1)[0])
runIf(nodeMajor >= 16)('https://github.com/nodejs/undici/issues/1757', async () => {
// fetch isn't exported in <16.8
const { fetch } = require('../..')
const mockAgent = new MockAgent()
const mockClient = mockAgent.get('http://localhost:3000')
mockAgent.disableNetConnect()
setGlobalDispatcher(new MiniflareDispatcher(mockAgent))
mockClient.intercept({
path: () => true,
method: () => true
}).reply(200, async (opts) => {
if (opts.body?.[Symbol.asyncIterator]) {
const chunks = []
for await (const chunk of opts.body) {
chunks.push(chunk)
}
return Buffer.concat(chunks)
}
return opts.body
})
const response = await fetch('http://localhost:3000', {
method: 'POST',
body: JSON.stringify({ foo: 'bar' })
})
expect(response.json()).resolves.toMatchObject({ foo: 'bar' })
expect(response.status).toBe(200)
})
|