forked from senchalabs/connect
/
static.js
329 lines (280 loc) · 7.95 KB
/
static.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
var connect = require('../');
var fixtures = __dirname + '/fixtures';
var app = connect();
app.use(connect.static(fixtures));
app.use(function(req, res){
res.statusCode = 404;
res.end('sorry!');
});
describe('connect.static()', function(){
it('should serve static files', function(done){
app.request()
.get('/todo.txt')
.expect('- groceries', done);
})
it('should support nesting', function(done){
app.request()
.get('/users/tobi.txt')
.expect('ferret', done);
})
it('should set Content-Type', function(done){
app.request()
.get('/todo.txt')
.expect('Content-Type', 'text/plain; charset=UTF-8', done);
})
it('should default max-age=0', function(done){
app.request()
.get('/todo.txt')
.expect('Cache-Control', 'public, max-age=0', done);
})
it('should support urlencoded pathnames', function(done){
app.request()
.get('/foo%20bar')
.expect('baz', done);
})
it('should redirect directories', function(done){
app.request()
.get('/users')
.expect(301, done);
})
it('should support index.html', function(done){
app.request()
.get('/users/')
.end(function(res){
res.body.should.equal('<p>tobi, loki, jane</p>');
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
done();
})
})
it('should support ../', function(done){
app.request()
.get('/users/../todo.txt')
.expect('- groceries', done);
})
it('should support HEAD', function(done){
app.request()
.head('/todo.txt')
.expect('', done);
})
it('should support conditional requests', function(done){
app.request()
.get('/todo.txt')
.end(function(res){
app.request()
.get('/todo.txt')
.set('If-None-Match', res.headers.etag)
.expect(304, done);
});
})
describe('hidden files', function(){
it('should be ignored by default', function(done){
app.request()
.get('/.hidden')
.expect(404, done);
})
it('should be served when hidden: true is given', function(done){
var app = connect();
app.use(connect.static(fixtures, { hidden: true }));
app.request()
.get('/.hidden')
.expect('I am hidden', done);
})
})
describe('maxAge', function(){
it('should be 0 by default', function(done){
app.request()
.get('/todo.txt')
.end(function(res){
res.should.have.header('cache-control', 'public, max-age=0');
done();
});
})
it('should be reasonable when infinite', function(done){
var app = connect();
app.use(connect.static(fixtures, { maxAge: Infinity }));
app.request()
.get('/todo.txt')
.end(function(res){
res.should.have.header('cache-control', 'public, max-age=' + 60*60*24*365);
done();
});
})
})
describe('when traversing passed root', function(){
it('should respond with 403 Forbidden', function(done){
app.request()
.get('/users/../../todo.txt')
.expect(403, done);
})
it('should catch urlencoded ../', function(done){
app.request()
.get('/users/%2e%2e/%2e%2e/todo.txt')
.expect(403, done);
})
})
describe('on ENOENT', function(){
it('should next()', function(done){
app.request()
.get('/does-not-exist')
.end(function(res){
res.should.have.status(404);
res.body.should.equal('sorry!');
done();
});
})
})
describe('Range', function(){
it('should support byte ranges', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=0-4')
.expect('12345', done);
})
it('should be inclusive', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=0-0')
.expect('1', done);
})
it('should set Content-Range', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=2-5')
.expect('Content-Range', 'bytes 2-5/9', done);
})
it('should support -n', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=-3')
.expect('789', done);
})
it('should support n-', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=3-')
.expect('456789', done);
})
it('should respond with 206 "Partial Content"', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=0-4')
.expect(206, done);
})
it('should set Content-Length to the # of octets transferred', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=2-3')
.end(function(res){
res.body.should.equal('34');
res.headers['content-length'].should.equal('2');
done();
});
})
describe('when last-byte-pos of the range is greater than current length', function(){
it('is taken to be equal to one less than the current length', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=2-50')
.expect('Content-Range', 'bytes 2-8/9', done)
})
it('should adapt the Content-Length accordingly', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=2-50')
.end(function(res){
res.headers['content-length'].should.equal('7');
done();
});
})
})
describe('when the first- byte-pos of the range is greater than the current length', function(){
it('should respond with 416', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=9-50')
.expect(416, done);
})
it('should include a Content-Range field with a byte-range- resp-spec of "*" and an instance-length specifying the current length', function(done){
app.request()
.get('/nums')
.set('Range', 'bytes=9-50')
.expect('Content-Range', 'bytes */9', done)
})
})
describe('when syntactically invalid', function(){
it('should respond with 200 and the entire contents', function(done){
app.request()
.get('/nums')
.set('Range', 'asdf')
.expect('123456789', done);
})
})
})
describe('when a trailing backslash is given', function(){
it('should 500', function(done){
app.request()
.get('/todo.txt\\')
.expect(500, done);
})
})
describe('with a malformed URL', function(){
it('should respond with 400', function(done){
app.request()
.get('/%')
.expect(400, done)
});
})
describe('on ENAMETOOLONG', function(){
it('should next()', function(done){
var path = Array(100).join('foobar');
app.request()
.get('/' + path)
.expect(404, done);
})
})
describe('on ENOTDIR', function(){
it('should next()', function(done) {
app.request()
.get('/todo.txt/a.php')
.expect(404, done);
})
})
describe('when mounted', function(){
it('should redirect relative to the originalUrl', function(done){
var app = connect();
app.use('/static', connect.static('test/fixtures'));
app.request()
.get('/static/users')
.end(function(res){
res.headers.location.should.equal('/static/users/');
res.should.have.status(301);
done();
});
})
})
describe('when responding non-2xx or 304', function(){
it('should respond as-is', function(done){
var app = connect();
var n = 0;
app.use(function(req, res, next){
switch (n++) {
case 0: return next();
case 1: res.statusCode = 500; return next();
}
});
app.use(connect.static(fixtures));
app.request()
.get('/todo.txt')
.end(function(res){
app.request()
.get('/todo.txt')
.set('If-None-Match', res.headers.etag)
.end(function(res){
res.should.have.status(500);
res.body.should.equal('- groceries');
done();
})
})
})
})
})