@fastify/static 8.0.3 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +1 -1
- package/README.md +92 -86
- package/eslint.config.js +1 -0
- package/index.js +4 -3
- package/lib/dirList.js +1 -1
- package/package.json +36 -7
- package/test/content-type.test.js +97 -127
- package/test/dir-list.test.js +264 -369
- package/test/static.test.js +1871 -2360
- package/types/index.d.ts +2 -1
- package/types/index.test-d.ts +22 -21
- package/.eslintrc.json +0 -31
- package/.taprc +0 -2
- package/test/static/shallow/no-link +0 -1
- package/test/static-symbolic-link/dir/symlink +0 -1
package/test/dir-list.test.js
CHANGED
|
@@ -4,75 +4,68 @@
|
|
|
4
4
|
|
|
5
5
|
const fs = require('node:fs')
|
|
6
6
|
const path = require('node:path')
|
|
7
|
-
const
|
|
8
|
-
const simple = require('simple-get')
|
|
7
|
+
const { test } = require('node:test')
|
|
9
8
|
const Fastify = require('fastify')
|
|
10
9
|
|
|
11
10
|
const fastifyStatic = require('..')
|
|
12
11
|
const dirList = require('../lib/dirList')
|
|
13
12
|
|
|
14
13
|
const helper = {
|
|
15
|
-
arrange: function (t, options, f) {
|
|
16
|
-
return helper.arrangeModule(t, options, fastifyStatic, f)
|
|
17
|
-
},
|
|
18
|
-
arrangeModule: function (t, options, mock, f) {
|
|
14
|
+
arrange: async function (t, options, f) {
|
|
19
15
|
const fastify = Fastify()
|
|
20
|
-
fastify.register(
|
|
21
|
-
t.
|
|
22
|
-
fastify.listen({ port: 0 }
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
f('http://localhost:' + fastify.server.address().port)
|
|
26
|
-
})
|
|
27
|
-
return f
|
|
16
|
+
fastify.register(fastifyStatic, options)
|
|
17
|
+
t.after(() => fastify.close())
|
|
18
|
+
await fastify.listen({ port: 0 })
|
|
19
|
+
fastify.server.unref()
|
|
20
|
+
await f('http://localhost:' + fastify.server.address().port)
|
|
28
21
|
}
|
|
29
22
|
}
|
|
30
23
|
|
|
31
24
|
try {
|
|
32
25
|
fs.mkdirSync(path.join(__dirname, 'static/shallow/empty'))
|
|
33
|
-
} catch
|
|
26
|
+
} catch {}
|
|
34
27
|
|
|
35
|
-
|
|
28
|
+
test('throws when `root` is an array', t => {
|
|
36
29
|
t.plan(2)
|
|
37
30
|
|
|
38
31
|
const err = dirList.validateOptions({ root: ['hello', 'world'], list: true })
|
|
39
|
-
t.
|
|
40
|
-
t.
|
|
32
|
+
t.assert.ok(err instanceof TypeError)
|
|
33
|
+
t.assert.deepStrictEqual(err.message, 'multi-root with list option is not supported')
|
|
41
34
|
})
|
|
42
35
|
|
|
43
|
-
|
|
36
|
+
test('throws when `list.format` option is invalid', t => {
|
|
44
37
|
t.plan(2)
|
|
45
38
|
|
|
46
39
|
const err = dirList.validateOptions({ list: { format: 'hello' } })
|
|
47
|
-
t.
|
|
48
|
-
t.
|
|
40
|
+
t.assert.ok(err instanceof TypeError)
|
|
41
|
+
t.assert.deepStrictEqual(err.message, 'The `list.format` option must be json or html')
|
|
49
42
|
})
|
|
50
43
|
|
|
51
|
-
|
|
44
|
+
test('throws when `list.names option` is not an array', t => {
|
|
52
45
|
t.plan(2)
|
|
53
46
|
|
|
54
47
|
const err = dirList.validateOptions({ list: { names: 'hello' } })
|
|
55
|
-
t.
|
|
56
|
-
t.
|
|
48
|
+
t.assert.ok(err instanceof TypeError)
|
|
49
|
+
t.assert.deepStrictEqual(err.message, 'The `list.names` option must be an array')
|
|
57
50
|
})
|
|
58
51
|
|
|
59
|
-
|
|
52
|
+
test('throws when `list.jsonFormat` option is invalid', t => {
|
|
60
53
|
t.plan(2)
|
|
61
54
|
|
|
62
55
|
const err = dirList.validateOptions({ list: { jsonFormat: 'hello' } })
|
|
63
|
-
t.
|
|
64
|
-
t.
|
|
56
|
+
t.assert.ok(err instanceof TypeError)
|
|
57
|
+
t.assert.deepStrictEqual(err.message, 'The `list.jsonFormat` option must be name or extended')
|
|
65
58
|
})
|
|
66
59
|
|
|
67
|
-
|
|
60
|
+
test('throws when `list.format` is html and `list render` is not a function', t => {
|
|
68
61
|
t.plan(2)
|
|
69
62
|
|
|
70
63
|
const err = dirList.validateOptions({ list: { format: 'html', render: 'hello' } })
|
|
71
|
-
t.
|
|
72
|
-
t.
|
|
64
|
+
t.assert.ok(err instanceof TypeError)
|
|
65
|
+
t.assert.deepStrictEqual(err.message, 'The `list.render` option must be a function and is required with html format')
|
|
73
66
|
})
|
|
74
67
|
|
|
75
|
-
|
|
68
|
+
test('dir list wrong options', async t => {
|
|
76
69
|
t.plan(3)
|
|
77
70
|
|
|
78
71
|
const cases = [
|
|
@@ -110,15 +103,13 @@ t.test('dir list wrong options', t => {
|
|
|
110
103
|
for (const case_ of cases) {
|
|
111
104
|
const fastify = Fastify()
|
|
112
105
|
fastify.register(fastifyStatic, case_.options)
|
|
113
|
-
fastify.listen({ port: 0 },
|
|
114
|
-
|
|
115
|
-
fastify.server.unref()
|
|
116
|
-
})
|
|
106
|
+
await t.assert.rejects(fastify.listen({ port: 0 }), new TypeError(case_.error.message))
|
|
107
|
+
fastify.server.unref()
|
|
117
108
|
}
|
|
118
109
|
})
|
|
119
110
|
|
|
120
|
-
|
|
121
|
-
t.plan(
|
|
111
|
+
test('dir list default options', async t => {
|
|
112
|
+
t.plan(1)
|
|
122
113
|
|
|
123
114
|
const options = {
|
|
124
115
|
root: path.join(__dirname, '/static'),
|
|
@@ -128,23 +119,20 @@ t.test('dir list default options', t => {
|
|
|
128
119
|
const route = '/public/shallow'
|
|
129
120
|
const content = { dirs: ['empty'], files: ['sample.jpg'] }
|
|
130
121
|
|
|
131
|
-
helper.arrange(t, options, (url) => {
|
|
132
|
-
t.test(route, t => {
|
|
122
|
+
await helper.arrange(t, options, async (url) => {
|
|
123
|
+
await t.test(route, async t => {
|
|
133
124
|
t.plan(3)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
t.equal(response.statusCode, 200)
|
|
140
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
141
|
-
})
|
|
125
|
+
|
|
126
|
+
const response = await fetch(url + route)
|
|
127
|
+
t.assert.ok(response.ok)
|
|
128
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
129
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
142
130
|
})
|
|
143
131
|
})
|
|
144
132
|
})
|
|
145
133
|
|
|
146
|
-
|
|
147
|
-
t.plan(
|
|
134
|
+
test('dir list, custom options', async t => {
|
|
135
|
+
t.plan(1)
|
|
148
136
|
|
|
149
137
|
const options = {
|
|
150
138
|
root: path.join(__dirname, '/static'),
|
|
@@ -156,23 +144,20 @@ t.test('dir list, custom options', t => {
|
|
|
156
144
|
const route = '/public/'
|
|
157
145
|
const content = { dirs: ['deep', 'shallow'], files: ['.example', '100%.txt', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }
|
|
158
146
|
|
|
159
|
-
helper.arrange(t, options, (url) => {
|
|
160
|
-
t.test(route, t => {
|
|
147
|
+
await helper.arrange(t, options, async (url) => {
|
|
148
|
+
await t.test(route, async t => {
|
|
161
149
|
t.plan(3)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
t.equal(response.statusCode, 200)
|
|
168
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
169
|
-
})
|
|
150
|
+
|
|
151
|
+
const response = await fetch(url + route)
|
|
152
|
+
t.assert.ok(response.ok)
|
|
153
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
154
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
170
155
|
})
|
|
171
156
|
})
|
|
172
157
|
})
|
|
173
158
|
|
|
174
|
-
|
|
175
|
-
t.plan(
|
|
159
|
+
test('dir list, custom options with empty array index', async t => {
|
|
160
|
+
t.plan(1)
|
|
176
161
|
|
|
177
162
|
const options = {
|
|
178
163
|
root: path.join(__dirname, '/static'),
|
|
@@ -184,23 +169,20 @@ t.test('dir list, custom options with empty array index', t => {
|
|
|
184
169
|
const route = '/public/'
|
|
185
170
|
const content = { dirs: ['deep', 'shallow'], files: ['.example', '100%.txt', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }
|
|
186
171
|
|
|
187
|
-
helper.arrange(t, options, (url) => {
|
|
188
|
-
t.test(route, t => {
|
|
172
|
+
await helper.arrange(t, options, async (url) => {
|
|
173
|
+
await t.test(route, async t => {
|
|
189
174
|
t.plan(3)
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
t.equal(response.statusCode, 200)
|
|
196
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
197
|
-
})
|
|
175
|
+
|
|
176
|
+
const response = await fetch(url + route)
|
|
177
|
+
t.assert.ok(response.ok)
|
|
178
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
179
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
198
180
|
})
|
|
199
181
|
})
|
|
200
182
|
})
|
|
201
183
|
|
|
202
|
-
|
|
203
|
-
t.plan(
|
|
184
|
+
test('dir list html format', async t => {
|
|
185
|
+
t.plan(2)
|
|
204
186
|
|
|
205
187
|
const options = {
|
|
206
188
|
root: path.join(__dirname, '/static'),
|
|
@@ -227,17 +209,15 @@ t.test('dir list html format', t => {
|
|
|
227
209
|
|
|
228
210
|
// check all routes by names
|
|
229
211
|
|
|
230
|
-
helper.arrange(t, options, (url) => {
|
|
212
|
+
await helper.arrange(t, options, async (url) => {
|
|
231
213
|
for (const route of routes) {
|
|
232
|
-
t.test(route, t => {
|
|
214
|
+
await t.test(route, async t => {
|
|
233
215
|
t.plan(3)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
t.equal(response.statusCode, 200)
|
|
240
|
-
t.equal(body.toString(), `
|
|
216
|
+
|
|
217
|
+
const response = await fetch(url + route)
|
|
218
|
+
t.assert.ok(response.ok)
|
|
219
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
220
|
+
t.assert.deepStrictEqual(await response.text(), `
|
|
241
221
|
<html><body>
|
|
242
222
|
<ul>
|
|
243
223
|
<li><a href="/public/deep">deep</a></li>
|
|
@@ -254,14 +234,13 @@ t.test('dir list html format', t => {
|
|
|
254
234
|
</ul>
|
|
255
235
|
</body></html>
|
|
256
236
|
`)
|
|
257
|
-
})
|
|
258
237
|
})
|
|
259
238
|
}
|
|
260
239
|
})
|
|
261
240
|
})
|
|
262
241
|
|
|
263
|
-
|
|
264
|
-
t.plan(
|
|
242
|
+
test('dir list href nested structure', async t => {
|
|
243
|
+
t.plan(5)
|
|
265
244
|
|
|
266
245
|
const options = {
|
|
267
246
|
root: path.join(__dirname, '/static'),
|
|
@@ -270,7 +249,7 @@ t.test('dir list href nested structure', t => {
|
|
|
270
249
|
list: {
|
|
271
250
|
format: 'html',
|
|
272
251
|
names: ['index', 'index.htm'],
|
|
273
|
-
render (dirs
|
|
252
|
+
render (dirs) {
|
|
274
253
|
return dirs[0].href
|
|
275
254
|
}
|
|
276
255
|
}
|
|
@@ -283,32 +262,27 @@ t.test('dir list href nested structure', t => {
|
|
|
283
262
|
{ path: '/public/deep/index.htm', response: '/public/deep/path' },
|
|
284
263
|
{ path: '/public/deep/path/', response: '/public/deep/path/for' }
|
|
285
264
|
]
|
|
286
|
-
helper.arrange(t, options, (url) => {
|
|
265
|
+
await helper.arrange(t, options, async (url) => {
|
|
287
266
|
for (const route of routes) {
|
|
288
|
-
t.test(route.path, t => {
|
|
267
|
+
await t.test(route.path, async t => {
|
|
289
268
|
t.plan(5)
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}, (err, response, body) => {
|
|
301
|
-
t.error(err)
|
|
302
|
-
t.equal(response.statusCode, 200)
|
|
303
|
-
})
|
|
304
|
-
})
|
|
269
|
+
|
|
270
|
+
const response = await fetch(url + route.path)
|
|
271
|
+
t.assert.ok(response.ok)
|
|
272
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
273
|
+
const responseContent = await response.text()
|
|
274
|
+
t.assert.deepStrictEqual(responseContent, route.response)
|
|
275
|
+
|
|
276
|
+
const response2 = await fetch(url + responseContent)
|
|
277
|
+
t.assert.ok(response2.ok)
|
|
278
|
+
t.assert.deepStrictEqual(response2.status, 200)
|
|
305
279
|
})
|
|
306
280
|
}
|
|
307
281
|
})
|
|
308
282
|
})
|
|
309
283
|
|
|
310
|
-
|
|
311
|
-
t.plan(
|
|
284
|
+
test('dir list html format - stats', async t => {
|
|
285
|
+
t.plan(6)
|
|
312
286
|
|
|
313
287
|
const options1 = {
|
|
314
288
|
root: path.join(__dirname, '/static'),
|
|
@@ -317,15 +291,14 @@ t.test('dir list html format - stats', t => {
|
|
|
317
291
|
list: {
|
|
318
292
|
format: 'html',
|
|
319
293
|
render (dirs, files) {
|
|
320
|
-
t.ok(dirs.length > 0)
|
|
321
|
-
t.ok(files.length > 0)
|
|
294
|
+
t.assert.ok(dirs.length > 0)
|
|
295
|
+
t.assert.ok(files.length > 0)
|
|
322
296
|
|
|
323
|
-
t.ok(dirs.every(every))
|
|
324
|
-
t.ok(files.every(every))
|
|
297
|
+
t.assert.ok(dirs.every(every))
|
|
298
|
+
t.assert.ok(files.every(every))
|
|
325
299
|
|
|
326
300
|
function every (value) {
|
|
327
|
-
return value.stats &&
|
|
328
|
-
value.stats.atime &&
|
|
301
|
+
return value.stats?.atime &&
|
|
329
302
|
!value.extendedInfo
|
|
330
303
|
}
|
|
331
304
|
}
|
|
@@ -334,19 +307,15 @@ t.test('dir list html format - stats', t => {
|
|
|
334
307
|
|
|
335
308
|
const route = '/public/'
|
|
336
309
|
|
|
337
|
-
helper.arrange(t, options1, (url) => {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}, (err, response, body) => {
|
|
342
|
-
t.error(err)
|
|
343
|
-
t.equal(response.statusCode, 200)
|
|
344
|
-
})
|
|
310
|
+
await helper.arrange(t, options1, async (url) => {
|
|
311
|
+
const response = await fetch(url + route)
|
|
312
|
+
t.assert.ok(response.ok)
|
|
313
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
345
314
|
})
|
|
346
315
|
})
|
|
347
316
|
|
|
348
|
-
|
|
349
|
-
t.plan(
|
|
317
|
+
test('dir list html format - extended info', async t => {
|
|
318
|
+
t.plan(2)
|
|
350
319
|
|
|
351
320
|
const route = '/public/'
|
|
352
321
|
|
|
@@ -357,38 +326,34 @@ t.test('dir list html format - extended info', t => {
|
|
|
357
326
|
list: {
|
|
358
327
|
format: 'html',
|
|
359
328
|
extendedFolderInfo: true,
|
|
360
|
-
render (dirs
|
|
361
|
-
|
|
329
|
+
render (dirs) {
|
|
330
|
+
test('dirs', t => {
|
|
362
331
|
t.plan(dirs.length * 7)
|
|
363
332
|
|
|
364
333
|
for (const value of dirs) {
|
|
365
|
-
t.ok(value.extendedInfo)
|
|
366
|
-
|
|
367
|
-
t.
|
|
368
|
-
t.
|
|
369
|
-
t.
|
|
370
|
-
t.
|
|
371
|
-
t.
|
|
372
|
-
t.
|
|
334
|
+
t.assert.ok(value.extendedInfo)
|
|
335
|
+
|
|
336
|
+
t.assert.deepStrictEqual(typeof value.extendedInfo.fileCount, 'number')
|
|
337
|
+
t.assert.deepStrictEqual(typeof value.extendedInfo.totalFileCount, 'number')
|
|
338
|
+
t.assert.deepStrictEqual(typeof value.extendedInfo.folderCount, 'number')
|
|
339
|
+
t.assert.deepStrictEqual(typeof value.extendedInfo.totalFolderCount, 'number')
|
|
340
|
+
t.assert.deepStrictEqual(typeof value.extendedInfo.totalSize, 'number')
|
|
341
|
+
t.assert.deepStrictEqual(typeof value.extendedInfo.lastModified, 'number')
|
|
373
342
|
}
|
|
374
343
|
})
|
|
375
344
|
}
|
|
376
345
|
}
|
|
377
346
|
}
|
|
378
347
|
|
|
379
|
-
helper.arrange(t, options, (url) => {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}, (err, response, body) => {
|
|
384
|
-
t.error(err)
|
|
385
|
-
t.equal(response.statusCode, 200)
|
|
386
|
-
})
|
|
348
|
+
await helper.arrange(t, options, async (url) => {
|
|
349
|
+
const response = await fetch(url + route)
|
|
350
|
+
t.assert.ok(response.ok)
|
|
351
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
387
352
|
})
|
|
388
353
|
})
|
|
389
354
|
|
|
390
|
-
|
|
391
|
-
t.plan(
|
|
355
|
+
test('dir list json format', async t => {
|
|
356
|
+
t.plan(1)
|
|
392
357
|
|
|
393
358
|
const options = {
|
|
394
359
|
root: path.join(__dirname, '/static'),
|
|
@@ -402,25 +367,22 @@ t.test('dir list json format', t => {
|
|
|
402
367
|
const routes = ['/public/shallow/']
|
|
403
368
|
const content = { dirs: ['empty'], files: ['sample.jpg'] }
|
|
404
369
|
|
|
405
|
-
helper.arrange(t, options, (url) => {
|
|
370
|
+
await helper.arrange(t, options, async (url) => {
|
|
406
371
|
for (const route of routes) {
|
|
407
|
-
t.test(route, t => {
|
|
372
|
+
await t.test(route, async t => {
|
|
408
373
|
t.plan(3)
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
t.equal(response.statusCode, 200)
|
|
415
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
416
|
-
})
|
|
374
|
+
|
|
375
|
+
const response = await fetch(url + route)
|
|
376
|
+
t.assert.ok(response.ok)
|
|
377
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
378
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
417
379
|
})
|
|
418
380
|
}
|
|
419
381
|
})
|
|
420
382
|
})
|
|
421
383
|
|
|
422
|
-
|
|
423
|
-
t.plan(
|
|
384
|
+
test('dir list json format - extended info', async t => {
|
|
385
|
+
t.plan(1)
|
|
424
386
|
|
|
425
387
|
const options = {
|
|
426
388
|
root: path.join(__dirname, '/static'),
|
|
@@ -436,28 +398,25 @@ t.test('dir list json format - extended info', t => {
|
|
|
436
398
|
}
|
|
437
399
|
const routes = ['/public/shallow/']
|
|
438
400
|
|
|
439
|
-
helper.arrange(t, options, (url) => {
|
|
401
|
+
await helper.arrange(t, options, async (url) => {
|
|
440
402
|
for (const route of routes) {
|
|
441
|
-
t.test(route, t => {
|
|
403
|
+
await t.test(route, async t => {
|
|
442
404
|
t.plan(5)
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
t.equal(typeof bodyObject.dirs[0].stats.atimeMs, 'number')
|
|
452
|
-
t.equal(typeof bodyObject.dirs[0].extendedInfo.totalSize, 'number')
|
|
453
|
-
})
|
|
405
|
+
|
|
406
|
+
const response = await fetch(url + route)
|
|
407
|
+
t.assert.ok(response.ok)
|
|
408
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
409
|
+
const responseContent = await response.json()
|
|
410
|
+
t.assert.deepStrictEqual(responseContent.dirs[0].name, 'empty')
|
|
411
|
+
t.assert.deepStrictEqual(typeof responseContent.dirs[0].stats.atimeMs, 'number')
|
|
412
|
+
t.assert.deepStrictEqual(typeof responseContent.dirs[0].extendedInfo.totalSize, 'number')
|
|
454
413
|
})
|
|
455
414
|
}
|
|
456
415
|
})
|
|
457
416
|
})
|
|
458
417
|
|
|
459
|
-
|
|
460
|
-
t.plan(
|
|
418
|
+
test('json format with url parameter format', async t => {
|
|
419
|
+
t.plan(12)
|
|
461
420
|
|
|
462
421
|
const options = {
|
|
463
422
|
root: path.join(__dirname, '/static'),
|
|
@@ -465,7 +424,7 @@ t.test('json format with url parameter format', t => {
|
|
|
465
424
|
index: false,
|
|
466
425
|
list: {
|
|
467
426
|
format: 'json',
|
|
468
|
-
render (
|
|
427
|
+
render () {
|
|
469
428
|
return 'html'
|
|
470
429
|
}
|
|
471
430
|
}
|
|
@@ -473,41 +432,29 @@ t.test('json format with url parameter format', t => {
|
|
|
473
432
|
const route = '/public/'
|
|
474
433
|
const jsonContent = { dirs: ['deep', 'shallow'], files: ['.example', '100%.txt', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }
|
|
475
434
|
|
|
476
|
-
helper.arrange(t, options, (url) => {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
t.ok(response.headers['content-type'].includes('text/html'))
|
|
495
|
-
})
|
|
496
|
-
|
|
497
|
-
simple.concat({
|
|
498
|
-
method: 'GET',
|
|
499
|
-
url: url + route + '?format=json'
|
|
500
|
-
}, (err, response, body) => {
|
|
501
|
-
t.error(err)
|
|
502
|
-
t.equal(response.statusCode, 200)
|
|
503
|
-
t.equal(body.toString(), JSON.stringify(jsonContent))
|
|
504
|
-
t.ok(response.headers['content-type'].includes('application/json'))
|
|
505
|
-
})
|
|
435
|
+
await helper.arrange(t, options, async (url) => {
|
|
436
|
+
const response = await fetch(url + route)
|
|
437
|
+
t.assert.ok(response.ok)
|
|
438
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
439
|
+
t.assert.deepStrictEqual(await response.json(), jsonContent)
|
|
440
|
+
t.assert.ok(response.headers.get('content-type').includes('application/json'))
|
|
441
|
+
|
|
442
|
+
const response2 = await fetch(url + route + '?format=html')
|
|
443
|
+
t.assert.ok(response2.ok)
|
|
444
|
+
t.assert.deepStrictEqual(response2.status, 200)
|
|
445
|
+
t.assert.deepStrictEqual(await response2.text(), 'html')
|
|
446
|
+
t.assert.ok(response2.headers.get('content-type').includes('text/html'))
|
|
447
|
+
|
|
448
|
+
const response3 = await fetch(url + route + '?format=json')
|
|
449
|
+
t.assert.ok(response3.ok)
|
|
450
|
+
t.assert.deepStrictEqual(response3.status, 200)
|
|
451
|
+
t.assert.deepStrictEqual(await response3.json(), jsonContent)
|
|
452
|
+
t.assert.ok(response3.headers.get('content-type').includes('application/json'))
|
|
506
453
|
})
|
|
507
454
|
})
|
|
508
455
|
|
|
509
|
-
|
|
510
|
-
t.plan(
|
|
456
|
+
test('json format with url parameter format and without render option', async t => {
|
|
457
|
+
t.plan(11)
|
|
511
458
|
|
|
512
459
|
const options = {
|
|
513
460
|
root: path.join(__dirname, '/static'),
|
|
@@ -520,40 +467,28 @@ t.test('json format with url parameter format and without render option', t => {
|
|
|
520
467
|
const route = '/public/'
|
|
521
468
|
const jsonContent = { dirs: ['deep', 'shallow'], files: ['.example', '100%.txt', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }
|
|
522
469
|
|
|
523
|
-
helper.arrange(t, options, (url) => {
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
t.equal(JSON.parse(body.toString()).message, 'The `list.render` option must be a function and is required with the URL parameter `format=html`')
|
|
541
|
-
})
|
|
542
|
-
|
|
543
|
-
simple.concat({
|
|
544
|
-
method: 'GET',
|
|
545
|
-
url: url + route + '?format=json'
|
|
546
|
-
}, (err, response, body) => {
|
|
547
|
-
t.error(err)
|
|
548
|
-
t.equal(response.statusCode, 200)
|
|
549
|
-
t.equal(body.toString(), JSON.stringify(jsonContent))
|
|
550
|
-
t.ok(response.headers['content-type'].includes('application/json'))
|
|
551
|
-
})
|
|
470
|
+
await helper.arrange(t, options, async (url) => {
|
|
471
|
+
const response = await fetch(url + route)
|
|
472
|
+
t.assert.ok(response.ok)
|
|
473
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
474
|
+
t.assert.deepStrictEqual(await response.json(), jsonContent)
|
|
475
|
+
t.assert.ok(response.headers.get('content-type').includes('application/json'))
|
|
476
|
+
|
|
477
|
+
const response2 = await fetch(url + route + '?format=html')
|
|
478
|
+
t.assert.ok(!response2.ok)
|
|
479
|
+
t.assert.deepStrictEqual(response2.status, 500)
|
|
480
|
+
t.assert.deepStrictEqual((await response2.json()).message, 'The `list.render` option must be a function and is required with the URL parameter `format=html`')
|
|
481
|
+
|
|
482
|
+
const response3 = await fetch(url + route + '?format=json')
|
|
483
|
+
t.assert.ok(response3.ok)
|
|
484
|
+
t.assert.deepStrictEqual(response3.status, 200)
|
|
485
|
+
t.assert.deepStrictEqual(await response3.json(), jsonContent)
|
|
486
|
+
t.assert.ok(response3.headers.get('content-type').includes('application/json'))
|
|
552
487
|
})
|
|
553
488
|
})
|
|
554
489
|
|
|
555
|
-
|
|
556
|
-
t.plan(
|
|
490
|
+
test('html format with url parameter format', async t => {
|
|
491
|
+
t.plan(12)
|
|
557
492
|
|
|
558
493
|
const options = {
|
|
559
494
|
root: path.join(__dirname, '/static'),
|
|
@@ -561,7 +496,7 @@ t.test('html format with url parameter format', t => {
|
|
|
561
496
|
index: false,
|
|
562
497
|
list: {
|
|
563
498
|
format: 'html',
|
|
564
|
-
render (
|
|
499
|
+
render () {
|
|
565
500
|
return 'html'
|
|
566
501
|
}
|
|
567
502
|
}
|
|
@@ -569,41 +504,29 @@ t.test('html format with url parameter format', t => {
|
|
|
569
504
|
const route = '/public/'
|
|
570
505
|
const jsonContent = { dirs: ['deep', 'shallow'], files: ['.example', '100%.txt', 'a .md', 'foo.html', 'foobar.html', 'index.css', 'index.html'] }
|
|
571
506
|
|
|
572
|
-
helper.arrange(t, options, (url) => {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
t.ok(response.headers['content-type'].includes('text/html'))
|
|
591
|
-
})
|
|
592
|
-
|
|
593
|
-
simple.concat({
|
|
594
|
-
method: 'GET',
|
|
595
|
-
url: url + route + '?format=json'
|
|
596
|
-
}, (err, response, body) => {
|
|
597
|
-
t.error(err)
|
|
598
|
-
t.equal(response.statusCode, 200)
|
|
599
|
-
t.equal(body.toString(), JSON.stringify(jsonContent))
|
|
600
|
-
t.ok(response.headers['content-type'].includes('application/json'))
|
|
601
|
-
})
|
|
507
|
+
await helper.arrange(t, options, async (url) => {
|
|
508
|
+
const response = await fetch(url + route)
|
|
509
|
+
t.assert.ok(response.ok)
|
|
510
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
511
|
+
t.assert.deepStrictEqual(await response.text(), 'html')
|
|
512
|
+
t.assert.ok(response.headers.get('content-type').includes('text/html'))
|
|
513
|
+
|
|
514
|
+
const response2 = await fetch(url + route + '?format=html')
|
|
515
|
+
t.assert.ok(response2.ok)
|
|
516
|
+
t.assert.deepStrictEqual(response2.status, 200)
|
|
517
|
+
t.assert.deepStrictEqual(await response2.text(), 'html')
|
|
518
|
+
t.assert.ok(response2.headers.get('content-type').includes('text/html'))
|
|
519
|
+
|
|
520
|
+
const response3 = await fetch(url + route + '?format=json')
|
|
521
|
+
t.assert.ok(response3.ok)
|
|
522
|
+
t.assert.deepStrictEqual(response3.status, 200)
|
|
523
|
+
t.assert.deepStrictEqual(await response3.json(), jsonContent)
|
|
524
|
+
t.assert.ok(response3.headers.get('content-type').includes('application/json'))
|
|
602
525
|
})
|
|
603
526
|
})
|
|
604
527
|
|
|
605
|
-
|
|
606
|
-
t.plan(
|
|
528
|
+
test('dir list on empty dir', async t => {
|
|
529
|
+
t.plan(1)
|
|
607
530
|
|
|
608
531
|
const options = {
|
|
609
532
|
root: path.join(__dirname, '/static'),
|
|
@@ -613,23 +536,20 @@ t.test('dir list on empty dir', t => {
|
|
|
613
536
|
const route = '/public/shallow/empty'
|
|
614
537
|
const content = { dirs: [], files: [] }
|
|
615
538
|
|
|
616
|
-
helper.arrange(t, options, (url) => {
|
|
617
|
-
t.test(route, t => {
|
|
539
|
+
await helper.arrange(t, options, async (url) => {
|
|
540
|
+
await t.test(route, async t => {
|
|
618
541
|
t.plan(3)
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
t.equal(response.statusCode, 200)
|
|
625
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
626
|
-
})
|
|
542
|
+
|
|
543
|
+
const response = await fetch(url + route)
|
|
544
|
+
t.assert.ok(response.ok)
|
|
545
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
546
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
627
547
|
})
|
|
628
548
|
})
|
|
629
549
|
})
|
|
630
550
|
|
|
631
|
-
|
|
632
|
-
t.plan(
|
|
551
|
+
test('dir list serve index.html on index option', async t => {
|
|
552
|
+
t.plan(1)
|
|
633
553
|
|
|
634
554
|
const options = {
|
|
635
555
|
root: path.join(__dirname, '/static'),
|
|
@@ -642,36 +562,25 @@ t.test('dir list serve index.html on index option', t => {
|
|
|
642
562
|
}
|
|
643
563
|
}
|
|
644
564
|
|
|
645
|
-
helper.arrange(t, options, (url) => {
|
|
646
|
-
t.test('serve index.html from fs', t => {
|
|
565
|
+
await helper.arrange(t, options, async (url) => {
|
|
566
|
+
await t.test('serve index.html from fs', async t => {
|
|
647
567
|
t.plan(6)
|
|
648
568
|
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
url: url + route
|
|
654
|
-
}, (err, response, body) => {
|
|
655
|
-
t.error(err)
|
|
656
|
-
t.equal(response.statusCode, 200)
|
|
657
|
-
t.equal(body.toString(), '<html>\n <body>\n the body\n </body>\n</html>\n')
|
|
658
|
-
})
|
|
569
|
+
const response = await fetch(url + '/public/index.html')
|
|
570
|
+
t.assert.ok(response.ok)
|
|
571
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
572
|
+
t.assert.deepStrictEqual(await response.text(), '<html>\n <body>\n the body\n </body>\n</html>\n')
|
|
659
573
|
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
}, (err, response, body) => {
|
|
665
|
-
t.error(err)
|
|
666
|
-
t.equal(response.statusCode, 200)
|
|
667
|
-
t.equal(body.toString(), 'dir list index')
|
|
668
|
-
})
|
|
574
|
+
const response2 = await fetch(url + '/public/index')
|
|
575
|
+
t.assert.ok(response2.ok)
|
|
576
|
+
t.assert.deepStrictEqual(response2.status, 200)
|
|
577
|
+
t.assert.deepStrictEqual(await response2.text(), 'dir list index')
|
|
669
578
|
})
|
|
670
579
|
})
|
|
671
580
|
})
|
|
672
581
|
|
|
673
|
-
|
|
674
|
-
t.plan(
|
|
582
|
+
test('serve a non existent dir and get error', async t => {
|
|
583
|
+
t.plan(1)
|
|
675
584
|
|
|
676
585
|
const options = {
|
|
677
586
|
root: '/none',
|
|
@@ -680,22 +589,19 @@ t.test('serve a non existent dir and get error', t => {
|
|
|
680
589
|
}
|
|
681
590
|
const route = '/public/'
|
|
682
591
|
|
|
683
|
-
helper.arrange(t, options, (url) => {
|
|
684
|
-
t.test(route, t => {
|
|
592
|
+
await helper.arrange(t, options, async (url) => {
|
|
593
|
+
await t.test(route, async t => {
|
|
685
594
|
t.plan(2)
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
t.error(err)
|
|
691
|
-
t.equal(response.statusCode, 404)
|
|
692
|
-
})
|
|
595
|
+
|
|
596
|
+
const response = await fetch(url + route)
|
|
597
|
+
t.assert.ok(!response.ok)
|
|
598
|
+
t.assert.deepStrictEqual(response.status, 404)
|
|
693
599
|
})
|
|
694
600
|
})
|
|
695
601
|
})
|
|
696
602
|
|
|
697
|
-
|
|
698
|
-
t.plan(
|
|
603
|
+
test('serve a non existent dir and get error', async t => {
|
|
604
|
+
t.plan(1)
|
|
699
605
|
|
|
700
606
|
const options = {
|
|
701
607
|
root: path.join(__dirname, '/static'),
|
|
@@ -706,22 +612,19 @@ t.test('serve a non existent dir and get error', t => {
|
|
|
706
612
|
}
|
|
707
613
|
const route = '/public/none/index'
|
|
708
614
|
|
|
709
|
-
helper.arrange(t, options, (url) => {
|
|
710
|
-
t.test(route, t => {
|
|
615
|
+
await helper.arrange(t, options, async (url) => {
|
|
616
|
+
await t.test(route, async t => {
|
|
711
617
|
t.plan(2)
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
t.error(err)
|
|
717
|
-
t.equal(response.statusCode, 404)
|
|
718
|
-
})
|
|
618
|
+
|
|
619
|
+
const response = await fetch(url + route)
|
|
620
|
+
t.assert.ok(!response.ok)
|
|
621
|
+
t.assert.deepStrictEqual(response.status, 404)
|
|
719
622
|
})
|
|
720
623
|
})
|
|
721
624
|
})
|
|
722
625
|
|
|
723
|
-
|
|
724
|
-
t.plan(
|
|
626
|
+
test('dir list with dotfiles allow option', async t => {
|
|
627
|
+
t.plan(1)
|
|
725
628
|
|
|
726
629
|
const options = {
|
|
727
630
|
root: path.join(__dirname, '/static-dotfiles'),
|
|
@@ -733,23 +636,20 @@ t.test('dir list with dotfiles allow option', t => {
|
|
|
733
636
|
const route = '/public/'
|
|
734
637
|
const content = { dirs: ['dir'], files: ['.aaa', 'test.txt'] }
|
|
735
638
|
|
|
736
|
-
helper.arrange(t, options, (url) => {
|
|
737
|
-
t.test(route, t => {
|
|
639
|
+
await helper.arrange(t, options, async (url) => {
|
|
640
|
+
await t.test(route, async t => {
|
|
738
641
|
t.plan(3)
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
t.equal(response.statusCode, 200)
|
|
745
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
746
|
-
})
|
|
642
|
+
|
|
643
|
+
const response = await fetch(url + route)
|
|
644
|
+
t.assert.ok(response.ok)
|
|
645
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
646
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
747
647
|
})
|
|
748
648
|
})
|
|
749
649
|
})
|
|
750
650
|
|
|
751
|
-
|
|
752
|
-
t.plan(
|
|
651
|
+
test('dir list with dotfiles deny option', async t => {
|
|
652
|
+
t.plan(1)
|
|
753
653
|
|
|
754
654
|
const options = {
|
|
755
655
|
root: path.join(__dirname, '/static-dotfiles'),
|
|
@@ -761,23 +661,20 @@ t.test('dir list with dotfiles deny option', t => {
|
|
|
761
661
|
const route = '/public/'
|
|
762
662
|
const content = { dirs: ['dir'], files: ['test.txt'] }
|
|
763
663
|
|
|
764
|
-
helper.arrange(t, options, (url) => {
|
|
765
|
-
t.test(route, t => {
|
|
664
|
+
await helper.arrange(t, options, async (url) => {
|
|
665
|
+
await t.test(route, async t => {
|
|
766
666
|
t.plan(3)
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
t.equal(response.statusCode, 200)
|
|
773
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
774
|
-
})
|
|
667
|
+
|
|
668
|
+
const response = await fetch(url + route)
|
|
669
|
+
t.assert.ok(response.ok)
|
|
670
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
671
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
775
672
|
})
|
|
776
673
|
})
|
|
777
674
|
})
|
|
778
675
|
|
|
779
|
-
|
|
780
|
-
t.plan(
|
|
676
|
+
test('dir list with dotfiles ignore option', async t => {
|
|
677
|
+
t.plan(1)
|
|
781
678
|
|
|
782
679
|
const options = {
|
|
783
680
|
root: path.join(__dirname, '/static-dotfiles'),
|
|
@@ -789,23 +686,20 @@ t.test('dir list with dotfiles ignore option', t => {
|
|
|
789
686
|
const route = '/public/'
|
|
790
687
|
const content = { dirs: ['dir'], files: ['test.txt'] }
|
|
791
688
|
|
|
792
|
-
helper.arrange(t, options, (url) => {
|
|
793
|
-
t.test(route, t => {
|
|
689
|
+
await helper.arrange(t, options, async (url) => {
|
|
690
|
+
await t.test(route, async t => {
|
|
794
691
|
t.plan(3)
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
t.equal(response.statusCode, 200)
|
|
801
|
-
t.equal(body.toString(), JSON.stringify(content))
|
|
802
|
-
})
|
|
692
|
+
|
|
693
|
+
const response = await fetch(url + route)
|
|
694
|
+
t.assert.ok(response.ok)
|
|
695
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
696
|
+
t.assert.deepStrictEqual(await response.json(), content)
|
|
803
697
|
})
|
|
804
698
|
})
|
|
805
699
|
})
|
|
806
700
|
|
|
807
|
-
|
|
808
|
-
t.plan(
|
|
701
|
+
test('dir list error', async t => {
|
|
702
|
+
t.plan(6)
|
|
809
703
|
|
|
810
704
|
const options = {
|
|
811
705
|
root: path.join(__dirname, '/static'),
|
|
@@ -822,22 +716,23 @@ t.test('dir list error', t => {
|
|
|
822
716
|
const errorMessage = 'mocking send'
|
|
823
717
|
dirList.send = async () => { throw new Error(errorMessage) }
|
|
824
718
|
|
|
825
|
-
|
|
826
|
-
'../lib/dirList.js'
|
|
719
|
+
t.beforeEach((ctx) => {
|
|
720
|
+
ctx.initialDirList = ctx['../lib/dirList.js']
|
|
721
|
+
ctx['../lib/dirList.js'] = dirList
|
|
722
|
+
})
|
|
723
|
+
|
|
724
|
+
t.afterEach((ctx) => {
|
|
725
|
+
ctx['../lib/dirList.js'] = ctx.initialDirList
|
|
827
726
|
})
|
|
828
727
|
|
|
829
728
|
const routes = ['/public/', '/public/index.htm']
|
|
830
729
|
|
|
831
|
-
helper.
|
|
730
|
+
await helper.arrange(t, options, async (url) => {
|
|
832
731
|
for (const route of routes) {
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
t.error(err)
|
|
838
|
-
t.equal(JSON.parse(body.toString()).message, errorMessage)
|
|
839
|
-
t.equal(response.statusCode, 500)
|
|
840
|
-
})
|
|
732
|
+
const response = await fetch(url + route)
|
|
733
|
+
t.assert.ok(!response.ok)
|
|
734
|
+
t.assert.deepStrictEqual(response.status, 500)
|
|
735
|
+
t.assert.deepStrictEqual((await response.json()).message, errorMessage)
|
|
841
736
|
}
|
|
842
737
|
})
|
|
843
738
|
})
|