@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.
@@ -4,75 +4,68 @@
4
4
 
5
5
  const fs = require('node:fs')
6
6
  const path = require('node:path')
7
- const t = require('tap')
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(mock, options)
21
- t.teardown(fastify.close.bind(fastify))
22
- fastify.listen({ port: 0 }, err => {
23
- t.error(err)
24
- fastify.server.unref()
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 (error) {}
26
+ } catch {}
34
27
 
35
- t.test('throws when `root` is an array', t => {
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.type(err, TypeError)
40
- t.equal(err.message, 'multi-root with list option is not supported')
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
- t.test('throws when `list.format` option is invalid', t => {
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.type(err, TypeError)
48
- t.equal(err.message, 'The `list.format` option must be json or html')
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
- t.test('throws when `list.names option` is not an array', t => {
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.type(err, TypeError)
56
- t.equal(err.message, 'The `list.names` option must be an array')
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
- t.test('throws when `list.jsonFormat` option is invalid', t => {
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.type(err, TypeError)
64
- t.equal(err.message, 'The `list.jsonFormat` option must be name or extended')
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
- t.test('throws when `list.format` is html and `list render` is not a function', t => {
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.type(err, TypeError)
72
- t.equal(err.message, 'The `list.render` option must be a function and is required with html format')
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
- t.test('dir list wrong options', t => {
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 }, err => {
114
- t.equal(err.message, case_.error.message)
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
- t.test('dir list default options', t => {
121
- t.plan(2)
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
- simple.concat({
135
- method: 'GET',
136
- url: url + route
137
- }, (err, response, body) => {
138
- t.error(err)
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
- t.test('dir list, custom options', t => {
147
- t.plan(2)
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
- simple.concat({
163
- method: 'GET',
164
- url: url + route
165
- }, (err, response, body) => {
166
- t.error(err)
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
- t.test('dir list, custom options with empty array index', t => {
175
- t.plan(2)
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
- simple.concat({
191
- method: 'GET',
192
- url: url + route
193
- }, (err, response, body) => {
194
- t.error(err)
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
- t.test('dir list html format', t => {
203
- t.plan(3)
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
- simple.concat({
235
- method: 'GET',
236
- url: url + route
237
- }, (err, response, body) => {
238
- t.error(err)
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
- t.test('dir list href nested structure', t => {
264
- t.plan(6)
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, files) {
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
- simple.concat({
291
- method: 'GET',
292
- url: url + route.path
293
- }, (err, response, body) => {
294
- t.error(err)
295
- t.equal(response.statusCode, 200)
296
- t.equal(body.toString(), route.response)
297
- simple.concat({
298
- method: 'GET',
299
- url: url + body.toString()
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
- t.test('dir list html format - stats', t => {
311
- t.plan(7)
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
- simple.concat({
339
- method: 'GET',
340
- url: url + route
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
- t.test('dir list html format - extended info', t => {
349
- t.plan(4)
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, files) {
361
- t.test('dirs', t => {
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.equal(typeof value.extendedInfo.fileCount, 'number')
368
- t.equal(typeof value.extendedInfo.totalFileCount, 'number')
369
- t.equal(typeof value.extendedInfo.folderCount, 'number')
370
- t.equal(typeof value.extendedInfo.totalFolderCount, 'number')
371
- t.equal(typeof value.extendedInfo.totalSize, 'number')
372
- t.equal(typeof value.extendedInfo.lastModified, 'number')
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
- simple.concat({
381
- method: 'GET',
382
- url: url + route
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
- t.test('dir list json format', t => {
391
- t.plan(2)
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
- simple.concat({
410
- method: 'GET',
411
- url: url + route
412
- }, (err, response, body) => {
413
- t.error(err)
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
- t.test('dir list json format - extended info', t => {
423
- t.plan(2)
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
- simple.concat({
444
- method: 'GET',
445
- url: url + route
446
- }, (err, response, body) => {
447
- t.error(err)
448
- t.equal(response.statusCode, 200)
449
- const bodyObject = JSON.parse(body.toString())
450
- t.equal(bodyObject.dirs[0].name, 'empty')
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
- t.test('json format with url parameter format', t => {
460
- t.plan(13)
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 (dirs, files) {
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
- simple.concat({
478
- method: 'GET',
479
- url: url + route
480
- }, (err, response, body) => {
481
- t.error(err)
482
- t.equal(response.statusCode, 200)
483
- t.equal(body.toString(), JSON.stringify(jsonContent))
484
- t.ok(response.headers['content-type'].includes('application/json'))
485
- })
486
-
487
- simple.concat({
488
- method: 'GET',
489
- url: url + route + '?format=html'
490
- }, (err, response, body) => {
491
- t.error(err)
492
- t.equal(response.statusCode, 200)
493
- t.equal(body.toString(), 'html')
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
- t.test('json format with url parameter format and without render option', t => {
510
- t.plan(12)
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
- simple.concat({
525
- method: 'GET',
526
- url: url + route
527
- }, (err, response, body) => {
528
- t.error(err)
529
- t.equal(response.statusCode, 200)
530
- t.equal(body.toString(), JSON.stringify(jsonContent))
531
- t.ok(response.headers['content-type'].includes('application/json'))
532
- })
533
-
534
- simple.concat({
535
- method: 'GET',
536
- url: url + route + '?format=html'
537
- }, (err, response, body) => {
538
- t.error(err)
539
- t.equal(response.statusCode, 500)
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
- t.test('html format with url parameter format', t => {
556
- t.plan(13)
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 (dirs, files) {
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
- simple.concat({
574
- method: 'GET',
575
- url: url + route
576
- }, (err, response, body) => {
577
- t.error(err)
578
- t.equal(response.statusCode, 200)
579
- t.equal(body.toString(), 'html')
580
- t.ok(response.headers['content-type'].includes('text/html'))
581
- })
582
-
583
- simple.concat({
584
- method: 'GET',
585
- url: url + route + '?format=html'
586
- }, (err, response, body) => {
587
- t.error(err)
588
- t.equal(response.statusCode, 200)
589
- t.equal(body.toString(), 'html')
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
- t.test('dir list on empty dir', t => {
606
- t.plan(2)
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
- simple.concat({
620
- method: 'GET',
621
- url: url + route
622
- }, (err, response, body) => {
623
- t.error(err)
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
- t.test('dir list serve index.html on index option', t => {
632
- t.plan(2)
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
- let route = '/public/index.html'
650
-
651
- simple.concat({
652
- method: 'GET',
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
- route = '/public/index'
661
- simple.concat({
662
- method: 'GET',
663
- url: url + route
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
- t.test('serve a non existent dir and get error', t => {
674
- t.plan(2)
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
- simple.concat({
687
- method: 'GET',
688
- url: url + route
689
- }, (err, response, body) => {
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
- t.test('serve a non existent dir and get error', t => {
698
- t.plan(2)
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
- simple.concat({
713
- method: 'GET',
714
- url: url + route
715
- }, (err, response, body) => {
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
- t.test('dir list with dotfiles allow option', t => {
724
- t.plan(2)
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
- simple.concat({
740
- method: 'GET',
741
- url: url + route
742
- }, (err, response, body) => {
743
- t.error(err)
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
- t.test('dir list with dotfiles deny option', t => {
752
- t.plan(2)
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
- simple.concat({
768
- method: 'GET',
769
- url: url + route
770
- }, (err, response, body) => {
771
- t.error(err)
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
- t.test('dir list with dotfiles ignore option', t => {
780
- t.plan(2)
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
- simple.concat({
796
- method: 'GET',
797
- url: url + route
798
- }, (err, response, body) => {
799
- t.error(err)
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
- t.test('dir list error', t => {
808
- t.plan(7)
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
- const mock = t.mockRequire('..', {
826
- '../lib/dirList.js': dirList
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.arrangeModule(t, options, mock, (url) => {
730
+ await helper.arrange(t, options, async (url) => {
832
731
  for (const route of routes) {
833
- simple.concat({
834
- method: 'GET',
835
- url: url + route
836
- }, (err, response, body) => {
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
  })