Skip to content

Commit 316c2ee

Browse files
Guido D'Orsimcollinaronag
authored andcommitted
fix(fetch): integration with MockAgent (nodejs#1149)
* fix(fetch): integration with MockAgent * fix(fetch): integration with MockAgent * chore: cleanup * chore: cleaned up the tests * fix: more tests and headers support * fix: corrects a syntax error in node 12 * Update lib/mock/mock-utils.js Co-authored-by: Robert Nagy <[email protected]> Co-authored-by: Matteo Collina <[email protected]> Co-authored-by: Robert Nagy <[email protected]>
1 parent 6eeb3e7 commit 316c2ee

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

lib/fetch/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const { kHeadersList } = require('../core/symbols')
4646
const EE = require('events')
4747
const { PassThrough, pipeline } = require('stream')
4848
const { isErrored, isReadable } = require('../core/util')
49+
const { kIsMockActive } = require('../mock/mock-symbols')
4950

5051
let ReadableStream
5152

@@ -1621,7 +1622,7 @@ function httpNetworkFetch (
16211622
path: url.pathname + url.search,
16221623
origin: url.origin,
16231624
method: request.method,
1624-
body,
1625+
body: context.dispatcher[kIsMockActive] ? request.body && request.body.source : body,
16251626
headers: request.headersList,
16261627
maxRedirections: 0
16271628
},

lib/mock/mock-utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const { MockNotMatchedError } = require('./mock-errors')
4+
const { kHeadersList } = require('../core/symbols')
45
const {
56
kDispatches,
67
kMockAgent,
@@ -30,8 +31,11 @@ function matchHeaders (mockDispatch, headers) {
3031
if (typeof headers !== 'object' || typeof mockDispatch.headers !== 'object') {
3132
return false
3233
}
34+
3335
for (const [matchHeaderName, matchHeaderValue] of Object.entries(mockDispatch.headers)) {
34-
if (!matchValue(matchHeaderValue, headers[matchHeaderName])) {
36+
const header = typeof headers.get === 'function' ? headers.get(matchHeaderName) : headers[matchHeaderName]
37+
38+
if (!matchValue(matchHeaderValue, header)) {
3539
return false
3640
}
3741
}

test/node-fetch/mock.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* eslint no-unused-expressions: "off" */
2+
3+
// Test tools
4+
const chai = require('chai')
5+
6+
const {
7+
fetch,
8+
MockAgent,
9+
setGlobalDispatcher,
10+
Headers
11+
} = require('../../index.js')
12+
13+
const { expect } = chai
14+
15+
describe('node-fetch with MockAgent', () => {
16+
it('should match the url', async () => {
17+
const mockAgent = new MockAgent()
18+
setGlobalDispatcher(mockAgent)
19+
const mockPool = mockAgent.get('http://localhost:3000')
20+
21+
mockPool
22+
.intercept({
23+
path: '/test',
24+
method: 'GET',
25+
})
26+
.reply(200, { success: true })
27+
.persist()
28+
29+
const res = await fetch('http://localhost:3000/test', {
30+
method: 'GET'
31+
})
32+
33+
expect(res.status).to.equal(200)
34+
expect(await res.json()).to.deep.equal({ success: true })
35+
})
36+
37+
it('should match the body', async () => {
38+
const mockAgent = new MockAgent()
39+
setGlobalDispatcher(mockAgent)
40+
const mockPool = mockAgent.get('http://localhost:3000')
41+
42+
mockPool
43+
.intercept({
44+
path: '/test',
45+
method: 'POST',
46+
body: (value) => {
47+
return value === 'request body'
48+
}
49+
})
50+
.reply(200, { success: true })
51+
.persist()
52+
53+
const res = await fetch('http://localhost:3000/test', {
54+
method: 'POST',
55+
body: 'request body'
56+
})
57+
58+
expect(res.status).to.equal(200)
59+
expect(await res.json()).to.deep.equal({ success: true })
60+
})
61+
62+
it('should match the headers', async () => {
63+
const mockAgent = new MockAgent()
64+
setGlobalDispatcher(mockAgent)
65+
const mockPool = mockAgent.get('http://localhost:3000')
66+
67+
mockPool
68+
.intercept({
69+
path: '/test',
70+
method: 'GET',
71+
headers: {
72+
'User-Agent': /^undici$/,
73+
}
74+
})
75+
.reply(200, { success: true })
76+
.persist()
77+
78+
const res = await fetch('http://localhost:3000/test', {
79+
method: 'GET',
80+
headers: new Headers({ 'User-Agent': 'undici' })
81+
})
82+
83+
expect(res.status).to.equal(200)
84+
expect(await res.json()).to.deep.equal({ success: true })
85+
})
86+
})

0 commit comments

Comments
 (0)