diff options
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 }) + }) +}) |