-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathparseBalancer.test.ts
166 lines (136 loc) · 6.26 KB
/
parseBalancer.test.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
getCleanBalancerValue,
parseBalancer,
prepareBackendFromBalancer,
removePort,
removeProtocol,
removeViewerPathname,
} from '../parseBalancer';
describe('parseBalancer', () => {
test('should parse balancer with viewer proxy', () => {
const rawValue =
'https://viewer.ydb.ru:443/ydb-testing-0000.search.ydb.net:8765/viewer/json';
const balancer = 'ydb-testing-0000.search.ydb.net:8765';
const proxy = 'viewer.ydb.ru:443';
const parsedBalancerWithViewer = parseBalancer(rawValue);
expect(parsedBalancerWithViewer.balancer).toBe(balancer);
expect(parsedBalancerWithViewer.proxy).toBe(proxy);
});
test('should parse balancer with bastion proxy', () => {
const rawValue = 'https://ydb.bastion.cloud.ru:443/ydbproxy.ydb.cloud.net:8765/viewer/json';
const balancer = 'ydbproxy.ydb.cloud.net:8765';
const proxy = 'ydb.bastion.cloud.ru:443';
const parsedBalancerWithBastion = parseBalancer(rawValue);
expect(parsedBalancerWithBastion.balancer).toBe(balancer);
expect(parsedBalancerWithBastion.proxy).toBe(proxy);
});
test('should parse balancer with custom proxy', () => {
const rawValue =
'https://proxy.ydb.mdb.cloud-preprod.net:443/ydbproxy-public.ydb.cloud-preprod.net:8765/viewer/json';
const balancer = 'ydbproxy-public.ydb.cloud-preprod.net:8765';
const proxy = 'proxy.ydb.mdb.cloud-preprod.net:443';
const parsedBalancerWithCustomProxy = parseBalancer(rawValue);
expect(parsedBalancerWithCustomProxy.balancer).toBe(balancer);
expect(parsedBalancerWithCustomProxy.proxy).toBe(proxy);
});
test('should parse balancer without proxy', () => {
const rawValue = 'https://ydb-testing-0000.search.net:8765/viewer/json';
const balancer = 'ydb-testing-0000.search.net:8765';
const parsedBalancerWithoutProxy = parseBalancer(rawValue);
expect(parsedBalancerWithoutProxy.balancer).toBe(balancer);
expect(parsedBalancerWithoutProxy.proxy).toBe(undefined);
});
test('should parse pure balancer', () => {
const pureBalancer = 'ydb-testing-0000.search.net:8765';
const parsedPureBalancer = parseBalancer(pureBalancer);
expect(parsedPureBalancer.balancer).toBe(pureBalancer);
expect(parsedPureBalancer.proxy).toBe(undefined);
});
});
describe('removeViewerPathname', () => {
test('should remove pathname', () => {
const initialValue = 'https://ydb-testing-0000.search.net:8765/viewer/json';
const result = 'https://ydb-testing-0000.search.net:8765';
expect(removeViewerPathname(initialValue)).toBe(result);
});
});
describe('removeProtocol', () => {
test('should remove protocol from start', () => {
const initialValue = 'https://ydb-testing-0000.search.net:8765/viewer/json';
const result = 'ydb-testing-0000.search.net:8765/viewer/json';
expect(removeProtocol(initialValue)).toBe(result);
});
test('should not remove protocol string in the middle', () => {
const initialValue = 'proxy/host/https:ydb-testing-0000.search.net';
expect(removeProtocol(initialValue)).toBe(initialValue);
});
});
describe('removePort', () => {
test('should remove port', () => {
const initialValue = 'ydb-testing-0000.search.net:8765';
const result = 'ydb-testing-0000.search.net';
expect(removePort(initialValue)).toBe(result);
});
});
describe('getCleanBalancerValue', () => {
test('should return balancer value without protocol, proxy, port and pathname', () => {
const initialValue =
'https://ydb.bastion.cloud.ru:443/ydbproxy.ydb.cloud.net:8765/viewer/json';
const result = 'ydbproxy.ydb.cloud.net';
expect(getCleanBalancerValue(initialValue)).toBe(result);
});
});
describe('prepareBackendFromBalancer', () => {
const windowSpy = jest.spyOn(window, 'window', 'get');
afterEach(() => {
windowSpy.mockClear();
});
afterAll(() => {
windowSpy.mockRestore();
});
test('should not change full balancer value - only remove viewer pathname', () => {
const initialValue = 'https://ydb-testing-0000.search.net:8765/viewer/json';
const result = 'https://ydb-testing-0000.search.net:8765';
expect(prepareBackendFromBalancer(initialValue)).toBe(result);
});
test('should add meta backend for relative balancer value', () => {
const initialValue = '/proxy/host/ydb-testing-0000.search.net/viewer/json';
const result = 'https://my-host.ru/proxy/host/ydb-testing-0000.search.net';
windowSpy.mockImplementation(() => {
return {
meta_backend: 'https://my-host.ru',
} as Window & typeof globalThis;
});
expect(prepareBackendFromBalancer(initialValue)).toBe(result);
});
test('should add relative meta backend for relative balancer value', () => {
const initialValue = '/proxy/host/ydb-testing-0000.search.net/viewer/json';
const result = '/meta/proxy/host/ydb-testing-0000.search.net';
windowSpy.mockImplementation(() => {
return {
meta_backend: '/meta',
} as Window & typeof globalThis;
});
expect(prepareBackendFromBalancer(initialValue)).toBe(result);
});
test('should not add empty meta backend for relative balancer value', () => {
const initialValue = '/proxy/host/ydb-testing-0000.search.net/viewer/json';
const result = '/proxy/host/ydb-testing-0000.search.net';
windowSpy.mockImplementation(() => {
return {
meta_backend: '',
} as Window & typeof globalThis;
});
expect(prepareBackendFromBalancer(initialValue)).toBe(result);
});
test('should not add undefined meta backend for relative balancer value', () => {
const initialValue = '/proxy/host/ydb-testing-0000.search.net/viewer/json';
const result = '/proxy/host/ydb-testing-0000.search.net';
windowSpy.mockImplementation(() => {
return {
meta_backend: undefined,
} as Window & typeof globalThis;
});
expect(prepareBackendFromBalancer(initialValue)).toBe(result);
});
});