UNPKG

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