forked from localtunnel/server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.test.js
109 lines (88 loc) · 3.55 KB
/
server.test.js
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
import request from 'supertest';
import assert from 'assert';
import { Server as WebSocketServer } from 'ws';
import WebSocket from 'ws';
import net from 'net';
import createServer from './server';
describe('Server', () => {
it('server starts and stops', async () => {
const server = createServer();
await new Promise(resolve => server.listen(resolve));
await new Promise(resolve => server.close(resolve));
});
it('should redirect root requests to landing page', async () => {
const server = createServer();
const res = await request(server).get('/');
assert.equal('https://localtunnel.github.io/www/', res.headers.location);
});
it('should support custom base domains', async () => {
const server = createServer({
domain: 'domain.example.com',
});
const res = await request(server).get('/');
assert.equal('https://localtunnel.github.io/www/', res.headers.location);
});
it('reject long domain name requests', async () => {
const server = createServer();
const res = await request(server).get('/thisdomainisoutsidethesizeofwhatweallowwhichissixtythreecharacters');
assert.equal(res.body.message, 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.');
});
it('should upgrade websocket requests', async () => {
const hostname = 'websocket-test';
const server = createServer({
domain: 'example.com',
});
await new Promise(resolve => server.listen(resolve));
const res = await request(server).get('/websocket-test');
const localTunnelPort = res.body.port;
const wss = await new Promise((resolve) => {
const wsServer = new WebSocketServer({ port: 0 }, () => {
resolve(wsServer);
});
});
const websocketServerPort = wss.address().port;
const ltSocket = net.createConnection({ port: localTunnelPort });
const wsSocket = net.createConnection({ port: websocketServerPort });
ltSocket.pipe(wsSocket).pipe(ltSocket);
wss.once('connection', (ws) => {
ws.once('message', (message) => {
ws.send(message);
});
});
const ws = new WebSocket('http://localhost:' + server.address().port, {
headers: {
host: hostname + '.example.com',
}
});
ws.on('open', () => {
ws.send('something');
});
await new Promise((resolve) => {
ws.once('message', (msg) => {
assert.equal(msg, 'something');
resolve();
});
});
wss.close();
await new Promise(resolve => server.close(resolve));
});
it('should support the /api/tunnels/:id/status endpoint', async () => {
const server = createServer();
await new Promise(resolve => server.listen(resolve));
// no such tunnel yet
const res = await request(server).get('/api/tunnels/foobar-test/status');
assert.equal(res.statusCode, 404);
// request a new client called foobar-test
{
const res = await request(server).get('/foobar-test');
}
{
const res = await request(server).get('/api/tunnels/foobar-test/status');
assert.equal(res.statusCode, 200);
assert.deepEqual(res.body, {
connected_sockets: 0,
});
}
await new Promise(resolve => server.close(resolve));
});
});