forked from Mathieu2301/TradingView-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetUser-redirect.test.ts
More file actions
49 lines (44 loc) · 1.49 KB
/
getUser-redirect.test.ts
File metadata and controls
49 lines (44 loc) · 1.49 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
import { describe, it, expect } from 'vitest';
describe('getUser redirect protection', () => {
it('should not loop infinitely on repeated redirects', async () => {
// Monkey-patch axios in the require cache to simulate redirect loop
let callCount = 0;
const axiosMock = {
get: async (url: string) => {
callCount += 1;
const isA = url === 'https://www.tradingview.com/';
return {
data: '<html>no auth here</html>',
headers: {
location: isA
? 'https://www.tradingview.com/accounts/signin/'
: 'https://www.tradingview.com/',
},
};
},
};
const axiosPath = require.resolve('axios');
const originalModule = require.cache[axiosPath];
require.cache[axiosPath] = {
id: axiosPath,
filename: axiosPath,
loaded: true,
exports: axiosMock,
} as any;
const miscPath = require.resolve('../src/miscRequests');
delete require.cache[miscPath];
try {
// eslint-disable-next-line global-require
const misc = require('../src/miscRequests');
await expect(
misc.getUser('fake_session', 'fake_signature'),
).rejects.toThrow('Too many redirects');
expect(callCount).toBeGreaterThan(0);
expect(callCount).toBeLessThanOrEqual(10);
} finally {
if (originalModule) require.cache[axiosPath] = originalModule;
else delete require.cache[axiosPath];
delete require.cache[miscPath];
}
}, 5000);
});