|
2 | 2 |
|
3 | 3 | describe('createSocketUrl', () => {
|
4 | 4 | const samples = [
|
5 |
| - '?test', |
6 |
| - 'https://example.com', |
7 |
| - 'https://example.com/path', |
8 |
| - 'https://example.com/path/foo.js', |
9 |
| - 'http://user:password@localhost/', |
10 |
| - 'http://0.0.0.0', |
11 |
| - 'https://localhost:123', |
12 |
| - 'http://user:pass@[::]:8080', |
13 |
| - 'http://127.0.0.1', |
14 |
| - 'file://filename', |
15 |
| - // eslint-disable-next-line no-undefined |
16 |
| - undefined, |
17 |
| - ]; |
18 |
| - |
19 |
| - samples.forEach((url) => { |
20 |
| - jest.doMock('../../../client-src/utils/getCurrentScriptSource', () => () => |
21 |
| - url |
22 |
| - ); |
23 |
| - |
24 |
| - const createSocketUrl = require('../../../client-src/utils/createSocketUrl'); |
25 |
| - |
26 |
| - test(`should return the url when __resourceQuery is ${url}`, () => { |
27 |
| - const query = url ? url.querystring : url; |
28 |
| - expect(createSocketUrl(query)).toMatchSnapshot(); |
29 |
| - }); |
30 |
| - |
31 |
| - test(`should return the url when the current script source is ${url}`, () => { |
32 |
| - expect(createSocketUrl()).toMatchSnapshot(); |
33 |
| - }); |
34 |
| - |
35 |
| - // put here because resetModules mustn't be reset when L20 is finished |
36 |
| - jest.resetModules(); |
37 |
| - }); |
38 |
| - |
39 |
| - const samples2 = [ |
40 |
| - // script source, location, output socket URL |
41 |
| - ['http://example.com', 'https://something.com', 'https://example.com/ws'], |
42 |
| - ['http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'], |
43 |
| - ['http://0.0.0.0', 'https://something.com', 'https://something.com/ws'], |
44 |
| - ['http://0.0.0.0', 'http://something.com', 'http://something.com/ws'], |
45 |
| - ['http://[::]', 'https://something.com', 'https://something.com/ws'], |
46 |
| - ['http://example.com', 'http://something.com', 'http://example.com/ws'], |
47 |
| - ['https://example.com', 'http://something.com', 'https://example.com/ws'], |
48 |
| - ]; |
49 |
| - |
50 |
| - samples2.forEach(([scriptSrc, loc, expected]) => { |
51 |
| - jest.doMock( |
52 |
| - '../../../client-src/utils/getCurrentScriptSource.js', |
53 |
| - () => () => scriptSrc |
54 |
| - ); |
55 |
| - |
56 |
| - const createSocketUrl = require('../../../client-src/utils/createSocketUrl'); |
57 |
| - |
58 |
| - test(`should return socket ${expected} for script source ${scriptSrc} and location ${loc}`, () => { |
59 |
| - // eslint-disable-next-line no-undefined |
60 |
| - expect(createSocketUrl(undefined, loc).toString()).toEqual(expected); |
61 |
| - }); |
62 |
| - |
63 |
| - jest.resetModules(); |
64 |
| - }); |
65 |
| - |
66 |
| - const samples3 = [ |
67 |
| - // script source, location, output socket URL |
68 |
| - ['?http://example.com', 'https://something.com', 'https://example.com/ws'], |
69 |
| - ['?http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'], |
70 |
| - ['?http://0.0.0.0', 'https://something.com', 'https://something.com/ws'], |
71 |
| - ['?http://0.0.0.0', 'http://something.com', 'http://something.com/ws'], |
72 |
| - ['?http://[::]', 'https://something.com', 'https://something.com/ws'], |
| 5 | + // __resourceQuery, location and socket URL |
| 6 | + ['?http://example.com', 'http://example.com', 'http://example.com/ws'], |
73 | 7 | ['?http://example.com', 'http://something.com', 'http://example.com/ws'],
|
74 |
| - ['?https://example.com', 'http://something.com', 'https://example.com/ws'], |
| 8 | + [null, 'http://example.com', 'http://example.com/ws'], |
| 9 | + ['?https://example.com', 'https://example.com', 'https://example.com/ws'], |
| 10 | + [null, 'https://example.com', 'https://example.com/ws'], |
75 | 11 | [
|
76 |
| - '?https://example.com?host=asdf', |
77 |
| - 'http://something.com', |
78 |
| - 'https://asdf/ws', |
| 12 | + '?http://example.com&port=8080', |
| 13 | + 'http://example.com:8080', |
| 14 | + 'http://example.com:8080/ws', |
79 | 15 | ],
|
80 | 16 | [
|
81 |
| - '?https://example.com?port=34', |
82 |
| - 'http://something.com', |
83 |
| - 'https://example.com:34/ws', |
| 17 | + '?http://example.com:0', |
| 18 | + 'http://example.com:8080', |
| 19 | + 'http://example.com:8080/ws', |
84 | 20 | ],
|
| 21 | + [null, 'http://example.com:8080', 'http://example.com:8080/ws'], |
85 | 22 | [
|
86 |
| - '?https://example.com?path=xxx', |
87 |
| - 'http://something.com', |
88 |
| - 'https://example.com/xxx', |
| 23 | + '?http://example.com/path/foo.js', |
| 24 | + 'http://example.com/foo/bar', |
| 25 | + 'http://example.com/ws', |
| 26 | + ], |
| 27 | + [null, 'http://example.com/foo/bar', 'http://example.com/ws'], |
| 28 | + [ |
| 29 | + '?http://user:password@localhost/', |
| 30 | + 'http://user:password@localhost/', |
| 31 | + 'http://user:password@localhost/ws', |
89 | 32 | ],
|
90 | 33 | [
|
91 |
| - '?http://0.0.0.0:8096&port=8097', |
92 |
| - 'http://localhost', |
93 |
| - 'http://localhost:8097/ws', |
| 34 | + null, |
| 35 | + 'http://user:password@localhost/', |
| 36 | + 'http://user:password@localhost/ws', |
94 | 37 | ],
|
95 | 38 | [
|
96 |
| - '?http://example.com:8096&port=location', |
| 39 | + '?http://user:password@localhost:8080/', |
| 40 | + 'http://user:password@localhost/', |
| 41 | + 'http://user:password@localhost:8080/ws', |
| 42 | + ], |
| 43 | + [ |
| 44 | + null, |
| 45 | + 'http://user:password@localhost:8080/', |
| 46 | + 'http://user:password@localhost:8080/ws', |
| 47 | + ], |
| 48 | + ['?http://0.0.0.0', 'http://127.0.0.1', 'http://127.0.0.1/ws'], |
| 49 | + ['?http://0.0.0.0', 'http://192.168.0.1', 'http://192.168.0.1/ws'], |
| 50 | + ['?http://0.0.0.0', 'https://192.168.0.1', 'https://192.168.0.1/ws'], |
| 51 | + ['?http://0.0.0.0', 'https://example.com', 'https://example.com/ws'], |
| 52 | + [ |
| 53 | + '?http://0.0.0.0', |
| 54 | + 'https://example.com:8080', |
| 55 | + 'https://example.com:8080/ws', |
| 56 | + ], |
| 57 | + [ |
| 58 | + '?http://0.0.0.0&port=9090', |
| 59 | + 'https://example.com', |
| 60 | + 'https://example.com:9090/ws', |
| 61 | + ], |
| 62 | + [ |
| 63 | + '?http://0.0.0.0&port=9090', |
| 64 | + 'https://example.com:8080', |
| 65 | + 'https://example.com:9090/ws', |
| 66 | + ], |
| 67 | + ['?http://localhost', 'http://localhost', 'http://localhost/ws'], |
| 68 | + [ |
| 69 | + '?http://localhost:8080', |
| 70 | + 'http://localhost:8080', |
| 71 | + 'http://localhost:8080/ws', |
| 72 | + ], |
| 73 | + [ |
| 74 | + '?https://localhost:8080', |
| 75 | + 'https://localhost:8080', |
| 76 | + 'https://localhost:8080/ws', |
| 77 | + ], |
| 78 | + [null, 'https://localhost:8080', 'https://localhost:8080/ws'], |
| 79 | + ['?http://127.0.0.1', 'http://something.com', 'http://127.0.0.1/ws'], |
| 80 | + ['?http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'], |
| 81 | + ['?https://127.0.0.1', 'http://something.com', 'https://127.0.0.1/ws'], |
| 82 | + [ |
| 83 | + '?https://example.com&host=example.com', |
97 | 84 | 'http://something.com',
|
98 |
| - 'http://example.com/ws', |
| 85 | + 'https://example.com/ws', |
99 | 86 | ],
|
100 | 87 | [
|
101 |
| - '?http://0.0.0.0:8096&port=location', |
102 |
| - 'http://localhost:3000', |
103 |
| - 'http://localhost:3000/ws', |
| 88 | + '?https://example.com&path=custom', |
| 89 | + 'http://something.com', |
| 90 | + 'https://example.com/custom', |
| 91 | + ], |
| 92 | + [ |
| 93 | + '?https://example.com&path=/custom', |
| 94 | + 'http://something.com', |
| 95 | + 'https://example.com/custom', |
104 | 96 | ],
|
| 97 | + ['?http://[::]', 'http://something.com', 'http://something.com/ws'], |
| 98 | + ['?http://[::]', 'https://something.com', 'https://something.com/ws'], |
| 99 | + ['?http://[::1]:8080/', 'http://[::1]:8080/', 'http://[::1]:8080/ws'], |
| 100 | + ['?https://[::1]:8080/', 'http://[::1]:8080/', 'https://[::1]:8080/ws'], |
| 101 | + [null, 'http://[::1]:8080/', 'http://[::1]:8080/ws'], |
| 102 | + [null, 'https://[::1]:8080/', 'https://[::1]:8080/ws'], |
105 | 103 | ];
|
106 |
| - samples3.forEach(([scriptSrc, loc, expected]) => { |
107 |
| - test(`should return socket ${expected} for query ${scriptSrc} and location ${loc}`, () => { |
108 |
| - const createSocketUrl = require('../../../client-src/utils/createSocketUrl'); |
109 | 104 |
|
110 |
| - expect(createSocketUrl(scriptSrc, loc).toString()).toEqual(expected); |
| 105 | + samples.forEach(([__resourceQuery, location, expected]) => { |
| 106 | + jest.doMock('../../../client-src/utils/getCurrentScriptSource', () => () => |
| 107 | + location |
| 108 | + ); |
| 109 | + |
| 110 | + const createSocketUrl = require('../../../client-src/utils/createSocketUrl'); |
| 111 | + const parseURL = require('../../../client-src/utils/parseURL'); |
| 112 | + |
| 113 | + test.only(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => { |
| 114 | + const selfLocation = new URL(location); |
| 115 | + |
| 116 | + delete window.location; |
| 117 | + |
| 118 | + window.location = selfLocation; |
| 119 | + |
| 120 | + const parsedURL = parseURL(__resourceQuery); |
| 121 | + |
| 122 | + expect(createSocketUrl(parsedURL)).toBe(expected); |
111 | 123 | });
|
| 124 | + |
| 125 | + jest.resetModules(); |
112 | 126 | });
|
113 | 127 | });
|
0 commit comments