diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:56:19 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:56:19 +0000 |
commit | 0b6210cd37b68b94252cb798598b12974a20e1c1 (patch) | |
tree | e371686554a877842d95aa94f100bee552ff2a8e /test/node-fetch/mock.js | |
parent | Initial commit. (diff) | |
download | node-undici-upstream.tar.xz node-undici-upstream.zip |
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/node-fetch/mock.js')
-rw-r--r-- | test/node-fetch/mock.js | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/test/node-fetch/mock.js b/test/node-fetch/mock.js new file mode 100644 index 0000000..a53f464 --- /dev/null +++ b/test/node-fetch/mock.js @@ -0,0 +1,112 @@ +/* eslint no-unused-expressions: "off" */ + +// Test tools +const chai = require('chai') + +const { + fetch, + MockAgent, + setGlobalDispatcher, + Headers +} = require('../../index.js') + +const { expect } = chai + +describe('node-fetch with MockAgent', () => { + it('should match the url', async () => { + const mockAgent = new MockAgent() + setGlobalDispatcher(mockAgent) + const mockPool = mockAgent.get('http://localhost:3000') + + mockPool + .intercept({ + path: '/test', + method: 'GET' + }) + .reply(200, { success: true }) + .persist() + + const res = await fetch('http://localhost:3000/test', { + method: 'GET' + }) + + expect(res.status).to.equal(200) + expect(await res.json()).to.deep.equal({ success: true }) + }) + + it('should match the body', async () => { + const mockAgent = new MockAgent() + setGlobalDispatcher(mockAgent) + const mockPool = mockAgent.get('http://localhost:3000') + + mockPool + .intercept({ + path: '/test', + method: 'POST', + body: (value) => { + return value === 'request body' + } + }) + .reply(200, { success: true }) + .persist() + + const res = await fetch('http://localhost:3000/test', { + method: 'POST', + body: 'request body' + }) + + expect(res.status).to.equal(200) + expect(await res.json()).to.deep.equal({ success: true }) + }) + + it('should match the headers', async () => { + const mockAgent = new MockAgent() + setGlobalDispatcher(mockAgent) + const mockPool = mockAgent.get('http://localhost:3000') + + mockPool + .intercept({ + path: '/test', + method: 'GET', + headers: (h) => { + return h['user-agent'] === 'undici' + } + }) + .reply(200, { success: true }) + .persist() + + const res = await fetch('http://localhost:3000/test', { + method: 'GET', + headers: new Headers({ 'User-Agent': 'undici' }) + }) + expect(res.status).to.equal(200) + expect(await res.json()).to.deep.equal({ success: true }) + }) + + it('should match the headers with a matching function', async () => { + const mockAgent = new MockAgent() + setGlobalDispatcher(mockAgent) + const mockPool = mockAgent.get('http://localhost:3000') + + mockPool + .intercept({ + path: '/test', + method: 'GET', + headers (headers) { + expect(headers).to.be.an('object') + expect(headers).to.have.property('user-agent', 'undici') + return true + } + }) + .reply(200, { success: true }) + .persist() + + const res = await fetch('http://localhost:3000/test', { + method: 'GET', + headers: new Headers({ 'User-Agent': 'undici' }) + }) + + expect(res.status).to.equal(200) + expect(await res.json()).to.deep.equal({ success: true }) + }) +}) |