summaryrefslogtreecommitdiffstats
path: root/test/jest/mock-agent.test.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
commit0b6210cd37b68b94252cb798598b12974a20e1c1 (patch)
treee371686554a877842d95aa94f100bee552ff2a8e /test/jest/mock-agent.test.js
parentInitial commit. (diff)
downloadnode-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.tar.xz
node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.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/jest/mock-agent.test.js')
-rw-r--r--test/jest/mock-agent.test.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/jest/mock-agent.test.js b/test/jest/mock-agent.test.js
new file mode 100644
index 0000000..6f6bac2
--- /dev/null
+++ b/test/jest/mock-agent.test.js
@@ -0,0 +1,46 @@
+'use strict'
+
+const { request, setGlobalDispatcher, MockAgent } = require('../..')
+const { getResponse } = require('../../lib/mock/mock-utils')
+
+/* global describe, it, expect */
+
+describe('MockAgent', () => {
+ let mockAgent
+
+ afterEach(() => {
+ mockAgent.close()
+ })
+
+ it('should work in jest', async () => {
+ expect.assertions(4)
+
+ const baseUrl = 'http://localhost:9999'
+
+ mockAgent = new MockAgent()
+ setGlobalDispatcher(mockAgent)
+ const mockClient = mockAgent.get(baseUrl)
+
+ mockClient.intercept({
+ path: '/foo?hello=there&see=ya',
+ method: 'POST',
+ body: 'form1=data1&form2=data2'
+ }).reply(200, { foo: 'bar' }, {
+ headers: {
+ 'content-type': 'application/json'
+ },
+ trailers: { 'Content-MD5': 'test' }
+ })
+
+ const { statusCode, headers, trailers, body } = await request(`${baseUrl}/foo?hello=there&see=ya`, {
+ method: 'POST',
+ body: 'form1=data1&form2=data2'
+ })
+ expect(statusCode).toBe(200)
+ expect(headers).toEqual({ 'content-type': 'application/json' })
+ expect(trailers).toEqual({ 'content-md5': 'test' })
+
+ const jsonResponse = JSON.parse(await getResponse(body))
+ expect(jsonResponse).toEqual({ foo: 'bar' })
+ })
+})