@@ -6,16 +6,12 @@ const test = require('tap').test;
66const request = require ( 'request' ) ;
77const spawn = require ( 'child_process' ) . spawn ;
88const path = require ( 'path' ) ;
9+ const portfinder = require ( 'portfinder' ) ;
910
1011const node = process . execPath ;
11- const defaultUrl = 'http://localhost' ;
1212const defaultPort = 8080 ;
1313
14- function getRandomInt ( min , max ) {
15- return Math . floor ( Math . random ( ) * ( ( max - min ) + 1 ) ) + min ;
16- }
17-
18- function startEcstatic ( args ) {
14+ function startServer ( args ) {
1915 return spawn ( node , [ require . resolve ( '../bin/http-server' ) ] . concat ( args ) ) ;
2016}
2117
@@ -43,82 +39,65 @@ function tearDown(ps, t) {
4339 } ) ;
4440}
4541
46- const getRandomPort = ( ( ) => {
47- const usedPorts = [ ] ;
48- return ( ) => {
49- const port = getRandomInt ( 1025 , 65536 ) ;
50- if ( usedPorts . indexOf ( port ) > - 1 ) {
51- return getRandomPort ( ) ;
52- }
53-
54- usedPorts . push ( port ) ;
55- return port ;
56- } ;
57- } ) ( ) ;
58-
59- test ( 'setting port via cli - default port' , ( t ) => {
60- t . plan ( 2 ) ;
61-
62- const port = defaultPort ;
63- const options = [ '.' ] ;
64- const ecstatic = startEcstatic ( options ) ;
65-
66- tearDown ( ecstatic , t ) ;
67-
68- ecstatic . stdout . on ( 'data' , ( msg ) => {
69- checkServerIsRunning ( `${ defaultUrl } :${ port } ` , msg , t ) ;
42+ const getPort = ( ) => new Promise ( ( resolve , reject ) => {
43+ portfinder . getPort ( ( err , port ) => {
44+ if ( err ) reject ( err ) ;
45+ resolve ( port ) ;
7046 } ) ;
7147} ) ;
7248
7349test ( 'setting port via cli - custom port' , ( t ) => {
7450 t . plan ( 2 ) ;
7551
76- const port = getRandomPort ( ) ;
77- const options = [ '.' , '--port' , port ] ;
78- const ecstatic = startEcstatic ( options ) ;
52+ getPort ( ) . then ( ( port ) => {
53+ const options = [ '.' , '--port' , port ] ;
54+ const server = startServer ( options ) ;
7955
80- tearDown ( ecstatic , t ) ;
56+ tearDown ( server , t ) ;
8157
82- ecstatic . stdout . on ( 'data' , ( msg ) => {
83- checkServerIsRunning ( `${ defaultUrl } :${ port } ` , msg , t ) ;
58+ server . stdout . on ( 'data' , ( msg ) => {
59+ checkServerIsRunning ( `http://localhost:${ port } ` , msg , t ) ;
60+ } ) ;
8461 } ) ;
8562} ) ;
8663
8764test ( 'setting mimeTypes via cli - .types file' , ( t ) => {
8865 t . plan ( 4 ) ;
8966
90- const port = getRandomPort ( ) ;
91- const root = path . resolve ( __dirname , 'public/' ) ;
92- const pathMimetypeFile = path . resolve ( __dirname , 'fixtures/custom_mime_type.types' ) ;
93- const options = [ root , '--port' , port , '--mimetypes' , pathMimetypeFile ] ;
94- const ecstatic = startEcstatic ( options ) ;
67+ getPort ( ) . then ( ( port ) => {
68+ const root = path . resolve ( __dirname , 'public/' ) ;
69+ const pathMimetypeFile = path . resolve ( __dirname , 'fixtures/custom_mime_type.types' ) ;
70+ const options = [ root , '--port' , port , '--mimetypes' , pathMimetypeFile ] ;
71+ const server = startServer ( options ) ;
9572
96- tearDown ( ecstatic , t ) ;
73+ tearDown ( server , t ) ;
9774
98- ecstatic . stdout . on ( 'data' , ( msg ) => {
99- checkServerIsRunning ( `${ defaultUrl } :${ port } /custom_mime_type.opml` , msg , t , ( err , res ) => {
100- t . error ( err ) ;
101- t . equal ( res . headers [ 'content-type' ] , 'application/secret' ) ;
75+ server . stdout . on ( 'data' , ( msg ) => {
76+ checkServerIsRunning ( `http://localhost:${ port } /custom_mime_type.opml` , msg , t , ( err , res ) => {
77+ t . error ( err ) ;
78+ t . equal ( res . headers [ 'content-type' ] , 'application/secret' ) ;
79+ } ) ;
10280 } ) ;
10381 } ) ;
10482} ) ;
10583
10684test ( 'setting mimeTypes via cli - directly' , ( t ) => {
10785 t . plan ( 4 ) ;
10886
109- const port = getRandomPort ( ) ;
110- const root = path . resolve ( __dirname , 'public/' ) ;
111- const mimeType = [ '--mimetypes' , '{ "application/x-my-type": ["opml"] }' ] ;
112- const options = [ root , '--port' , port ] . concat ( mimeType ) ;
113- const ecstatic = startEcstatic ( options ) ;
87+ getPort ( ) . then ( ( port ) => {
88+ const root = path . resolve ( __dirname , 'public/' ) ;
89+ const mimeType = [ '--mimetypes' , '{ "application/x-my-type": ["opml"] }' ] ;
90+ const options = [ root , '--port' , port ] . concat ( mimeType ) ;
91+ const server = startServer ( options ) ;
11492
115- // TODO: remove error handler
116- tearDown ( ecstatic , t ) ;
93+ // TODO: remove error handler
94+ tearDown ( server , t ) ;
11795
118- ecstatic . stdout . on ( 'data' , ( msg ) => {
119- checkServerIsRunning ( `${ defaultUrl } :${ port } /custom_mime_type.opml` , msg , t , ( err , res ) => {
120- t . error ( err ) ;
121- t . equal ( res . headers [ 'content-type' ] , 'application/x-my-type' ) ;
96+ server . stdout . on ( 'data' , ( msg ) => {
97+ checkServerIsRunning ( `http://localhost:${ port } /custom_mime_type.opml` , msg , t , ( err , res ) => {
98+ t . error ( err ) ;
99+ t . equal ( res . headers [ 'content-type' ] , 'application/x-my-type' ) ;
100+ } ) ;
122101 } ) ;
123102 } ) ;
124103} ) ;
0 commit comments