@@ -6,16 +6,12 @@ const test = require('tap').test;
6
6
const request = require ( 'request' ) ;
7
7
const spawn = require ( 'child_process' ) . spawn ;
8
8
const path = require ( 'path' ) ;
9
+ const portfinder = require ( 'portfinder' ) ;
9
10
10
11
const node = process . execPath ;
11
- const defaultUrl = 'http://localhost' ;
12
12
const defaultPort = 8080 ;
13
13
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 ) {
19
15
return spawn ( node , [ require . resolve ( '../bin/http-server' ) ] . concat ( args ) ) ;
20
16
}
21
17
@@ -43,82 +39,65 @@ function tearDown(ps, t) {
43
39
} ) ;
44
40
}
45
41
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 ) ;
70
46
} ) ;
71
47
} ) ;
72
48
73
49
test ( 'setting port via cli - custom port' , ( t ) => {
74
50
t . plan ( 2 ) ;
75
51
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 ) ;
79
55
80
- tearDown ( ecstatic , t ) ;
56
+ tearDown ( server , t ) ;
81
57
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
+ } ) ;
84
61
} ) ;
85
62
} ) ;
86
63
87
64
test ( 'setting mimeTypes via cli - .types file' , ( t ) => {
88
65
t . plan ( 4 ) ;
89
66
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 ) ;
95
72
96
- tearDown ( ecstatic , t ) ;
73
+ tearDown ( server , t ) ;
97
74
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
+ } ) ;
102
80
} ) ;
103
81
} ) ;
104
82
} ) ;
105
83
106
84
test ( 'setting mimeTypes via cli - directly' , ( t ) => {
107
85
t . plan ( 4 ) ;
108
86
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 ) ;
114
92
115
- // TODO: remove error handler
116
- tearDown ( ecstatic , t ) ;
93
+ // TODO: remove error handler
94
+ tearDown ( server , t ) ;
117
95
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
+ } ) ;
122
101
} ) ;
123
102
} ) ;
124
103
} ) ;
0 commit comments