Skip to content

Commit

Permalink
use portfinder in cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
thornjad committed Oct 11, 2021
1 parent bdf879a commit 6f49089
Showing 1 changed file with 36 additions and 57 deletions.
93 changes: 36 additions & 57 deletions test/cli.test.js
Expand Up @@ -6,16 +6,12 @@ const test = require('tap').test;
const request = require('request');
const spawn = require('child_process').spawn;
const path = require('path');
const portfinder = require('portfinder');

const node = process.execPath;
const defaultUrl = 'http://localhost';
const defaultPort = 8080;

function getRandomInt(min, max) {
return Math.floor(Math.random() * ((max - min) + 1)) + min;
}

function startEcstatic(args) {
function startServer(args) {
return spawn(node, [require.resolve('../bin/http-server')].concat(args));
}

Expand Down Expand Up @@ -43,82 +39,65 @@ function tearDown(ps, t) {
});
}

const getRandomPort = (() => {
const usedPorts = [];
return () => {
const port = getRandomInt(1025, 65536);
if (usedPorts.indexOf(port) > -1) {
return getRandomPort();
}

usedPorts.push(port);
return port;
};
})();

test('setting port via cli - default port', (t) => {
t.plan(2);

const port = defaultPort;
const options = ['.'];
const ecstatic = startEcstatic(options);

tearDown(ecstatic, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
const getPort = () => new Promise((resolve, reject) => {
portfinder.getPort((err, port) => {
if (err) reject(err);
resolve(port);
});
});

test('setting port via cli - custom port', (t) => {
t.plan(2);

const port = getRandomPort();
const options = ['.', '--port', port];
const ecstatic = startEcstatic(options);
getPort().then((port) => {
const options = ['.', '--port', port];
const server = startServer(options);

tearDown(ecstatic, t);
tearDown(server, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}`, msg, t);
});
});
});

test('setting mimeTypes via cli - .types file', (t) => {
t.plan(4);

const port = getRandomPort();
const root = path.resolve(__dirname, 'public/');
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
const ecstatic = startEcstatic(options);
getPort().then((port) => {
const root = path.resolve(__dirname, 'public/');
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
const server = startServer(options);

tearDown(ecstatic, t);
tearDown(server, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/secret');
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/secret');
});
});
});
});

test('setting mimeTypes via cli - directly', (t) => {
t.plan(4);

const port = getRandomPort();
const root = path.resolve(__dirname, 'public/');
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
const options = [root, '--port', port].concat(mimeType);
const ecstatic = startEcstatic(options);
getPort().then((port) => {
const root = path.resolve(__dirname, 'public/');
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
const options = [root, '--port', port].concat(mimeType);
const server = startServer(options);

// TODO: remove error handler
tearDown(ecstatic, t);
// TODO: remove error handler
tearDown(server, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type');
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type');
});
});
});
});

0 comments on commit 6f49089

Please sign in to comment.