Skip to content

Commit 20089d8

Browse files
chore(tests): fix https agent for ClientRequest
1 parent ebfe908 commit 20089d8

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

proxy/handlers/ingress.test.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@azure/functions'
1212
import proxy from '../index'
1313
import * as ingress from './ingress'
14-
import https from 'https'
14+
import https, { Agent } from 'https'
1515
import { ClientRequest } from 'http'
1616

1717
const fp: FormPart = {
@@ -112,6 +112,15 @@ describe('Result Endpoint', function () {
112112
const origin: string = 'https://__ingress_api__'
113113
const search: string = '?ii=fingerprint-pro-azure%2F__azure_function_version__%2Fingress'
114114

115+
const mockEndpoint = (targetUrl: string) => {
116+
requestSpy.mockImplementationOnce((...args) => {
117+
const [url, options] = args
118+
expect(url.toString()).toBe(targetUrl)
119+
options.agent = new Agent()
120+
return Reflect.construct(ClientRequest, args)
121+
})
122+
}
123+
115124
beforeAll(() => {
116125
jest.spyOn(ingress, 'handleIngress')
117126
requestSpy = jest.spyOn(https, 'request')
@@ -127,10 +136,7 @@ describe('Result Endpoint', function () {
127136

128137
test('HTTP GET without suffix', async () => {
129138
const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId')
130-
requestSpy.mockImplementationOnce((url) => {
131-
expect(url.toString()).toBe(`${origin}/${search}`)
132-
return Reflect.construct(ClientRequest, url)
133-
})
139+
mockEndpoint(`${origin}/${search}`)
134140
await proxy(mockContext(req), req)
135141
expect(ingress.handleIngress).toHaveBeenCalledTimes(1)
136142
expect(https.request).toHaveBeenCalledWith(
@@ -147,10 +153,7 @@ describe('Result Endpoint', function () {
147153

148154
test('HTTP GET with suffix', async () => {
149155
const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId/with/suffix')
150-
requestSpy.mockImplementationOnce((url) => {
151-
expect(url.toString()).toBe(`${origin}/with/suffix${search}`)
152-
return Reflect.construct(ClientRequest, url)
153-
})
156+
mockEndpoint(`${origin}/with/suffix${search}`)
154157
await proxy(mockContext(req), req)
155158
expect(ingress.handleIngress).toHaveBeenCalledTimes(1)
156159
expect(https.request).toHaveBeenCalledWith(
@@ -174,10 +177,7 @@ describe('Result Endpoint', function () {
174177

175178
test('HTTP POST without suffix', async () => {
176179
const req = mockRequestPost('https://fp.domain.com', 'fpjs/resultId')
177-
requestSpy.mockImplementationOnce((url) => {
178-
expect(url.toString()).toBe(`${origin}/${search}`)
179-
return Reflect.construct(ClientRequest, url)
180-
})
180+
mockEndpoint(`${origin}/${search}`)
181181
await proxy(mockContext(req), req)
182182
expect(ingress.handleIngress).toHaveBeenCalledTimes(1)
183183
expect(https.request).toHaveBeenCalledWith(
@@ -194,10 +194,7 @@ describe('Result Endpoint', function () {
194194

195195
test('HTTP POST with suffix', async () => {
196196
const req = mockRequestPost('https://fp.domain.com', 'fpjs/resultId/with/suffix')
197-
requestSpy.mockImplementationOnce((url) => {
198-
expect(url.toString()).toBe(`${origin}/with/suffix${search}`)
199-
return Reflect.construct(ClientRequest, url)
200-
})
197+
mockEndpoint(`${origin}/with/suffix${search}`)
201198
await proxy(mockContext(req), req)
202199
expect(ingress.handleIngress).toHaveBeenCalledTimes(1)
203200
expect(https.request).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)