-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapi.interceptor.spec.ts
More file actions
102 lines (85 loc) · 3.73 KB
/
Copy pathapi.interceptor.spec.ts
File metadata and controls
102 lines (85 loc) · 3.73 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
/// <reference types="jest" />
import { addSignatureFn } from '../lib/api/helper/interceptors';
import ConfigStore, { CONFIG_KEYS } from '../lib/Config';
const packageJSON = require('../../package.json');
describe('API request interceptor', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('adds fdk cli version header before service requests are signed', async () => {
const interceptor = addSignatureFn({});
const config: any = {
url: 'https://api.fyndx1.de/service/panel/authentication/v1.0/oauth/token',
method: 'get',
headers: {},
};
const signedConfig = await interceptor(config);
expect(signedConfig.headers['x-fp-cli']).toBe(`${packageJSON.version}`);
expect(signedConfig.headers['x-fp-date']).toBeDefined();
expect(signedConfig.headers['x-fp-signature']).toBeDefined();
});
it('adds stored region header before fynd service requests are signed', async () => {
jest.spyOn(ConfigStore, 'get').mockImplementation((key: string) => {
if (key === CONFIG_KEYS.REGION) return 'asia-south1';
return undefined;
});
const interceptor = addSignatureFn({});
const config: any = {
url: 'https://api.fyndx1.de/service/panel/authentication/v1.0/oauth/token',
method: 'get',
headers: {},
};
const signedConfig = await interceptor(config);
expect(signedConfig.headers['x-region']).toBe('asia-south1');
expect(signedConfig.headers['x-fp-signature']).toBeDefined();
});
it('prefers request region header over stored region header', async () => {
jest.spyOn(ConfigStore, 'get').mockImplementation((key: string) => {
if (key === CONFIG_KEYS.REGION) return 'chinmay';
return undefined;
});
const interceptor = addSignatureFn({});
const config: any = {
url: 'https://api.fyndx1.de/service/panel/authentication/v1.0/oauth/token',
method: 'get',
headers: {
'x-region': 'asia-south2',
},
};
const signedConfig = await interceptor(config);
expect(signedConfig.headers['x-region']).toBe('asia-south2');
expect(signedConfig.headers['x-fp-signature']).toBeDefined();
});
it('does not add stored region header when request opts out of region', async () => {
jest.spyOn(ConfigStore, 'get').mockImplementation((key: string) => {
if (key === CONFIG_KEYS.REGION) return 'chinmay';
return undefined;
});
const interceptor = addSignatureFn({});
const config: any = {
url: 'https://api.fyndx1.de/service/panel/authentication/v1.0/oauth/token',
method: 'get',
headers: {
'x-region': null,
},
};
const signedConfig = await interceptor(config);
expect(signedConfig.headers['x-region']).toBeUndefined();
expect(signedConfig.headers['x-fp-signature']).toBeDefined();
});
it('does not add stored region header to third-party requests', async () => {
jest.spyOn(ConfigStore, 'get').mockImplementation((key: string) => {
if (key === CONFIG_KEYS.REGION) return 'asia-south1';
return undefined;
});
const interceptor = addSignatureFn({});
const config: any = {
url: 'https://storage.googleapis.com/fdk-upload/signed-url',
method: 'put',
headers: {},
};
const signedConfig = await interceptor(config);
expect(signedConfig.headers['x-region']).toBeUndefined();
expect(signedConfig.headers['x-fp-signature']).toBeUndefined();
});
});