Skip to content
Closed
Show file tree
Hide file tree
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
33 changes: 19 additions & 14 deletions app/api/metadata/proxy/__tests__/endpoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* @jest-environment node
* @vi-environment node
*/
import _dns from 'dns';
import fetch, { Headers } from 'node-fetch';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { GET } from '../route';

Expand All @@ -12,28 +13,32 @@ function setEnvironment(key: string, value: string) {
Object.assign(process.env, { ...process.env, [key]: value });
}

jest.mock('node-fetch', () => {
const originalFetch = jest.requireActual('node-fetch');
const mockFn = jest.fn();

Object.assign(mockFn, originalFetch);

return mockFn;
vi.mock('node-fetch', async () => {
const actual = await vi.importActual('node-fetch');
return {
...actual,
default: vi.fn()
};
});

jest.mock('dns', () => {
const originalDns = jest.requireActual('dns');
const lookupFn = jest.fn();

vi.mock('dns', async () => {
const originalDns = await vi.importActual('dns');
const lookupFn = vi.fn();
return {
...originalDns,
default: {
promises: {
lookup: lookupFn,
}
},
promises: {
...originalDns.promises,
lookup: lookupFn,
}
};
});

async function mockFileResponseOnce(data: any, headers: Headers){
async function mockFileResponseOnce(data: any, headers: Headers) {
// @ts-expect-error unavailable mock method for fetch
fetch.mockResolvedValueOnce({ headers, json: async () => data });
}
Expand All @@ -53,7 +58,7 @@ describe('metadata/[network] endpoint', () => {
const unsupportedUri = encodeURIComponent('ftp://unsupported.resource/file.json');

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should return status when disabled', async () => {
Expand Down
28 changes: 14 additions & 14 deletions app/api/metadata/proxy/__tests__/fetch-resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* @jest-environment node
*/
import fetch, { Headers } from 'node-fetch';
import { vi } from 'vitest';

import { fetchResource } from '../feature';

jest.mock('node-fetch', () => {
const originalFetch = jest.requireActual('node-fetch');
const mockFn = jest.fn();

Object.assign(mockFn, originalFetch);

return mockFn;
vi.mock('node-fetch', async () => {
const actual = await vi.importActual('node-fetch');
return {
...actual,
default: vi.fn()
};
});

/**
Expand All @@ -34,11 +34,11 @@ function mockRejectOnce<T extends Error>(error: T) {
}

describe('fetchResource', () => {
const uri = 'http://hello.world/data.json' ;
const uri = 'http://hello.world/data.json';
const headers = new Headers({ 'Content-Type': 'application/json' });

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should be called with proper arguments', async () => {
Expand All @@ -53,15 +53,15 @@ describe('fetchResource', () => {
it('should throw exception for unsupported media', async () => {
mockFetchOnce();

expect(() => {
await expect(() => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When testing promise rejections, consider passing the promise directly to expect(...).rejects rather than wrapping it in a function. This improves clarity and conforms with typical async testing patterns.

Suggested change
await expect(() => {
await expect(fetchResource(uri, headers, 100, 100)).rejects.toThrowError('Unsupported Media Type');

return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Unsupported Media Type');
});

it('should throw exception upon exceeded size', async () => {
mockRejectOnce(new Error('FetchError: content size at https://path/to/resour.ce over limit: 100'));

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Max Content Size Exceeded');
});
Expand All @@ -75,15 +75,15 @@ describe('fetchResource', () => {
}
mockRejectOnce(new TimeoutError());

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Gateway Timeout');
});

it('should handle size overflow', async () => {
mockRejectOnce(new Error('file is over limit: 100'));

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Max Content Size Exceeded');
});
Expand All @@ -98,7 +98,7 @@ describe('fetchResource', () => {

try {
await fn();
} catch(e: any) {
} catch (e: any) {
expect(e.message).toEqual('General Error');
expect(e.status).toEqual(500);
}
Expand Down
26 changes: 14 additions & 12 deletions app/api/metadata/proxy/__tests__/ip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* @jest-environment node
*/
import _dns from 'dns';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import { checkURLForPrivateIP } from '../feature/ip';

const dns = _dns.promises;

jest.mock('dns', () => {
const originalDns = jest.requireActual('dns');
const lookupFn = jest.fn();
vi.mock('dns', async () => {
const originalDns = await vi.importActual('dns');
const lookupFn = vi.fn();
return {
...originalDns,
default: {
promises: {
lookup: lookupFn,
}
},
promises: {
...originalDns.promises,
lookup: lookupFn,
}
};
Expand All @@ -26,13 +28,13 @@ jest.mock('dns', () => {
type LookupAddress = { address: string };

function mockLookupOnce(addresses: LookupAddress | LookupAddress[] | undefined) {
// @ts-expect-error lookup does not have mocked fn
// @ts-expect-error lookup does not have mockImplementation
dns.lookup.mockResolvedValueOnce(addresses);
}

describe('ip::checkURLForPrivateIP', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

// do not throw exceptions forinvalid input to not break the execution flow
Expand Down Expand Up @@ -80,15 +82,15 @@ describe('ip::checkURLForPrivateIP', () => {
});

test('should handle DNS resolution failure gracefully', async () => {
// @ts-expect-error fetch does not have mocked fn
// @ts-expect-error lookup does not have mockImplementation
dns.lookup.mockRejectedValueOnce(new Error('DNS resolution failed'));
await expect(checkURLForPrivateIP('http://unknown.domain')).resolves.toBe(true);
});
});

describe('ip::checkURLForPrivateIP with single resolved address', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should handle single address positively', async () => {
Expand All @@ -100,7 +102,7 @@ describe('ip::checkURLForPrivateIP with single resolved address', () => {
// move case for localhost to a separate test case as it's a special case and doesn't require DNS resolution
describe('ip::checkURLForPrivateIP with localhost', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should block localhost', async () => {
Expand Down
Loading