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

[INTER-624] Fix mock app tests #218

Merged
merged 9 commits into from
Apr 24, 2024
3 changes: 2 additions & 1 deletion e2e/scripts/mockTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execSync } from 'child_process'
import { readTestInfo } from '../shared/testInfo'
import pkg from '../../package.json'

async function main() {
let hasError = false
Expand All @@ -22,7 +23,7 @@ async function main() {

try {
execSync(
`npm exec -y "git+https://github.com/fingerprintjs/dx-team-mock-for-proxy-integrations-e2e-tests.git" -- --api-url="https://${apiUrl}" --host="${host}" --cdn-proxy-path="${agentPath}" --ingress-proxy-path="${resultPath}"`,
`npm exec -y "git+https://github.com/fingerprintjs/dx-team-mock-for-proxy-integrations-e2e-tests.git" -- --api-url="https://${apiUrl}" --host="${host}" --cdn-proxy-path="${agentPath}" --ingress-proxy-path="${resultPath}" --traffic-name="fingerprint-pro-azure" --integration-version=${pkg.version}`,
{
stdio: 'inherit',
}
Expand Down
21 changes: 19 additions & 2 deletions proxy/handlers/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ describe('Agent Endpoint', () => {
})
})

test('Call with custom query parameters', async () => {
const req = mockRequestGet('https://fp.domain.com', 'fpjs/agent', {
apiKey: 'ujKG34hUYKLJKJ1F',
version: '5',
customQuery: '123',
})
const ctx = mockContext(req)

await proxy(ctx, req)

const [url] = requestSpy.mock.calls[0]

expect(url.toString()).toEqual(
`https://${origin}/v5/ujKG34hUYKLJKJ1F?customQuery=123&ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn`
)
})

test('Call with version', async () => {
const req = mockRequestGet('https://fp.domain.com', 'fpjs/agent', {
apiKey: 'ujKG34hUYKLJKJ1F',
Expand Down Expand Up @@ -214,7 +231,7 @@ describe('Agent Endpoint', () => {
})
})

test('Req body and headers are the same, expect cookies, which should include only _iidt cookie', async () => {
test('Req body and headers are the same, expect cookies, which should be omitted', async () => {
const req = mockRequestGet('https://fp.domain.com', 'fpjs/agent', {
apiKey: 'ujKG34hUYKLJKJ1F',
version: '5',
Expand Down Expand Up @@ -242,7 +259,7 @@ describe('Agent Endpoint', () => {

expect(options.headers).toEqual({
...req.headers,
cookie: '_iidt=GlMQaHMfzYvomxCuA7Uymy7ArmjH04jPkT+enN7j/Xk8tJG+UYcQV+Qw60Ry4huw9bmDoO/smyjQp5vLCuSf8t4Jow==',
cookie: undefined,
})
})

Expand Down
16 changes: 15 additions & 1 deletion proxy/handlers/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpRequest, Logger } from '@azure/functions'
import { config } from '../utils/config'
import * as https from 'https'
import { filterRequestHeaders, updateResponseHeadersForAgentDownload } from '../utils/headers'
import { HttpResponseSimple } from '@azure/functions/types/http'
import { HttpRequestQuery, HttpResponseSimple } from '@azure/functions/types/http'
import { addTrafficMonitoringSearchParamsForProCDN } from '../utils/traffic'
import { IntegrationError } from '../errors/IntegrationError'

Expand All @@ -14,6 +14,16 @@ export interface DownloadAgentParams {

const DEFAULT_VERSION = '3'

function copySearchParams(query: HttpRequestQuery, newURL: URL): void {
const params = new URLSearchParams(query)

params.delete('apiKey')
params.delete('version')
params.delete('loaderVersion')

newURL.search = params.toString()
}

export async function downloadAgent({ httpRequest, logger, path }: DownloadAgentParams): Promise<HttpResponseSimple> {
const apiKey = httpRequest.query.apiKey
const version = httpRequest.query.version ?? DEFAULT_VERSION
Expand All @@ -30,13 +40,17 @@ export async function downloadAgent({ httpRequest, logger, path }: DownloadAgent
}

const url = new URL(`https://${config.fpcdn}`)
copySearchParams(httpRequest.query, url)

url.pathname = getEndpoint(apiKey, version, loaderVersion)
addTrafficMonitoringSearchParamsForProCDN(url)

logger.verbose('Downloading agent from', url.toString())

const headers = filterRequestHeaders(httpRequest.headers)

delete headers['cookie']

return new Promise<HttpResponseSimple>((resolve) => {
const data: any[] = []

Expand Down
17 changes: 17 additions & 0 deletions proxy/handlers/ingress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ describe('Result Endpoint', function () {
expect(options.headers.cookie).toBe('_iidt=7A03Gwg')
})

test('Cookies are undefined if _iidt is not est', async () => {
const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId')

req.headers.cookie = '_vid_t=gEFRuIQlzYmv692/UL4GLA=='

mockSuccessfulResponse({
checkRequestUrl: (url) => {
expect(url.toString()).toBe(`${defaultOrigin}/${search}`)
},
})

await proxy(mockContext(req), req)

const [, options] = requestSpy.mock.calls[0]
expect(options.headers.cookie).toBeUndefined()
necipallef marked this conversation as resolved.
Show resolved Hide resolved
})

test('Request body and headers are not modified, expect strict-transport-security and transfer-encoding', async () => {
const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId')
const resHeaders = {
Expand Down
9 changes: 7 additions & 2 deletions proxy/utils/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ export function filterRequestHeaders(headers: HttpRequestHeaders) {
headerValue = headerValue.split(/; */).join('; ')

headerValue = filterCookie(headerValue, (key) => key === FPJS_COOKIE_NAME)
}

result[headerName] = headerValue
// Only set cookie header if there are relevant cookies
if (headerValue) {
result[headerName] = headerValue
}
} else {
result[headerName] = headerValue
}
}

return result
Expand Down
Loading