forked from solana-foundation/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.test.ts
More file actions
105 lines (82 loc) · 3.38 KB
/
endpoint.test.ts
File metadata and controls
105 lines (82 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @jest-environment node
*/
import _dns from 'dns';
import fetch, { Headers } from 'node-fetch';
import { GET } from '../route';
const dns = _dns.promises;
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;
});
jest.mock('dns', () => {
const originalDns = jest.requireActual('dns');
const lookupFn = jest.fn();
return {
...originalDns,
promises: {
...originalDns.promises,
lookup: lookupFn,
}
};
});
async function mockFileResponseOnce(data: any, headers: Headers){
// @ts-expect-error unavailable mock method for fetch
fetch.mockResolvedValueOnce({ headers, json: async () => data });
}
const ORIGIN = 'http://explorer.solana.com';
function requestFactory(uri?: string) {
const params = new URLSearchParams({ uri: uri ?? '' });
const request = new Request(`${ORIGIN}/api/metadata/devnet?${params.toString()}`);
const nextParams = { params: { network: 'devnet' } };
return { nextParams, request };
}
describe('metadata/[network] endpoint', () => {
const validUrl = encodeURIComponent('http://external.resource/file.json');
const unsupportedUri = encodeURIComponent('ftp://unsupported.resource/file.json');
afterEach(() => {
jest.clearAllMocks();
});
it('should return status when disabled', async () => {
setEnvironment('NEXT_PUBLIC_METADATA_ENABLED', 'false');
const { request, nextParams } = requestFactory();
const response = await GET(request, nextParams);
expect(response.status).toBe(404);
});
it('should return 400 for URIs with unsupported protocols', async () => {
setEnvironment('NEXT_PUBLIC_METADATA_ENABLED', 'true');
const request = requestFactory(unsupportedUri);
const response = await GET(request.request, request.nextParams);
expect(response.status).toBe(400);
});
it('should return proper status upon processing data', async () => {
setEnvironment('NEXT_PUBLIC_METADATA_ENABLED', 'true');
const { request, nextParams } = requestFactory();
const response = await GET(request, nextParams);
expect(response.status).toBe(400);
// fail on encoded incorrectly input
const request2 = requestFactory('https://example.com/%E0%A4%A');
expect((await GET(request2.request, request2.nextParams)).status).toBe(400);
// fail due to unexpected error
const request3 = requestFactory(validUrl);
const result = await GET(request3.request, request3.nextParams);
expect(result.status).toBe(403);
});
it('should handle valid response successfully', async () => {
await mockFileResponseOnce({ attributes: [], name: "NFT" }, new Headers({
'Cache-Control': 'no-cache',
'Content-Length': '140',
'Content-Type': 'application/json',
'Etag': 'random-etag',
}));
// @ts-expect-error lookup does not have mocked fn
dns.lookup.mockResolvedValueOnce([{ address: '8.8.8.8' }]);
const request = requestFactory(validUrl);
expect((await GET(request.request, request.nextParams)).status).toBe(200);
});
});