1 |
|
2 |
|
3 | const crypto = require('crypto');
|
4 | const http = require('http');
|
5 | const https = require('https');
|
6 | const url = require('url');
|
7 | const assert = require('assert');
|
8 |
|
9 | const localtunnel = require('./localtunnel');
|
10 |
|
11 | let fakePort;
|
12 |
|
13 | before(done => {
|
14 | const server = http.createServer();
|
15 | server.on('request', (req, res) => {
|
16 | res.write(req.headers.host);
|
17 | res.end();
|
18 | });
|
19 | server.listen(() => {
|
20 | const { port } = server.address();
|
21 | fakePort = port;
|
22 | done();
|
23 | });
|
24 | });
|
25 |
|
26 | test('query localtunnel server w/ ident', async done => {
|
27 | const tunnel = await localtunnel({ port: fakePort });
|
28 | assert.ok(new RegExp('^https://.*localtunnel.me$').test(tunnel.url));
|
29 |
|
30 | const parsed = url.parse(tunnel.url);
|
31 | const opt = {
|
32 | host: parsed.host,
|
33 | port: 443,
|
34 | headers: { host: parsed.hostname },
|
35 | path: '/',
|
36 | };
|
37 |
|
38 | const req = https.request(opt, res => {
|
39 | res.setEncoding('utf8');
|
40 | let body = '';
|
41 |
|
42 | res.on('data', chunk => {
|
43 | body += chunk;
|
44 | });
|
45 |
|
46 | res.on('end', () => {
|
47 | assert(/.*[.]localtunnel[.]me/.test(body), body);
|
48 | tunnel.close();
|
49 | done();
|
50 | });
|
51 | });
|
52 |
|
53 | req.end();
|
54 | });
|
55 |
|
56 | test('request specific domain', async () => {
|
57 | const subdomain = Math.random()
|
58 | .toString(36)
|
59 | .substr(2);
|
60 | const tunnel = await localtunnel({ port: fakePort, subdomain });
|
61 | assert.ok(new RegExp(`^https://${subdomain}.localtunnel.me$`).test(tunnel.url));
|
62 | tunnel.close();
|
63 | });
|
64 |
|
65 | describe('--local-host localhost', () => {
|
66 | test('override Host header with local-host', async done => {
|
67 | const tunnel = await localtunnel({ port: fakePort, local_host: 'localhost' });
|
68 | assert.ok(new RegExp('^https://.*localtunnel.me$').test(tunnel.url));
|
69 |
|
70 | const parsed = url.parse(tunnel.url);
|
71 | const opt = {
|
72 | host: parsed.host,
|
73 | port: 443,
|
74 | headers: { host: parsed.hostname },
|
75 | path: '/',
|
76 | };
|
77 |
|
78 | const req = https.request(opt, res => {
|
79 | res.setEncoding('utf8');
|
80 | let body = '';
|
81 |
|
82 | res.on('data', chunk => {
|
83 | body += chunk;
|
84 | });
|
85 |
|
86 | res.on('end', () => {
|
87 | assert.equal(body, 'localhost');
|
88 | tunnel.close();
|
89 | done();
|
90 | });
|
91 | });
|
92 |
|
93 | req.end();
|
94 | });
|
95 | });
|
96 |
|
97 | describe('--local-host 127.0.0.1', () => {
|
98 | test('override Host header with local-host', async done => {
|
99 | const tunnel = await localtunnel({ port: fakePort, local_host: '127.0.0.1' });
|
100 | assert.ok(new RegExp('^https://.*localtunnel.me$').test(tunnel.url));
|
101 |
|
102 | const parsed = url.parse(tunnel.url);
|
103 | const opt = {
|
104 | host: parsed.host,
|
105 | port: 443,
|
106 | headers: {
|
107 | host: parsed.hostname,
|
108 | },
|
109 | path: '/',
|
110 | };
|
111 |
|
112 | const req = https.request(opt, res => {
|
113 | res.setEncoding('utf8');
|
114 | let body = '';
|
115 |
|
116 | res.on('data', chunk => {
|
117 | body += chunk;
|
118 | });
|
119 |
|
120 | res.on('end', () => {
|
121 | assert.equal(body, '127.0.0.1');
|
122 | tunnel.close();
|
123 | done();
|
124 | });
|
125 | });
|
126 |
|
127 | req.end();
|
128 | });
|
129 |
|
130 | test('send chunked request', async done => {
|
131 | const tunnel = await localtunnel({ port: fakePort, local_host: '127.0.0.1' });
|
132 | assert.ok(new RegExp('^https://.*localtunnel.me$').test(tunnel.url));
|
133 |
|
134 | const parsed = url.parse(tunnel.url);
|
135 | const opt = {
|
136 | host: parsed.host,
|
137 | port: 443,
|
138 | headers: {
|
139 | host: parsed.hostname,
|
140 | 'Transfer-Encoding': 'chunked',
|
141 | },
|
142 | path: '/',
|
143 | };
|
144 |
|
145 | const req = https.request(opt, res => {
|
146 | res.setEncoding('utf8');
|
147 | let body = '';
|
148 |
|
149 | res.on('data', chunk => {
|
150 | body += chunk;
|
151 | });
|
152 |
|
153 | res.on('end', () => {
|
154 | assert.equal(body, '127.0.0.1');
|
155 | tunnel.close();
|
156 | done();
|
157 | });
|
158 | });
|
159 |
|
160 | req.end(crypto.randomBytes(1024 * 8).toString('base64'));
|
161 | });
|
162 | });
|