@@ -8,15 +8,21 @@ const subject = require('../../lib/handler');
8
8
describe ( 'handler' , ( ) => {
9
9
let sandbox ;
10
10
let mockFiles ;
11
+ let mockDirs ;
11
12
12
13
before ( ( ) => {
13
14
sandbox = sinon . sandbox . create ( ) ;
14
15
// Stub accessSync to act like only files in mockFiles exist
15
16
sandbox . stub ( fs , 'accessSync' , path => {
16
- if ( mockFiles . indexOf ( path ) === - 1 ) {
17
+ if ( mockFiles . indexOf ( path ) === - 1 && mockDirs . indexOf ( path ) === - 1 ) {
17
18
throw new Error ( 'Doesn\'t exist' ) ;
18
19
}
19
20
} ) ;
21
+ sandbox . stub ( fs , 'statSync' , path => ( {
22
+ isFile ( ) {
23
+ return mockFiles . indexOf ( path ) !== - 1 ;
24
+ } ,
25
+ } ) ) ;
20
26
} ) ;
21
27
22
28
after ( ( ) => {
@@ -25,6 +31,7 @@ describe('handler', () => {
25
31
26
32
beforeEach ( ( ) => {
27
33
mockFiles = [ ] ;
34
+ mockDirs = [ ] ;
28
35
} ) ;
29
36
30
37
// Create a handler with the specified options
@@ -33,8 +40,7 @@ describe('handler', () => {
33
40
cwd : '.' ,
34
41
name : 'serve' ,
35
42
endpoint : 'dist' ,
36
- directoryIndexFile : 'index.html' ,
37
- indexPath : undefined ,
43
+ indexPath : join ( '.' , 'index.html' ) ,
38
44
} , options ) ) ;
39
45
}
40
46
@@ -57,13 +63,15 @@ describe('handler', () => {
57
63
}
58
64
59
65
it ( 'works' , ( ) => {
66
+ mockDirs . push ( '.' ) ;
60
67
mockFiles . push ( 'script.js' ) ;
61
68
return handlerExec ( 'serve://dist/script.js' ) . then ( path => {
62
69
assert . equal ( path , 'script.js' ) ;
63
70
} ) ;
64
71
} ) ;
65
72
66
73
it ( 'works with multiple requests' , ( ) => {
74
+ mockDirs . push ( '.' ) ;
67
75
mockFiles . push ( 'script1.js' ) ;
68
76
mockFiles . push ( 'script2.js' ) ;
69
77
@@ -77,25 +85,19 @@ describe('handler', () => {
77
85
} ) ;
78
86
} ) ;
79
87
80
- it ( 'serves directoryIndexFile from the root' , ( ) => {
88
+ it ( 'serves indexPath from the root' , ( ) => {
89
+ mockDirs . push ( '.' ) ;
81
90
mockFiles . push ( 'foo.html' ) ;
82
91
return handlerExec ( 'serve://dist' , {
83
- directoryIndexFile : 'foo.html' ,
84
- } ) . then ( path => {
85
- assert . equal ( path , 'foo.html' ) ;
86
- } ) ;
87
- } ) ;
88
-
89
- it ( 'serves directoryIndexFile for missing files' , ( ) => {
90
- mockFiles . push ( 'foo.html' ) ;
91
- return handlerExec ( 'serve://dist/missing.js' , {
92
- directoryIndexFile : 'foo.html' ,
92
+ cwd : '.' ,
93
+ indexPath : join ( '.' , 'foo.html' ) ,
93
94
} ) . then ( path => {
94
- assert . equal ( path , ' foo.html') ;
95
+ assert . equal ( path , join ( '.' , ' foo.html') ) ;
95
96
} ) ;
96
97
} ) ;
97
98
98
99
it ( 'respects relative cwd' , ( ) => {
100
+ mockDirs . push ( join ( 'foo' , 'bar' ) ) ;
99
101
mockFiles . push ( join ( 'foo' , 'bar' , 'script.js' ) ) ;
100
102
return handlerExec ( 'serve://dist/script.js' , {
101
103
cwd : join ( 'foo' , 'bar' ) ,
@@ -105,6 +107,7 @@ describe('handler', () => {
105
107
} ) ;
106
108
107
109
it ( 'respects absolute cwd' , ( ) => {
110
+ mockDirs . push ( resolve ( 'foo' , 'bar' ) ) ;
108
111
mockFiles . push ( resolve ( 'foo' , 'bar' , 'script.js' ) ) ;
109
112
return handlerExec ( 'serve://dist/script.js' , {
110
113
cwd : resolve ( 'foo' , 'bar' ) ,
@@ -114,6 +117,7 @@ describe('handler', () => {
114
117
} ) ;
115
118
116
119
it ( 'respects endpoint' , ( ) => {
120
+ mockDirs . push ( '.' ) ;
117
121
mockFiles . push ( 'script.js' ) ;
118
122
return handlerExec ( 'serve://custom/script.js' , {
119
123
endpoint : 'custom' ,
@@ -123,22 +127,26 @@ describe('handler', () => {
123
127
} ) ;
124
128
125
129
it ( 'respects indexPath for missing files' , ( ) => {
130
+ mockDirs . push ( 'bar' ) ;
126
131
mockFiles . push ( join ( 'foo' , 'bar.html' ) ) ;
127
132
return handlerExec ( 'serve://dist/missing.js' , {
133
+ cwd : 'bar' ,
128
134
indexPath : join ( 'foo' , 'bar.html' ) ,
129
135
} ) . then ( path => {
130
136
assert . equal ( path , join ( 'foo' , 'bar.html' ) ) ;
131
137
} ) ;
132
138
} ) ;
133
139
134
140
it ( 'ignores hashes' , ( ) => {
141
+ mockDirs . push ( '.' ) ;
135
142
mockFiles . push ( 'script.js' ) ;
136
143
return handlerExec ( 'serve://dist/script.js#hash' ) . then ( path => {
137
144
assert . equal ( path , 'script.js' ) ;
138
145
} ) ;
139
146
} ) ;
140
147
141
148
it ( 'ignores query params' , ( ) => {
149
+ mockDirs . push ( '.' ) ;
142
150
mockFiles . push ( 'script.js' ) ;
143
151
return handlerExec ( 'serve://dist/script.js?query=param' ) . then ( path => {
144
152
assert . equal ( path , 'script.js' ) ;
0 commit comments