-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathurls.test.ts
More file actions
107 lines (81 loc) · 3.45 KB
/
urls.test.ts
File metadata and controls
107 lines (81 loc) · 3.45 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
106
107
import address from 'address';
import * as urls from '../urls';
jest.mock('address');
describe('urls', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
});
describe('getDefaultPort', () => {
it('should return default if no port is provided', () => {
expect(urls.getDefaultPort()).toEqual(urls.DEFAULT_PORT);
});
it('should return default if port is an empty string', () => {
expect(urls.getDefaultPort('')).toEqual(urls.DEFAULT_PORT);
});
it('should return default if port is not a number string', () => {
expect(urls.getDefaultPort('abc')).toEqual(urls.DEFAULT_PORT);
});
it('should return port passed', () => {
expect(urls.getDefaultPort('123')).toEqual(123);
});
it('should return int from float port passed', () => {
expect(urls.getDefaultPort('123.456')).toEqual(123);
});
});
describe('getLocalAndNetworkUrls', () => {
const OLD_ENV = process.env;
beforeEach(() => {
process.env = { ...OLD_ENV };
delete process.env.DOMAIN; // Ensure clean state
});
it('should return http with localhost (default)', () => {
const ip = jest.spyOn(address, 'ip').mockReturnValue('192.0.0.0');
const result = urls.getLocalAndNetworkUrls(1234);
expect(result.local.host).toEqual('0.0.0.0');
expect(result.local.port).toEqual(1234);
expect(result.local.url).toEqual('http://localhost:1234/');
expect(result.network.host).toEqual('192.0.0.0');
expect(result.network.port).toEqual(1234);
expect(result.network.url).toEqual('http://192.0.0.0:1234/');
ip.mockRestore();
});
it('should return http with custom domain', () => {
process.env.DOMAIN = 'flex.local.com';
const ip = jest.spyOn(address, 'ip').mockReturnValue('192.0.0.0');
const result = urls.getLocalAndNetworkUrls(1234);
expect(result.local.host).toEqual('0.0.0.0');
expect(result.local.port).toEqual(1234);
expect(result.local.url).toEqual('http://flex.local.com:1234/');
expect(result.network.host).toEqual('192.0.0.0');
expect(result.network.port).toEqual(1234);
expect(result.network.url).toEqual('http://192.0.0.0:1234/');
ip.mockRestore();
});
it('should return https with localhost (default)', () => {
process.env.HTTPS = 'true';
const ip = jest.spyOn(address, 'ip').mockReturnValue('192.0.0.0');
const result = urls.getLocalAndNetworkUrls(1234);
expect(result.local.host).toEqual('0.0.0.0');
expect(result.local.port).toEqual(1234);
expect(result.local.url).toEqual('https://localhost:1234/');
expect(result.network.host).toEqual('192.0.0.0');
expect(result.network.port).toEqual(1234);
expect(result.network.url).toEqual('https://192.0.0.0:1234/');
ip.mockRestore();
});
it('should return https with custom domain', () => {
process.env.HTTPS = 'true';
process.env.DOMAIN = 'secure.local.dev';
const ip = jest.spyOn(address, 'ip').mockReturnValue('192.0.0.0');
const result = urls.getLocalAndNetworkUrls(1234);
expect(result.local.host).toEqual('0.0.0.0');
expect(result.local.port).toEqual(1234);
expect(result.local.url).toEqual('https://secure.local.dev:1234/');
expect(result.network.host).toEqual('192.0.0.0');
expect(result.network.port).toEqual(1234);
expect(result.network.url).toEqual('https://192.0.0.0:1234/');
ip.mockRestore();
});
});
});