-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcreateSocketURL.test.js
More file actions
139 lines (128 loc) · 4.82 KB
/
createSocketURL.test.js
File metadata and controls
139 lines (128 loc) · 4.82 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
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
/**
* @jest-environment jsdom
*/
"use strict";
describe("'createSocketURL' function ", () => {
const samples = [
// __resourceQuery, location and socket URL
[
"?hostname=example.com&pathname=/ws",
"http://example.com",
"ws://example.com/ws",
],
["?protocol=auto:", "http://example.com", "ws://example.com/ws"],
["?protocol=auto:", "https://example.com", "wss://example.com/ws"],
["?protocol=wss:", "http://example.com", "wss://example.com/ws"],
["?protocol=https:", "https://example.com", "wss://example.com/ws"],
["?protocol=http:", "https://example.com", "ws://example.com/ws"],
["?hostname=example.com", "http://example.com", "ws://example.com/ws"],
[
"?username=username&password=password",
"http://example.com",
"ws://username:password@example.com/ws",
],
[
"?hostname=example.com&port=80",
"http://example.com",
"ws://example.com:80/ws",
],
["?port=0", "http://example.com:8080", "ws://example.com:8080/ws"],
["?port=80", "http://example.com:8080", "ws://example.com:80/ws"],
["?hostname=0.0.0.0", "http://127.0.0.1", "ws://127.0.0.1/ws"],
["?hostname=0.0.0.0", "http://192.168.0.1", "ws://192.168.0.1/ws"],
["?hostname=0.0.0.0", "https://192.168.0.1", "wss://192.168.0.1/ws"],
["?hostname=0.0.0.0", "https://example.com", "wss://example.com/ws"],
[
"?hostname=0.0.0.0",
"http://example.com:8080",
"ws://example.com:8080/ws",
],
[
"?hostname=0.0.0.0",
"http://example.com:8080",
"ws://example.com:8080/ws",
],
[
"?hostname=0.0.0.0",
"https://example.com:8080",
"wss://example.com:8080/ws",
],
["?hostname=::", "http://example.com:8080", "ws://example.com:8080/ws"],
["?hostname=[::]", "http://example.com:8080", "ws://example.com:8080/ws"],
["?hostname=[::]", "http://example.com:8080", "ws://example.com:8080/ws"],
["?hostname=%3A%3A", "http://example.com:8080", "ws://example.com:8080/ws"],
["?hostname=%3A%3A1", "http://example.com:8080", "ws://[::1]:8080/ws"],
["?hostname=%3A%3A1", "https://example.com:8080", "wss://[::1]:8080/ws"],
[
"?hostname=%3A%3A",
"https://example.com:8080",
"wss://example.com:8080/ws",
],
[
"?hostname=%3A%3A",
"https://example.com:8080",
"wss://example.com:8080/ws",
],
[
"?pathname=/custom-ws",
"http://example.com",
"ws://example.com/custom-ws",
],
[
"?protocol=wss:&username=user&password=password&hostname=localhost&port=8080&pathname=/ws",
"http://user:password@localhost/",
"wss://user:password@localhost:8080/ws",
],
[null, "http://example.com", "ws://example.com/ws"],
[null, "https://example.com", "wss://example.com/ws"],
[null, "http://example.com:8080", "ws://example.com:8080/ws"],
[null, "http://example.com/foo/bar", "ws://example.com/ws"],
[
null,
"http://user:password@localhost/",
"ws://user:password@localhost/ws",
],
[null, "http://user@localhost:8080/", "ws://user@localhost:8080/ws"],
[
null,
"http://user:password@localhost:8080/",
"ws://user:password@localhost:8080/ws",
],
[null, "https://localhost:8080", "wss://localhost:8080/ws"],
[null, "http://127.0.0.1", "ws://127.0.0.1/ws"],
[null, "http://127.0.0.1:8080", "ws://127.0.0.1:8080/ws"],
[null, "https://127.0.0.1", "wss://127.0.0.1/ws"],
[null, "http://[::1]:8080/ws", "ws://[::1]:8080/ws"],
[null, "https://[::1]:8080/ws", "wss://[::1]:8080/ws"],
[null, "file:///home/user/project/index.html", "ws://localhost/ws"],
[null, "chrome-extension://localhost/", "ws://localhost/ws"],
[null, "file://localhost/", "ws://localhost/ws"],
[
"?protocol=ws:&hostname=0.0.0.0&port=3000&pathname=/ws&logging=none&reconnect=10",
"https://app.test.com",
"wss://app.test.com/ws",
],
[
"?protocol=ws:&hostname=0.0.0.0&port=3000&pathname=/ws&logging=none&reconnect=10",
"https://app.test.com:7777",
"wss://app.test.com:7777/ws",
],
];
samples.forEach(([__resourceQuery, location, expected]) => {
jest.doMock(
"../../../client-src/utils/getCurrentScriptSource",
() => () => new URL("./entry.js", location).toString()
);
const createSocketURL =
require("../../../client-src/utils/createSocketURL").default;
const parseURL = require("../../../client-src/utils/parseURL").default;
test(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
const selfLocation = new URL(location);
delete window.location;
window.location = selfLocation;
const parsedURL = parseURL(__resourceQuery);
expect(createSocketURL(parsedURL)).toBe(expected);
});
jest.resetModules();
});
});