@@ -3,20 +3,26 @@ const sinon = require('sinon');
3
3
const Promise = require ( 'bluebird' ) ;
4
4
const fs = require ( 'fs' ) ;
5
5
const { join, resolve } = require ( 'path' ) ;
6
- const createHandler = require ( '../../lib/handler' ) ;
6
+ const subject = require ( '../../lib/handler' ) ;
7
7
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,19 +31,22 @@ describe('handler', () => {
25
31
26
32
beforeEach ( ( ) => {
27
33
mockFiles = [ ] ;
34
+ mockDirs = [ ] ;
28
35
} ) ;
29
36
30
- // Create a handler with the specified options and call it with the given URL
31
- function handlerExec ( url , options = { } ) {
32
- return new Promise ( ( resolve , reject ) => {
33
- let handler = createHandler ( Object . assign ( {
34
- cwd : '.' ,
35
- name : 'serve' ,
36
- endpoint : 'dist' ,
37
- directoryIndexFile : 'index.html' ,
38
- indexPath : undefined ,
39
- } , options ) ) ;
37
+ // Create a handler with the specified options
38
+ function createHandler ( options ) {
39
+ return subject ( Object . assign ( {
40
+ cwd : '.' ,
41
+ name : 'serve' ,
42
+ endpoint : 'dist' ,
43
+ indexPath : join ( '.' , 'index.html' ) ,
44
+ } , options ) ) ;
45
+ }
40
46
47
+ // Call the handler with the given URL
48
+ function callHandler ( handler , url ) {
49
+ return new Promise ( ( resolve , reject ) => {
41
50
handler ( { url } , ( { path, error } ) => {
42
51
if ( path !== undefined ) {
43
52
resolve ( path ) ;
@@ -48,32 +57,47 @@ describe('handler', () => {
48
57
} ) ;
49
58
}
50
59
60
+ // Create a handler with the specified options and call it with the given URL
61
+ function handlerExec ( url , options = { } ) {
62
+ return callHandler ( createHandler ( options ) , url ) ;
63
+ }
64
+
51
65
it ( 'works' , ( ) => {
66
+ mockDirs . push ( '.' ) ;
52
67
mockFiles . push ( 'script.js' ) ;
53
68
return handlerExec ( 'serve://dist/script.js' ) . then ( path => {
54
69
assert . equal ( path , 'script.js' ) ;
55
70
} ) ;
56
71
} ) ;
57
72
58
- it ( 'serves directoryIndexFile from the root' , ( ) => {
59
- mockFiles . push ( 'foo.html' ) ;
60
- return handlerExec ( 'serve://dist' , {
61
- directoryIndexFile : 'foo.html' ,
73
+ it ( 'works with multiple requests' , ( ) => {
74
+ mockDirs . push ( '.' ) ;
75
+ mockFiles . push ( 'script1.js' ) ;
76
+ mockFiles . push ( 'script2.js' ) ;
77
+
78
+ let handler = createHandler ( ) ;
79
+ return callHandler ( handler , 'serve://dist/script1.js' ) . then ( path => {
80
+ assert . equal ( path , 'script1.js' ) ;
81
+
82
+ return callHandler ( handler , 'serve://dist/script2.js' ) ;
62
83
} ) . then ( path => {
63
- assert . equal ( path , 'foo.html ' ) ;
84
+ assert . equal ( path , 'script2.js ' ) ;
64
85
} ) ;
65
86
} ) ;
66
87
67
- it ( 'serves directoryIndexFile for missing files' , ( ) => {
88
+ it ( 'serves indexPath from the root' , ( ) => {
89
+ mockDirs . push ( '.' ) ;
68
90
mockFiles . push ( 'foo.html' ) ;
69
- return handlerExec ( 'serve://dist/missing.js' , {
70
- directoryIndexFile : 'foo.html' ,
91
+ return handlerExec ( 'serve://dist' , {
92
+ cwd : '.' ,
93
+ indexPath : join ( '.' , 'foo.html' ) ,
71
94
} ) . then ( path => {
72
- assert . equal ( path , ' foo.html') ;
95
+ assert . equal ( path , join ( '.' , ' foo.html') ) ;
73
96
} ) ;
74
97
} ) ;
75
98
76
99
it ( 'respects relative cwd' , ( ) => {
100
+ mockDirs . push ( join ( 'foo' , 'bar' ) ) ;
77
101
mockFiles . push ( join ( 'foo' , 'bar' , 'script.js' ) ) ;
78
102
return handlerExec ( 'serve://dist/script.js' , {
79
103
cwd : join ( 'foo' , 'bar' ) ,
@@ -83,6 +107,7 @@ describe('handler', () => {
83
107
} ) ;
84
108
85
109
it ( 'respects absolute cwd' , ( ) => {
110
+ mockDirs . push ( resolve ( 'foo' , 'bar' ) ) ;
86
111
mockFiles . push ( resolve ( 'foo' , 'bar' , 'script.js' ) ) ;
87
112
return handlerExec ( 'serve://dist/script.js' , {
88
113
cwd : resolve ( 'foo' , 'bar' ) ,
@@ -92,6 +117,7 @@ describe('handler', () => {
92
117
} ) ;
93
118
94
119
it ( 'respects endpoint' , ( ) => {
120
+ mockDirs . push ( '.' ) ;
95
121
mockFiles . push ( 'script.js' ) ;
96
122
return handlerExec ( 'serve://custom/script.js' , {
97
123
endpoint : 'custom' ,
@@ -101,22 +127,26 @@ describe('handler', () => {
101
127
} ) ;
102
128
103
129
it ( 'respects indexPath for missing files' , ( ) => {
130
+ mockDirs . push ( 'bar' ) ;
104
131
mockFiles . push ( join ( 'foo' , 'bar.html' ) ) ;
105
132
return handlerExec ( 'serve://dist/missing.js' , {
133
+ cwd : 'bar' ,
106
134
indexPath : join ( 'foo' , 'bar.html' ) ,
107
135
} ) . then ( path => {
108
136
assert . equal ( path , join ( 'foo' , 'bar.html' ) ) ;
109
137
} ) ;
110
138
} ) ;
111
139
112
140
it ( 'ignores hashes' , ( ) => {
141
+ mockDirs . push ( '.' ) ;
113
142
mockFiles . push ( 'script.js' ) ;
114
143
return handlerExec ( 'serve://dist/script.js#hash' ) . then ( path => {
115
144
assert . equal ( path , 'script.js' ) ;
116
145
} ) ;
117
146
} ) ;
118
147
119
148
it ( 'ignores query params' , ( ) => {
149
+ mockDirs . push ( '.' ) ;
120
150
mockFiles . push ( 'script.js' ) ;
121
151
return handlerExec ( 'serve://dist/script.js?query=param' ) . then ( path => {
122
152
assert . equal ( path , 'script.js' ) ;
0 commit comments