process-env-port.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. const test = require('tap').test;
  3. const request = require('request');
  4. const spawn = require('child_process').spawn;
  5. function getRandomInt(min, max) {
  6. return Math.floor((Math.random() * ((max - min) + 1))) + min;
  7. }
  8. const sanePort = getRandomInt(1025, 65536);
  9. const floatingPointPort = 9090.86;
  10. const insanePorts = [1023, 65537, Infinity, 'wow'];
  11. function checkServerIsRunning(url, ps, t) {
  12. request(url, (err, res) => {
  13. if (!err && res.statusCode !== 500) {
  14. t.pass('a successful request from the server was made');
  15. } else {
  16. t.fail('the server could not be reached');
  17. }
  18. ps.kill('SIGTERM');
  19. });
  20. }
  21. function startServer(url, port, t) {
  22. t.plan(2);
  23. const ecstatic = spawn(process.execPath, [`${__dirname}/../lib/ecstatic.js`], {
  24. env: {
  25. PORT: String(port),
  26. },
  27. });
  28. ecstatic.stdout.on('data', () => {
  29. t.pass('ecstatic should be started');
  30. checkServerIsRunning(url, ecstatic, t);
  31. });
  32. }
  33. test('sane port', (t) => {
  34. startServer(`http://127.0.0.1:${sanePort}`, sanePort, t);
  35. });
  36. test('floating point port', (t) => {
  37. startServer('http://127.0.0.1:9090', floatingPointPort, t);
  38. });
  39. insanePorts.forEach((port) => {
  40. test(`insane port: ${port}`, (t) => {
  41. startServer('http://127.0.0.1:8000', port, t);
  42. });
  43. });