localtunnel.spec.js 3.7 KB

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