Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tests): fix https agent for ClientRequest #144

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions proxy/handlers/ingress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@azure/functions'
import proxy from '../index'
import * as ingress from './ingress'
import https from 'https'
import https, { Agent } from 'https'
import { ClientRequest } from 'http'

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

const mockEndpoint = (targetUrl: string) => {
requestSpy.mockImplementationOnce((...args) => {
const [url, options] = args
expect(url.toString()).toBe(targetUrl)
options.agent = new Agent()
return Reflect.construct(ClientRequest, args)
})
}

beforeAll(() => {
jest.spyOn(ingress, 'handleIngress')
requestSpy = jest.spyOn(https, 'request')
Expand All @@ -127,10 +136,7 @@ describe('Result Endpoint', function () {

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

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

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

test('HTTP POST with suffix', async () => {
const req = mockRequestPost('https://fp.domain.com', 'fpjs/resultId/with/suffix')
requestSpy.mockImplementationOnce((url) => {
expect(url.toString()).toBe(`${origin}/with/suffix${search}`)
return Reflect.construct(ClientRequest, url)
})
mockEndpoint(`${origin}/with/suffix${search}`)
await proxy(mockContext(req), req)
expect(ingress.handleIngress).toHaveBeenCalledTimes(1)
expect(https.request).toHaveBeenCalledWith(
Expand Down