-
Notifications
You must be signed in to change notification settings - Fork 14
/
localtunnel.spec.js
134 lines (114 loc) · 3.83 KB
/
localtunnel.spec.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
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
/* eslint-disable no-console */
const crypto = require('crypto');
const http = require('http');
const https = require('https');
const url = require('url');
const supertest = require('supertest');
const chai = require('chai');
chai.use(require('chai-string'));
const expect = chai.expect;
const assert = require('assert');
const localtunnel = require('./localtunnel');
let fakePort;
let testServer;
describe('localtunnel', () => {
before(done => {
testServer = http.createServer();
testServer.on('request', (req, res) => {
res.write(req.headers.host);
res.end();
});
testServer.listen(() => {
const { port } = testServer.address();
fakePort = port;
done();
});
});
after(() => {
testServer.close();
});
it('query localtunnel server w/ ident', async () => {
const tunnel = await localtunnel({ port: fakePort });
try {
await supertest(tunnel.url)
.get('/')
.expect(200)
.expect(res => {
expect(tunnel.url).to.endsWith(res.text);
});
} finally {
tunnel.close();
}
});
it('request specific domain', async () => {
const subdomain = Math.random()
.toString(36)
.substr(2);
const tunnel = await localtunnel({ port: fakePort, subdomain });
tunnel.close();
expect(tunnel.url).to.startsWith(`https://${subdomain}.`);
});
describe('--local-host localhost', () => {
it('override Host header with local-host', async () => {
const tunnel = await localtunnel({ port: fakePort, local_host: 'localhost' });
try {
await supertest(tunnel.url)
.get('/')
.expect(200)
.expect(res => {
expect(res.text).to.equal('localhost');
});
} finally {
tunnel.close();
}
});
});
describe('--local-host 127.0.0.1', () => {
it('override Host header with local-host', async () => {
const tunnel = await localtunnel({ port: fakePort, local_host: '127.0.0.1' });
try {
await supertest(tunnel.url)
.get('/')
.expect(200)
.expect(res => {
expect(res.text).to.equal('127.0.0.1');
});
} finally {
tunnel.close();
}
});
it('send chunked request', async () => {
const tunnel = await localtunnel({ port: fakePort, local_host: '127.0.0.1' });
const parsed = url.parse(tunnel.url);
const opt = {
host: parsed.host,
port: 443,
headers: {
host: parsed.hostname,
'Transfer-Encoding': 'chunked',
},
path: '/',
};
await new Promise((resolve, reject) => {
const req = https.request(opt, res => {
res.setEncoding('utf8');
let body = '';
res.on('data', chunk => {
body += chunk;
});
res.on('end', () => {
try {
assert.strictEqual(body, '127.0.0.1');
} catch (e) {
reject(e);
} finally {
tunnel.close();
}
resolve();
});
});
req.end(crypto.randomBytes(1024 * 8).toString('base64'));
});
});
});
});