cli.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. /* this test suit is incomplete 2015-12-18 */
  3. const test = require('tap').test;
  4. const request = require('request');
  5. const spawn = require('child_process').spawn;
  6. const path = require('path');
  7. const node = process.execPath;
  8. const defaultUrl = 'http://localhost';
  9. const defaultPort = 8000;
  10. function getRandomInt(min, max) {
  11. return Math.floor(Math.random() * ((max - min) + 1)) + min;
  12. }
  13. function startEcstatic(args) {
  14. return spawn(node, [require.resolve('../lib/ecstatic.js')].concat(args));
  15. }
  16. function checkServerIsRunning(url, t, _cb) {
  17. const cb = _cb || (() => {});
  18. request(url, (err, res) => {
  19. if (!err && res.statusCode !== 500) {
  20. t.pass('a successful request from the server was made');
  21. cb(null, res);
  22. } else {
  23. t.fail(`the server could not be reached @ ${url}`);
  24. cb(err);
  25. }
  26. });
  27. }
  28. function tearDown(ps, t) {
  29. t.tearDown(() => {
  30. ps.kill('SIGTERM');
  31. });
  32. }
  33. const getRandomPort = (() => {
  34. const usedPorts = [];
  35. return () => {
  36. const port = getRandomInt(1025, 65536);
  37. if (usedPorts.indexOf(port) > -1) {
  38. return getRandomPort();
  39. }
  40. usedPorts.push(port);
  41. return port;
  42. };
  43. })();
  44. test('setting port via cli - default port', (t) => {
  45. t.plan(2);
  46. const port = defaultPort;
  47. const options = ['.'];
  48. const ecstatic = startEcstatic(options);
  49. tearDown(ecstatic, t);
  50. ecstatic.stdout.on('data', () => {
  51. t.pass('ecstatic should be started');
  52. checkServerIsRunning(`${defaultUrl}:${port}`, t);
  53. });
  54. });
  55. test('setting port via cli - custom port', (t) => {
  56. t.plan(2);
  57. const port = getRandomPort();
  58. const options = ['.', '--port', port];
  59. const ecstatic = startEcstatic(options);
  60. tearDown(ecstatic, t);
  61. ecstatic.stdout.on('data', () => {
  62. t.pass('ecstatic should be started');
  63. checkServerIsRunning(`${defaultUrl}:${port}`, t);
  64. });
  65. });
  66. test('setting mimeTypes via cli - .types file', (t) => {
  67. t.plan(2);
  68. const port = getRandomPort();
  69. const root = path.resolve(__dirname, 'public/');
  70. const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
  71. const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
  72. const ecstatic = startEcstatic(options);
  73. tearDown(ecstatic, t);
  74. ecstatic.stdout.on('data', () => {
  75. t.pass('ecstatic should be started');
  76. checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, t);
  77. });
  78. });
  79. test('setting mimeTypes via cli - directly', (t) => {
  80. t.plan(4);
  81. const port = getRandomPort();
  82. const root = path.resolve(__dirname, 'public/');
  83. const mimeType = ['--mimeTypes', '{ "application/x-my-type": ["opml"] }'];
  84. const options = [root, '--port', port, '--mimetypes'].concat(mimeType);
  85. const ecstatic = startEcstatic(options);
  86. // TODO: remove error handler
  87. tearDown(ecstatic, t);
  88. ecstatic.stdout.on('data', () => {
  89. t.pass('ecstatic should be started');
  90. checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, t, (err, res) => {
  91. t.error(err);
  92. t.equal(res.headers['content-type'], 'application/x-my-type; charset=utf-8');
  93. });
  94. });
  95. });