New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/send

Package Overview
Dependencies
Maintainers
20
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/send - npm Package Compare versions

Comparing version

to
4.0.0

test/fixtures/.mine/.hidden.txt

8

package.json
{
"name": "@fastify/send",
"description": "Better streaming static file server with Range and conditional-GET support",
"version": "3.3.1",
"version": "4.0.0",
"author": "TJ Holowaychuk <tj@vision-media.ca>",

@@ -63,6 +63,6 @@ "contributors": [

"benchmark": "^2.1.4",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"neostandard": "^0.12.0",
"supertest": "6.3.4",
"tap": "^21.0.0",
"tsd": "^0.31.0"

@@ -74,5 +74,5 @@ },

"test": "npm run test:unit && npm run test:typescript",
"test:coverage": "tap --coverage-report=html",
"test:coverage": "c8 --reporter html node --test",
"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},

@@ -79,0 +79,0 @@ "pre-commit": [

# @fastify/send
[![CI](https://github.com/fastify/send/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/send/actions/workflows/ci.yml)
[![CI](https://github.com/fastify/send/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/send/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/send.svg?style=flat)](https://www.npmjs.com/package/@fastify/send)

@@ -5,0 +5,0 @@ [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const { collapseLeadingSlashes } = require('../lib/collapseLeadingSlashes')

@@ -19,5 +19,5 @@

for (let i = 0; i < testCases.length; ++i) {
t.strictSame(collapseLeadingSlashes(testCases[i][0]), testCases[i][1])
for (const testCase of testCases) {
t.assert.deepStrictEqual(collapseLeadingSlashes(testCase[0]), testCase[1])
}
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const { containsDotFile } = require('../lib/containsDotFile')

@@ -15,5 +15,5 @@

for (let i = 0; i < testCases.length; ++i) {
t.strictSame(containsDotFile(testCases[i][0].split('/')), testCases[i][1], testCases[i][0])
for (const testCase of testCases) {
t.assert.deepStrictEqual(containsDotFile(testCase[0].split('/')), testCase[1], testCase[0])
}
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const { isUtf8MimeType } = require('../lib/isUtf8MimeType')

@@ -19,5 +19,5 @@

for (let i = 0; i < testCases.length; ++i) {
t.strictSame(isUtf8MimeType(testCases[i][0], 'test'), testCases[i][1])
for (const testCase of testCases) {
t.assert.deepStrictEqual(isUtf8MimeType(testCase[0], 'test'), testCase[1])
}
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const path = require('node:path')

@@ -11,11 +11,11 @@ const request = require('supertest')

test('send.mime', function (t) {
test('send.mime', async function (t) {
t.plan(2)
t.test('should be exposed', function (t) {
await t.test('should be exposed', function (t) {
t.plan(1)
t.ok(send.mime)
t.assert.ok(send.mime)
})
t.test('.default_type', function (t) {
await t.test('.default_type', async function (t) {
t.plan(3)

@@ -31,31 +31,28 @@

t.test('should change the default type', function (t) {
t.plan(1)
await t.test('should change the default type', async function (t) {
send.mime.default_type = 'text/plain'
request(createServer({ root: fixtures }))
await request(createServer({ root: fixtures }))
.get('/no_ext')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, err => t.error(err))
.expect(200)
})
t.test('should not add Content-Type for undefined default', function (t) {
t.plan(2)
await t.test('should not add Content-Type for undefined default', async function (t) {
t.plan(1)
send.mime.default_type = undefined
request(createServer({ root: fixtures }))
await request(createServer({ root: fixtures }))
.get('/no_ext')
.expect(shouldNotHaveHeader('Content-Type', t))
.expect(200, err => t.error(err))
.expect(200)
})
t.test('should return Content-Type without charset', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should return Content-Type without charset', async function (t) {
await request(createServer({ root: fixtures }))
.get('/images/node-js.png')
.expect('Content-Type', 'image/png')
.expect(200, err => t.error(err))
.expect(200)
})
})
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const { normalizeList } = require('../lib/normalizeList')

@@ -8,3 +8,3 @@

const testCases = [
[undefined, new Error('test must be array of strings or false')],
[undefined, new TypeError('test must be array of strings or false')],
[false, []],

@@ -16,15 +16,15 @@ [[], []],

['a', ['a']],
[true, new Error('test must be array of strings or false')],
[1, new Error('test must be array of strings or false')],
[[1], new Error('test must be array of strings or false')]
[true, new TypeError('test must be array of strings or false')],
[1, new TypeError('test must be array of strings or false')],
[[1], new TypeError('test must be array of strings or false')]
]
t.plan(testCases.length)
for (let i = 0; i < testCases.length; ++i) {
if (testCases[i][1] instanceof Error) {
t.throws(() => normalizeList(testCases[i][0], 'test'), testCases[i][1])
for (const testCase of testCases) {
if (testCase[1] instanceof Error) {
t.assert.throws(() => normalizeList(testCase[0], 'test'), testCase[1])
} else {
t.strictSame(normalizeList(testCases[i][0], 'test'), testCases[i][1])
t.assert.deepStrictEqual(normalizeList(testCase[0], 'test'), testCase[1])
}
}
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const { parseBytesRange } = require('../lib/parseBytesRange')
test('parseBytesRange', function (t) {
test('parseBytesRange', async function (t) {
t.plan(13)
t.test('should return empty array if all specified ranges are invalid', function (t) {
await t.test('should return empty array if all specified ranges are invalid', function (t) {
t.plan(3)
t.strictSame(parseBytesRange(200, 'bytes=500-20'), [])
t.strictSame(parseBytesRange(200, 'bytes=500-999'), [])
t.strictSame(parseBytesRange(200, 'bytes=500-999,1000-1499'), [])
t.assert.deepStrictEqual(parseBytesRange(200, 'bytes=500-20'), [])
t.assert.deepStrictEqual(parseBytesRange(200, 'bytes=500-999'), [])
t.assert.deepStrictEqual(parseBytesRange(200, 'bytes=500-999,1000-1499'), [])
})
t.test('should parse str', function (t) {
await t.test('should parse str', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=0-499')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 0, end: 499, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 0, end: 499, index: 0 })
})
t.test('should cap end at size', function (t) {
await t.test('should cap end at size', function (t) {
t.plan(2)
const range = parseBytesRange(200, 'bytes=0-499')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 0, end: 199, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 0, end: 199, index: 0 })
})
t.test('should parse str', function (t) {
await t.test('should parse str', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=40-80')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 40, end: 80, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 40, end: 80, index: 0 })
})
t.test('should parse str asking for last n bytes', function (t) {
await t.test('should parse str asking for last n bytes', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=-400')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 600, end: 999, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 600, end: 999, index: 0 })
})
t.test('should parse str with only start', function (t) {
await t.test('should parse str with only start', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=400-')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 400, end: 999, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 400, end: 999, index: 0 })
})
t.test('should parse "bytes=0-"', function (t) {
await t.test('should parse "bytes=0-"', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=0-')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 0, end: 999, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 0, end: 999, index: 0 })
})
t.test('should parse str with no bytes', function (t) {
await t.test('should parse str with no bytes', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=0-0')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 0, end: 0, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 0, end: 0, index: 0 })
})
t.test('should parse str asking for last byte', function (t) {
await t.test('should parse str asking for last byte', function (t) {
t.plan(2)
const range = parseBytesRange(1000, 'bytes=-1')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 999, end: 999, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 999, end: 999, index: 0 })
})
t.test('should parse str with some invalid ranges', function (t) {
await t.test('should parse str with some invalid ranges', function (t) {
t.plan(2)
const range = parseBytesRange(200, 'bytes=0-499,1000-,500-999')
t.equal(range.length, 1)
t.strictSame(range[0], { start: 0, end: 199, index: 0 })
t.assert.deepStrictEqual(range.length, 1)
t.assert.deepStrictEqual(range[0], { start: 0, end: 199, index: 0 })
})
t.test('should combine overlapping ranges', function (t) {
await t.test('should combine overlapping ranges', function (t) {
t.plan(3)
const range = parseBytesRange(150, 'bytes=0-4,90-99,5-75,100-199,101-102')
t.equal(range.length, 2)
t.strictSame(range[0], { start: 0, end: 75, index: 0 })
t.strictSame(range[1], { start: 90, end: 149, index: 1 })
t.assert.deepStrictEqual(range.length, 2)
t.assert.deepStrictEqual(range[0], { start: 0, end: 75, index: 0 })
t.assert.deepStrictEqual(range[1], { start: 90, end: 149, index: 1 })
})
t.test('should retain original order /1', function (t) {
await t.test('should retain original order /1', function (t) {
t.plan(3)
const range = parseBytesRange(150, 'bytes=90-99,5-75,100-199,101-102,0-4')
t.equal(range.length, 2)
t.strictSame(range[0], { start: 90, end: 149, index: 0 })
t.strictSame(range[1], { start: 0, end: 75, index: 1 })
t.assert.deepStrictEqual(range.length, 2)
t.assert.deepStrictEqual(range[0], { start: 90, end: 149, index: 0 })
t.assert.deepStrictEqual(range[1], { start: 0, end: 75, index: 1 })
})
t.test('should retain original order /2', function (t) {
await t.test('should retain original order /2', function (t) {
t.plan(4)
const range = parseBytesRange(150, 'bytes=-1,20-100,0-1,101-120')
t.equal(range.length, 3)
t.strictSame(range[0], { start: 149, end: 149, index: 0 })
t.strictSame(range[1], { start: 20, end: 120, index: 1 })
t.strictSame(range[2], { start: 0, end: 1, index: 2 })
t.assert.deepStrictEqual(range.length, 3)
t.assert.deepStrictEqual(range[0], { start: 149, end: 149, index: 0 })
t.assert.deepStrictEqual(range[1], { start: 20, end: 120, index: 1 })
t.assert.deepStrictEqual(range[2], { start: 0, end: 1, index: 2 })
})
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const fs = require('node:fs')

@@ -8,3 +8,3 @@ const http = require('node:http')

const request = require('supertest')
const send = require('..').send
const { send } = require('..')
const { shouldNotHaveHeader, createServer } = require('./utils')

@@ -16,21 +16,21 @@

test('send(file, options)', function (t) {
test('send(file, options)', async function (t) {
t.plan(11)
t.test('acceptRanges', function (t) {
await t.test('acceptRanges', async function (t) {
t.plan(6)
t.test('should support disabling accept-ranges', function (t) {
t.plan(2)
await t.test('should support disabling accept-ranges', async function (t) {
t.plan(1)
request(createServer({ acceptRanges: false, root: fixtures }))
await request(createServer({ acceptRanges: false, root: fixtures }))
.get('/nums.txt')
.expect(shouldNotHaveHeader('Accept-Ranges', t))
.expect(200, err => t.error(err))
.expect(200)
})
t.test('should ignore requested range', function (t) {
t.plan(3)
await t.test('should ignore requested range', async function (t) {
t.plan(2)
request(createServer({ acceptRanges: false, root: fixtures }))
await request(createServer({ acceptRanges: false, root: fixtures }))
.get('/nums.txt')

@@ -40,302 +40,267 @@ .set('Range', 'bytes=0-2')

.expect(shouldNotHaveHeader('Content-Range', t))
.expect(200, '123456789', err => t.error(err))
.expect(200, '123456789')
})
t.test('should limit high return size /1', function (t) {
t.plan(4)
await t.test('should limit high return size /1', async function (t) {
t.plan(3)
request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 1, root: fixtures }))
await request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 1, root: fixtures }))
.get('/nums.txt')
.set('Range', 'bytes=0-2')
.expect((res) => t.equal(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.equal(res.headers['content-range'], 'bytes 0-0/9'))
.expect((res) => t.equal(res.headers['content-length'], '1', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '1', (err) => t.error(err))
.expect((res) => t.assert.deepStrictEqual(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-range'], 'bytes 0-0/9'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-length'], '1', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '1')
})
t.test('should limit high return size /2', function (t) {
t.plan(4)
await t.test('should limit high return size /2', async function (t) {
t.plan(3)
request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 1, root: fixtures }))
await request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 1, root: fixtures }))
.get('/nums.txt')
.set('Range', 'bytes=1-2')
.expect((res) => t.equal(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.equal(res.headers['content-range'], 'bytes 1-1/9'))
.expect((res) => t.equal(res.headers['content-length'], '1', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '2', (err) => t.error(err))
.expect((res) => t.assert.deepStrictEqual(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-range'], 'bytes 1-1/9'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-length'], '1', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '2')
})
t.test('should limit high return size /3', function (t) {
t.plan(4)
await t.test('should limit high return size /3', async function (t) {
t.plan(3)
request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 1, root: fixtures }))
await request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 1, root: fixtures }))
.get('/nums.txt')
.set('Range', 'bytes=1-3')
.expect((res) => t.equal(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.equal(res.headers['content-range'], 'bytes 1-1/9'))
.expect((res) => t.equal(res.headers['content-length'], '1', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '2', (err) => t.error(err))
.expect((res) => t.assert.deepStrictEqual(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-range'], 'bytes 1-1/9'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-length'], '1', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '2')
})
t.test('should limit high return size /4', function (t) {
t.plan(4)
await t.test('should limit high return size /4', async function (t) {
t.plan(3)
request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 4, root: fixtures }))
await request(createServer({ acceptRanges: true, maxContentRangeChunkSize: 4, root: fixtures }))
.get('/nums.txt')
.set('Range', 'bytes=1-2,3-6')
.expect((res) => t.equal(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.equal(res.headers['content-range'], 'bytes 1-4/9'))
.expect((res) => t.equal(res.headers['content-length'], '4', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '2345', (err) => t.error(err))
.expect((res) => t.assert.deepStrictEqual(res.headers['accept-ranges'], 'bytes'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-range'], 'bytes 1-4/9'))
.expect((res) => t.assert.deepStrictEqual(res.headers['content-length'], '4', 'should content-length must be as same as maxContentRangeChunkSize'))
.expect(206, '2345')
})
})
t.test('cacheControl', function (t) {
await t.test('cacheControl', async function (t) {
t.plan(2)
t.test('should support disabling cache-control', function (t) {
t.plan(2)
request(createServer({ cacheControl: false, root: fixtures }))
await t.test('should support disabling cache-control', async function (t) {
t.plan(1)
await request(createServer({ cacheControl: false, root: fixtures }))
.get('/name.txt')
.expect(shouldNotHaveHeader('Cache-Control', t))
.expect(200, err => t.error(err))
.expect(200)
})
t.test('should ignore maxAge option', function (t) {
t.plan(2)
await t.test('should ignore maxAge option', async function (t) {
t.plan(1)
request(createServer({ cacheControl: false, maxAge: 1000, root: fixtures }))
await request(createServer({ cacheControl: false, maxAge: 1000, root: fixtures }))
.get('/name.txt')
.expect(shouldNotHaveHeader('Cache-Control', t))
.expect(200, err => t.error(err))
.expect(200)
})
})
t.test('contentType', function (t) {
await t.test('contentType', async function (t) {
t.plan(1)
t.test('should support disabling content-type', function (t) {
t.plan(2)
await t.test('should support disabling content-type', async function (t) {
t.plan(1)
request(createServer({ contentType: false, root: fixtures }))
await request(createServer({ contentType: false, root: fixtures }))
.get('/name.txt')
.expect(shouldNotHaveHeader('Content-Type', t))
.expect(200, err => t.error(err))
.expect(200)
})
})
t.test('etag', function (t) {
await t.test('etag', async function (t) {
t.plan(1)
t.test('should support disabling etags', function (t) {
t.plan(2)
await t.test('should support disabling etags', async function (t) {
t.plan(1)
request(createServer({ etag: false, root: fixtures }))
await request(createServer({ etag: false, root: fixtures }))
.get('/name.txt')
.expect(shouldNotHaveHeader('ETag', t))
.expect(200, err => t.error(err))
.expect(200)
})
})
t.test('extensions', function (t) {
await t.test('extensions', async function (t) {
t.plan(9)
t.test('should reject numbers', function (t) {
t.plan(1)
request(createServer({ extensions: 42, root: fixtures }))
await t.test('should reject numbers', async function (t) {
await request(createServer({ extensions: 42, root: fixtures }))
.get('/pets/')
.expect(500, /TypeError: extensions option/, err => t.error(err))
.expect(500, /TypeError: extensions option/)
})
t.test('should reject true', function (t) {
t.plan(1)
request(createServer({ extensions: true, root: fixtures }))
await t.test('should reject true', async function (t) {
await request(createServer({ extensions: true, root: fixtures }))
.get('/pets/')
.expect(500, /TypeError: extensions option/, err => t.error(err))
.expect(500, /TypeError: extensions option/)
})
t.test('should be not be enabled by default', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should be not be enabled by default', async function (t) {
await request(createServer({ root: fixtures }))
.get('/tobi')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should be configurable', function (t) {
t.plan(1)
request(createServer({ extensions: 'txt', root: fixtures }))
await t.test('should be configurable', async function (t) {
await request(createServer({ extensions: 'txt', root: fixtures }))
.get('/name')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('should support disabling extensions', function (t) {
t.plan(1)
request(createServer({ extensions: false, root: fixtures }))
await t.test('should support disabling extensions', async function (t) {
await request(createServer({ extensions: false, root: fixtures }))
.get('/name')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should support fallbacks', function (t) {
t.plan(1)
request(createServer({ extensions: ['htm', 'html', 'txt'], root: fixtures }))
await t.test('should support fallbacks', async function (t) {
await request(createServer({ extensions: ['htm', 'html', 'txt'], root: fixtures }))
.get('/name')
.expect(200, '<p>tobi</p>', err => t.error(err))
.expect(200, '<p>tobi</p>')
})
t.test('should 404 if nothing found', function (t) {
t.plan(1)
request(createServer({ extensions: ['htm', 'html', 'txt'], root: fixtures }))
await t.test('should 404 if nothing found', async function (t) {
await request(createServer({ extensions: ['htm', 'html', 'txt'], root: fixtures }))
.get('/bob')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should skip directories', function (t) {
t.plan(1)
request(createServer({ extensions: ['file', 'dir'], root: fixtures }))
await t.test('should skip directories', async function (t) {
await request(createServer({ extensions: ['file', 'dir'], root: fixtures }))
.get('/name')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should not search if file has extension', function (t) {
t.plan(1)
request(createServer({ extensions: 'html', root: fixtures }))
await t.test('should not search if file has extension', async function (t) {
await request(createServer({ extensions: 'html', root: fixtures }))
.get('/thing.html')
.expect(404, err => t.error(err))
.expect(404)
})
})
t.test('lastModified', function (t) {
await t.test('lastModified', async function (t) {
t.plan(1)
t.test('should support disabling last-modified', function (t) {
t.plan(2)
await t.test('should support disabling last-modified', async function (t) {
t.plan(1)
request(createServer({ lastModified: false, root: fixtures }))
await request(createServer({ lastModified: false, root: fixtures }))
.get('/name.txt')
.expect(shouldNotHaveHeader('Last-Modified', t))
.expect(200, err => t.error(err))
.expect(200)
})
})
t.test('dotfiles', function (t) {
await t.test('dotfiles', async function (t) {
t.plan(5)
t.test('should default to "ignore"', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should default to "ignore"', async function (t) {
await request(createServer({ root: fixtures }))
.get('/.hidden.txt')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should reject bad value', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'bogus' }))
await t.test('should reject bad value', async function (t) {
await request(createServer({ dotfiles: 'bogus' }))
.get('/name.txt')
.expect(500, /dotfiles/, err => t.error(err))
.expect(500, /dotfiles/)
})
t.test('when "allow"', function (t) {
await t.test('when "allow"', async function (t) {
t.plan(3)
t.test('should send dotfile', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'allow', root: fixtures }))
await t.test('should send dotfile', async function (t) {
await request(createServer({ dotfiles: 'allow', root: fixtures }))
.get('/.hidden.txt')
.expect(200, 'secret', err => t.error(err))
.expect(200, 'secret')
})
t.test('should send within dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'allow', root: fixtures }))
await t.test('should send within dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'allow', root: fixtures }))
.get('/.mine/name.txt')
.expect(200, /tobi/, err => t.error(err))
.expect(200, /tobi/)
})
t.test('should 404 for non-existent dotfile', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'allow', root: fixtures }))
await t.test('should 404 for non-existent dotfile', async function (t) {
await request(createServer({ dotfiles: 'allow', root: fixtures }))
.get('/.nothere')
.expect(404, err => t.error(err))
.expect(404)
})
})
t.test('when "deny"', function (t) {
await t.test('when "deny"', async function (t) {
t.plan(10)
t.test('should 403 for dotfile', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
await t.test('should 403 for dotfile', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.hidden.txt')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should 403 for dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
await t.test('should 403 for dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.mine')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should 403 for dotfile directory with trailing slash', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
await t.test('should 403 for dotfile directory with trailing slash', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.mine/')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should 403 for file within dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
await t.test('should 403 for file within dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.mine/name.txt')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should 403 for non-existent dotfile', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
await t.test('should 403 for non-existent dotfile', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.nothere')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should 403 for non-existent dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
await t.test('should 403 for non-existent dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.what/name.txt')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should 403 for dotfile in directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/pets/.hidden')
.expect(403, err => t.error(err))
await t.test('should 403 for dotfile in directory', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/pets/.hidden.txt')
.expect(403)
})
t.test('should 403 for dotfile in dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.mine/.hidden')
.expect(403, err => t.error(err))
await t.test('should 403 for dotfile in dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'deny', root: fixtures }))
.get('/.mine/.hidden.txt')
.expect(403)
})
t.test('should send files in root dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'deny', root: path.join(fixtures, '.mine') }))
await t.test('should send files in root dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'deny', root: path.join(fixtures, '.mine') }))
.get('/name.txt')
.expect(200, /tobi/, err => t.error(err))
.expect(200, /tobi/)
})
t.test('should 403 for dotfile without root', function (t) {
t.plan(1)
await t.test('should 403 for dotfile without root', async function (t) {
const server = http.createServer(async function onRequest (req, res) {

@@ -347,70 +312,54 @@ const { statusCode, headers, stream } = await send(req, fixtures + '/.mine' + req.url, { dotfiles: 'deny' })

request(server)
await request(server)
.get('/name.txt')
.expect(403, err => t.error(err))
.expect(403)
})
})
t.test('when "ignore"', function (t) {
await t.test('when "ignore"', async function (t) {
t.plan(8)
t.test('should 404 for dotfile', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: fixtures }))
await t.test('should 404 for dotfile', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: fixtures }))
.get('/.hidden.txt')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should 404 for dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: fixtures }))
await t.test('should 404 for dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: fixtures }))
.get('/.mine')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should 404 for dotfile directory with trailing slash', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: fixtures }))
await t.test('should 404 for dotfile directory with trailing slash', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: fixtures }))
.get('/.mine/')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should 404 for file within dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: fixtures }))
await t.test('should 404 for file within dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: fixtures }))
.get('/.mine/name.txt')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should 404 for non-existent dotfile', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: fixtures }))
await t.test('should 404 for non-existent dotfile', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: fixtures }))
.get('/.nothere')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should 404 for non-existent dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: fixtures }))
await t.test('should 404 for non-existent dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: fixtures }))
.get('/.what/name.txt')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should send files in root dotfile directory', function (t) {
t.plan(1)
request(createServer({ dotfiles: 'ignore', root: path.join(fixtures, '.mine') }))
await t.test('should send files in root dotfile directory', async function (t) {
await request(createServer({ dotfiles: 'ignore', root: path.join(fixtures, '.mine') }))
.get('/name.txt')
.expect(200, /tobi/, err => t.error(err))
.expect(200, /tobi/)
})
t.test('should 404 for dotfile without root', function (t) {
t.plan(1)
await t.test('should 404 for dotfile without root', async function (t) {
const server = http.createServer(async function onRequest (req, res) {

@@ -422,5 +371,5 @@ const { statusCode, headers, stream } = await send(req, fixtures + '/.mine' + req.url, { dotfiles: 'ignore' })

request(server)
await request(server)
.get('/name.txt')
.expect(404, err => t.error(err))
.expect(404)
})

@@ -430,132 +379,104 @@ })

t.test('immutable', function (t) {
await t.test('immutable', async function (t) {
t.plan(2)
t.test('should default to false', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should default to false', async function (t) {
await request(createServer({ root: fixtures }))
.get('/name.txt')
.expect('Cache-Control', 'public, max-age=0', err => t.error(err))
.expect('Cache-Control', 'public, max-age=0')
})
t.test('should set immutable directive in Cache-Control', function (t) {
t.plan(1)
request(createServer({ immutable: true, maxAge: '1h', root: fixtures }))
await t.test('should set immutable directive in Cache-Control', async function (t) {
await request(createServer({ immutable: true, maxAge: '1h', root: fixtures }))
.get('/name.txt')
.expect('Cache-Control', 'public, max-age=3600, immutable', err => t.error(err))
.expect('Cache-Control', 'public, max-age=3600, immutable')
})
})
t.test('maxAge', function (t) {
await t.test('maxAge', async function (t) {
t.plan(4)
t.test('should default to 0', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should default to 0', async function (t) {
await request(createServer({ root: fixtures }))
.get('/name.txt')
.expect('Cache-Control', 'public, max-age=0', err => t.error(err))
.expect('Cache-Control', 'public, max-age=0')
})
t.test('should floor to integer', function (t) {
t.plan(1)
request(createServer({ maxAge: 123956, root: fixtures }))
await t.test('should floor to integer', async function (t) {
await request(createServer({ maxAge: 123956, root: fixtures }))
.get('/name.txt')
.expect('Cache-Control', 'public, max-age=123', err => t.error(err))
.expect('Cache-Control', 'public, max-age=123')
})
t.test('should accept string', function (t) {
t.plan(1)
request(createServer({ maxAge: '30d', root: fixtures }))
await t.test('should accept string', async function (t) {
await request(createServer({ maxAge: '30d', root: fixtures }))
.get('/name.txt')
.expect('Cache-Control', 'public, max-age=2592000', err => t.error(err))
.expect('Cache-Control', 'public, max-age=2592000')
})
t.test('should max at 1 year', function (t) {
t.plan(1)
request(createServer({ maxAge: '2y', root: fixtures }))
await t.test('should max at 1 year', async function (t) {
await request(createServer({ maxAge: '2y', root: fixtures }))
.get('/name.txt')
.expect('Cache-Control', 'public, max-age=31536000', err => t.error(err))
.expect('Cache-Control', 'public, max-age=31536000')
})
})
t.test('index', function (t) {
await t.test('index', async function (t) {
t.plan(10)
t.test('should reject numbers', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: 42 }))
await t.test('should reject numbers', async function (t) {
await request(createServer({ root: fixtures, index: 42 }))
.get('/pets/')
.expect(500, /TypeError: index option/, err => t.error(err))
.expect(500, /TypeError: index option/)
})
t.test('should reject true', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: true }))
await t.test('should reject true', async function (t) {
await request(createServer({ root: fixtures, index: true }))
.get('/pets/')
.expect(500, /TypeError: index option/, err => t.error(err))
.expect(500, /TypeError: index option/)
})
t.test('should default to index.html', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should default to index.html', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets/')
.expect(fs.readFileSync(path.join(fixtures, 'pets', 'index.html'), 'utf8'), err => t.error(err))
.expect(fs.readFileSync(path.join(fixtures, 'pets', 'index.html'), 'utf8'))
})
t.test('should be configurable', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: 'tobi.html' }))
await t.test('should be configurable', async function (t) {
await request(createServer({ root: fixtures, index: 'tobi.html' }))
.get('/')
.expect(200, '<p>tobi</p>', err => t.error(err))
.expect(200, '<p>tobi</p>')
})
t.test('should support disabling', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: false }))
await t.test('should support disabling', async function (t) {
await request(createServer({ root: fixtures, index: false }))
.get('/pets/')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should support fallbacks', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: ['default.htm', 'index.html'] }))
await t.test('should support fallbacks', async function (t) {
await request(createServer({ root: fixtures, index: ['default.htm', 'index.html'] }))
.get('/pets/')
.expect(200, fs.readFileSync(path.join(fixtures, 'pets', 'index.html'), 'utf8'), err => t.error(err))
.expect(200, fs.readFileSync(path.join(fixtures, 'pets', 'index.html'), 'utf8'))
})
t.test('should 404 if no index file found (file)', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: 'default.htm' }))
await t.test('should 404 if no index file found (file)', async function (t) {
await request(createServer({ root: fixtures, index: 'default.htm' }))
.get('/pets/')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should 404 if no index file found (dir)', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: 'pets' }))
await t.test('should 404 if no index file found (dir)', async function (t) {
await request(createServer({ root: fixtures, index: 'pets' }))
.get('/')
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should not follow directories', function (t) {
t.plan(1)
request(createServer({ root: fixtures, index: ['pets', 'name.txt'] }))
await t.test('should not follow directories', async function (t) {
await request(createServer({ root: fixtures, index: ['pets', 'name.txt'] }))
.get('/')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('should work without root', function (t) {
t.plan(1)
await t.test('should work without root', async function (t) {
const server = http.createServer(async function (req, res) {

@@ -568,24 +489,21 @@ const p = path.join(fixtures, 'pets').replace(/\\/g, '/') + '/'

request(server)
await request(server)
.get('/')
.expect(200, /tobi/, err => t.error(err))
.expect(200, /tobi/)
})
})
t.test('root', function (t) {
await t.test('root', async function (t) {
t.plan(2)
t.test('when given', function (t) {
await t.test('when given', async function (t) {
t.plan(8)
t.test('should join root', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should join root', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets/../name.txt')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('should work with trailing slash', function (t) {
t.plan(1)
await t.test('should work with trailing slash', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -597,10 +515,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures + '/' })

request(app)
await request(app)
.get('/name.txt')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('should work with empty path', function (t) {
t.plan(1)
await t.test('should work with empty path', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -612,5 +528,5 @@ const { statusCode, headers, stream } = await send(req, '', { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect(301, /Redirecting to/, err => t.error(err))
.expect(301, /Redirecting to/)
})

@@ -624,5 +540,3 @@

//
t.test('should try as file with empty path', function (t) {
t.plan(1)
await t.test('should try as file with empty path', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -634,18 +548,14 @@ const { statusCode, headers, stream } = await send(req, '', { root: path.join(fixtures, 'name.txt') })

request(app)
await request(app)
.get('/')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('should restrict paths to within root', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should restrict paths to within root', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets/../../send.js')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should allow .. in root', function (t) {
t.plan(1)
await t.test('should allow .. in root', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -657,30 +567,24 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures + '/../fixtures' })

request(app)
await request(app)
.get('/pets/../../send.js')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should not allow root transversal', function (t) {
t.plan(1)
request(createServer({ root: path.join(fixtures, 'name.d') }))
await t.test('should not allow root transversal', async function (t) {
await request(createServer({ root: path.join(fixtures, 'name.d') }))
.get('/../name.dir/name.txt')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should not allow root path disclosure', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should not allow root path disclosure', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets/../../fixtures/name.txt')
.expect(403, err => t.error(err))
.expect(403)
})
})
t.test('when missing', function (t) {
await t.test('when missing', async function (t) {
t.plan(2)
t.test('should consider .. malicious', function (t) {
t.plan(1)
await t.test('should consider .. malicious', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -692,10 +596,8 @@ const { statusCode, headers, stream } = await send(req, fixtures + req.url)

request(app)
await request(app)
.get('/../send.js')
.expect(403, err => t.error(err))
.expect(403)
})
t.test('should still serve files with dots in name', function (t) {
t.plan(1)
await t.test('should still serve files with dots in name', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -707,5 +609,5 @@ const { statusCode, headers, stream } = await send(req, fixtures + req.url)

request(app)
await request(app)
.get('/do..ts.txt')
.expect(200, '...', err => t.error(err))
.expect(200, '...')
})

@@ -712,0 +614,0 @@ })

'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const http = require('node:http')

@@ -13,8 +13,6 @@ const path = require('node:path')

test('send(file)', function (t) {
test('send(file)', async function (t) {
t.plan(22)
t.test('should stream the file contents', function (t) {
t.plan(1)
await t.test('should stream the file contents', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -26,11 +24,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect('Content-Length', '4')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('should stream a zero-length file', function (t) {
t.plan(1)
await t.test('should stream a zero-length file', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -42,11 +38,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/empty.txt')
.expect('Content-Length', '0')
.expect(200, '', err => t.error(err))
.expect(200, '')
})
t.test('should decode the given path as a URI', function (t) {
t.plan(1)
await t.test('should decode the given path as a URI', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -58,10 +52,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/some%20thing.txt')
.expect(200, 'hey', err => t.error(err))
.expect(200, 'hey')
})
t.test('should serve files with dots in name', function (t) {
t.plan(1)
await t.test('should serve files with dots in name', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -73,10 +65,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/do..ts.txt')
.expect(200, '...', err => t.error(err))
.expect(200, '...')
})
t.test('should treat a malformed URI as a bad request', function (t) {
t.plan(1)
await t.test('should treat a malformed URI as a bad request', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -88,10 +78,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/some%99thing.txt')
.expect(400, /Bad Request/, err => t.error(err))
.expect(400, /Bad Request/)
})
t.test('should 400 on NULL bytes', function (t) {
t.plan(1)
await t.test('should 400 on NULL bytes', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -103,10 +91,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/some%00thing.txt')
.expect(400, /Bad Request/, err => t.error(err))
.expect(400, /Bad Request/)
})
t.test('should treat an ENAMETOOLONG as a 404', function (t) {
t.plan(1)
await t.test('should treat an ENAMETOOLONG as a 404', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -119,9 +105,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

const path = Array(100).join('foobar')
request(app)
await request(app)
.get('/' + path)
.expect(404, err => t.error(err))
.expect(404)
})
t.test('should support HEAD', function (t) {
t.plan(2)
await t.test('should support HEAD', async function (t) {
t.plan(1)

@@ -134,3 +120,3 @@ const app = http.createServer(async function (req, res) {

request(app)
await request(app)
.head('/name.txt')

@@ -140,8 +126,5 @@ .expect(200)

.expect(shouldNotHaveBody(t))
.end(err => t.error(err))
})
t.test('should add an ETag header field', function (t) {
t.plan(1)
await t.test('should add an ETag header field', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -153,11 +136,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect('etag', /^W\/"[^"]+"$/)
.end(err => t.error(err))
})
t.test('should add a Date header field', function (t) {
t.plan(1)
await t.test('should add a Date header field', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -169,10 +149,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect('date', dateRegExp, err => t.error(err))
.expect('date', dateRegExp)
})
t.test('should add a Last-Modified header field', function (t) {
t.plan(1)
await t.test('should add a Last-Modified header field', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -184,10 +162,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect('last-modified', dateRegExp, err => t.error(err))
.expect('last-modified', dateRegExp)
})
t.test('should add a Accept-Ranges header field', function (t) {
t.plan(1)
await t.test('should add a Accept-Ranges header field', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -199,10 +175,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect('Accept-Ranges', 'bytes', err => t.error(err))
.expect('Accept-Ranges', 'bytes')
})
t.test('should 404 if the file does not exist', function (t) {
t.plan(1)
await t.test('should 404 if the file does not exist', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -214,10 +188,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/meow')
.expect(404, /Not Found/, err => t.error(err))
.expect(404, /Not Found/)
})
t.test('should 404 if the filename is too long', function (t) {
t.plan(1)
await t.test('should 404 if the filename is too long', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -231,10 +203,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/' + longFilename)
.expect(404, /Not Found/, err => t.error(err))
.expect(404, /Not Found/)
})
t.test('should 404 if the requested resource is not a directory', function (t) {
t.plan(1)
await t.test('should 404 if the requested resource is not a directory', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -246,10 +216,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt/invalid')
.expect(404, /Not Found/, err => t.error(err))
.expect(404, /Not Found/)
})
t.test('should not override content-type', function (t) {
t.plan(1)
await t.test('should not override content-type', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -263,10 +231,8 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

})
request(app)
await request(app)
.get('/name.txt')
.expect('Content-Type', 'application/x-custom', err => t.error(err))
.expect('Content-Type', 'application/x-custom')
})
t.test('should set Content-Type via mime map', function (t) {
t.plan(2)
await t.test('should set Content-Type via mime map', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -278,58 +244,47 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, function (err) {
t.error(err)
request(app)
.get('/tobi.html')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, err => t.error(err))
})
.expect(200)
await request(app)
.get('/tobi.html')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200)
})
t.test('send directory', function (t) {
await t.test('send directory', async function (t) {
t.plan(5)
t.test('should redirect directories to trailing slash', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should redirect directories to trailing slash', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets')
.expect('Location', '/pets/')
.expect(301, err => t.error(err))
.expect(301)
})
t.test('should respond with an HTML redirect', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should respond with an HTML redirect', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets')
.expect('Location', '/pets/')
.expect('Content-Type', /html/)
.expect(301, />Redirecting to \/pets\/</, err => t.error(err))
.expect(301, />Redirecting to \/pets\/</)
})
t.test('should respond with default Content-Security-Policy', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should respond with default Content-Security-Policy', async function (t) {
await request(createServer({ root: fixtures }))
.get('/pets')
.expect('Location', '/pets/')
.expect('Content-Security-Policy', "default-src 'none'")
.expect(301, err => t.error(err))
.expect(301)
})
t.test('should not redirect to protocol-relative locations', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should not redirect to protocol-relative locations', async function (t) {
await request(createServer({ root: fixtures }))
.get('//pets')
.expect('Location', '/pets/')
.expect(301, err => t.error(err))
.expect(301)
})
t.test('should respond with an HTML redirect', function (t) {
t.plan(1)
await t.test('should respond with an HTML redirect', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -341,37 +296,31 @@ const { statusCode, headers, stream } = await send(req, req.url.replace('/snow', '/snow ☃'), { root: 'test/fixtures' })

request(app)
await request(app)
.get('/snow')
.expect('Location', '/snow%20%E2%98%83/')
.expect('Content-Type', /html/)
.expect(301, />Redirecting to \/snow%20%E2%98%83\/</, err => t.error(err))
.expect(301, />Redirecting to \/snow%20%E2%98%83\/</)
})
})
t.test('send error', function (t) {
await t.test('send error', async function (t) {
t.plan(2)
t.test('should respond to errors directly', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should respond to errors directly', async function (t) {
await request(createServer({ root: fixtures }))
.get('/foobar')
.expect(404, />Not Found</, err => t.error(err))
.expect(404, />Not Found</)
})
t.test('should respond with default Content-Security-Policy', function (t) {
t.plan(1)
request(createServer({ root: fixtures }))
await t.test('should respond with default Content-Security-Policy', async function (t) {
await request(createServer({ root: fixtures }))
.get('/foobar')
.expect('Content-Security-Policy', "default-src 'none'")
.expect(404, err => t.error(err))
.expect(404)
})
})
t.test('with conditional-GET', function (t) {
await t.test('with conditional-GET', async function (t) {
t.plan(6)
t.test('should remove Content headers with 304', function (t) {
t.plan(2)
await t.test('should remove Content headers with 304', async function (t) {
const server = createServer({ root: fixtures }, function (_req, res) {

@@ -383,18 +332,15 @@ res.setHeader('Content-Language', 'en-US')

request(server)
const res = await request(server)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(server)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.expect('Content-Location', 'http://localhost/name.txt')
.expect('Contents', 'foo')
.expect(304, err => t.error(err))
})
.expect(200)
await request(server)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.expect('Content-Location', 'http://localhost/name.txt')
.expect('Contents', 'foo')
.expect(304)
})
t.test('should not remove all Content-* headers', function (t) {
t.plan(2)
await t.test('should not remove all Content-* headers', async function (t) {
const server = createServer({ root: fixtures }, function (_req, res) {

@@ -405,21 +351,18 @@ res.setHeader('Content-Location', 'http://localhost/name.txt')

request(server)
const res = await request(server)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(server)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.expect('Content-Location', 'http://localhost/name.txt')
.expect('Content-Security-Policy', 'default-src \'self\'')
.expect(304, err => t.error(err))
})
.expect(200)
await request(server)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.expect('Content-Location', 'http://localhost/name.txt')
.expect('Content-Security-Policy', 'default-src \'self\'')
.expect(304)
})
t.test('where "If-Match" is set', function (t) {
await t.test('where "If-Match" is set', async function (t) {
t.plan(4)
t.test('should respond with 200 when "*"', function (t) {
t.plan(1)
await t.test('should respond with 200 when "*"', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -431,11 +374,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.set('If-Match', '*')
.expect(200, err => t.error(err))
.expect(200)
})
t.test('should respond with 412 when ETag unmatched', function (t) {
t.plan(1)
await t.test('should respond with 412 when ETag unmatched', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -447,11 +388,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.set('If-Match', ' "foo",, "bar" ,')
.expect(412, err => t.error(err))
.expect(412)
})
t.test('should respond with 200 when ETag matched /1', function (t) {
t.plan(2)
await t.test('should respond with 200 when ETag matched /1', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -463,16 +402,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-Match', '"foo", "bar", ' + res.headers.etag)
.expect(200, err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-Match', '"foo", "bar", ' + res.headers.etag)
.expect(200)
})
t.test('should respond with 200 when ETag matched /2', function (t) {
t.plan(2)
await t.test('should respond with 200 when ETag matched /2', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -484,20 +420,17 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-Match', '"foo", ' + res.headers.etag + ', "bar"')
.expect(200, err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-Match', '"foo", ' + res.headers.etag + ', "bar"')
.expect(200)
})
})
t.test('where "If-Modified-Since" is set', function (t) {
await t.test('where "If-Modified-Since" is set', async function (t) {
t.plan(3)
t.test('should respond with 304 when unmodified', function (t) {
t.plan(2)
await t.test('should respond with 304 when unmodified', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -509,16 +442,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-Modified-Since', res.headers['last-modified'])
.expect(304, err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-Modified-Since', res.headers['last-modified'])
.expect(304)
})
t.test('should respond with 200 when modified', function (t) {
t.plan(2)
await t.test('should respond with 200 when modified', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -530,18 +460,15 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
const lmod = new Date(res.headers['last-modified'])
const date = new Date(lmod - 60000)
request(app)
.get('/name.txt')
.set('If-Modified-Since', date.toUTCString())
.expect(200, 'tobi', err => t.error(err))
})
.expect(200)
const lmod = new Date(res.headers['last-modified'])
const date = new Date(lmod - 60000)
await request(app)
.get('/name.txt')
.set('If-Modified-Since', date.toUTCString())
.expect(200, 'tobi')
})
t.test('should respond with 200 when modified', function (t) {
t.plan(2)
await t.test('should respond with 200 when modified', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -553,21 +480,18 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-Modified-Since', res.headers['last-modified'])
.set('cache-control', 'no-cache')
.expect(200, 'tobi', err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-Modified-Since', res.headers['last-modified'])
.set('cache-control', 'no-cache')
.expect(200, 'tobi')
})
})
t.test('where "If-None-Match" is set', function (t) {
await t.test('where "If-None-Match" is set', async function (t) {
t.plan(6)
t.test('should respond with 304 when ETag matched', function (t) {
t.plan(2)
await t.test('should respond with 304 when ETag matched', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -579,16 +503,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.expect(304, err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.expect(304)
})
t.test('should respond with 200 when ETag unmatched', function (t) {
t.plan(2)
await t.test('should respond with 200 when ETag unmatched', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -600,16 +521,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect(200, function (err) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-None-Match', '"123"')
.expect(200, 'tobi', err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-None-Match', '"123"')
.expect(200, 'tobi')
})
t.test('should respond with 200 when ETag is not generated', function (t) {
t.plan(2)
await t.test('should respond with 200 when ETag is not generated', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -621,16 +539,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { etag: false, root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect(200, function (err) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-None-Match', '"123"')
.expect(200, 'tobi', err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-None-Match', '"123"')
.expect(200, 'tobi')
})
t.test('should respond with 306 Not Modified when using wildcard * on existing file', function (t) {
t.plan(2)
await t.test('should respond with 306 Not Modified when using wildcard * on existing file', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -642,16 +557,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { etag: false, root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.expect(200, function (err) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-None-Match', '*')
.expect(304, '', err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-None-Match', '*')
.expect(304, '')
})
t.test('should respond with 404 Not Found when using wildcard * on non-existing file', function (t) {
t.plan(1)
await t.test('should respond with 404 Not Found when using wildcard * on non-existing file', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -663,11 +575,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { etag: false, root: fixtures })

request(app)
await request(app)
.get('/asdf.txt')
.set('If-None-Match', '*')
.expect(404, /Not Found/, err => t.error(err))
.expect(404, /Not Found/)
})
t.test('should respond with 200 cache-control is set to no-cache', function (t) {
t.plan(2)
await t.test('should respond with 200 cache-control is set to no-cache', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -679,21 +589,18 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.set('cache-control', 'no-cache')
.expect(200, 'tobi', err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-None-Match', res.headers.etag)
.set('cache-control', 'no-cache')
.expect(200, 'tobi')
})
})
t.test('where "If-Unmodified-Since" is set', function (t) {
await t.test('where "If-Unmodified-Since" is set', async function (t) {
t.plan(3)
t.test('should respond with 200 when unmodified', function (t) {
t.plan(2)
await t.test('should respond with 200 when unmodified', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -705,16 +612,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
request(app)
.get('/name.txt')
.set('If-Unmodified-Since', res.headers['last-modified'])
.expect(200, err => t.error(err))
})
.expect(200)
await request(app)
.get('/name.txt')
.set('If-Unmodified-Since', res.headers['last-modified'])
.expect(200)
})
t.test('should respond with 412 when modified', function (t) {
t.plan(2)
await t.test('should respond with 412 when modified', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -726,18 +630,15 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/name.txt')
.expect(200, function (err, res) {
t.error(err)
const lmod = new Date(res.headers['last-modified'])
const date = new Date(lmod - 60000).toUTCString()
request(app)
.get('/name.txt')
.set('If-Unmodified-Since', date)
.expect(412, err => t.error(err))
})
.expect(200)
const lmod = new Date(res.headers['last-modified'])
const date = new Date(lmod - 60000).toUTCString()
await request(app)
.get('/name.txt')
.set('If-Unmodified-Since', date)
.expect(412)
})
t.test('should respond with 200 when invalid date', function (t) {
t.plan(1)
await t.test('should respond with 200 when invalid date', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -749,6 +650,6 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/name.txt')
.set('If-Unmodified-Since', 'foo')
.expect(200, err => t.error(err))
.expect(200)
})

@@ -758,8 +659,6 @@ })

t.test('with Range request', function (t) {
await t.test('with Range request', async function (t) {
t.plan(13)
t.test('should support byte ranges', function (t) {
t.plan(1)
await t.test('should support byte ranges', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -771,11 +670,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=0-4')
.expect(206, '12345', err => t.error(err))
.expect(206, '12345')
})
t.test('should ignore non-byte ranges', function (t) {
t.plan(1)
await t.test('should ignore non-byte ranges', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -787,11 +684,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'items=0-4')
.expect(200, '123456789', err => t.error(err))
.expect(200, '123456789')
})
t.test('should be inclusive', function (t) {
t.plan(1)
await t.test('should be inclusive', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -803,11 +698,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=0-0')
.expect(206, '1', err => t.error(err))
.expect(206, '1')
})
t.test('should set Content-Range', function (t) {
t.plan(1)
await t.test('should set Content-Range', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -819,12 +712,10 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=2-5')
.expect('Content-Range', 'bytes 2-5/9')
.expect(206, err => t.error(err))
.expect(206)
})
t.test('should support -n', function (t) {
t.plan(1)
await t.test('should support -n', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -836,11 +727,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=-3')
.expect(206, '789', err => t.error(err))
.expect(206, '789')
})
t.test('should support n-', function (t) {
t.plan(1)
await t.test('should support n-', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -852,11 +741,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=3-')
.expect(206, '456789', err => t.error(err))
.expect(206, '456789')
})
t.test('should respond with 206 "Partial Content"', function (t) {
t.plan(1)
await t.test('should respond with 206 "Partial Content"', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -868,11 +755,9 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=0-4')
.expect(206, err => t.error(err))
.expect(206)
})
t.test('should set Content-Length to the # of octets transferred', function (t) {
t.plan(1)
await t.test('should set Content-Length to the # of octets transferred', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -884,15 +769,13 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=2-3')
.expect('Content-Length', '2')
.expect(206, '34', err => t.error(err))
.expect(206, '34')
})
t.test('when last-byte-pos of the range is greater the length', function (t) {
await t.test('when last-byte-pos of the range is greater the length', async function (t) {
t.plan(2)
t.test('is taken to be equal to one less than the length', function (t) {
t.plan(1)
await t.test('is taken to be equal to one less than the length', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -904,12 +787,10 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=2-50')
.expect('Content-Range', 'bytes 2-8/9')
.expect(206, err => t.error(err))
.expect(206)
})
t.test('should adapt the Content-Length accordingly', function (t) {
t.plan(1)
await t.test('should adapt the Content-Length accordingly', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -921,16 +802,14 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=2-50')
.expect('Content-Length', '7')
.expect(206, err => t.error(err))
.expect(206)
})
})
t.test('when the first- byte-pos of the range is greater length', function (t) {
await t.test('when the first- byte-pos of the range is greater length', async function (t) {
t.plan(2)
t.test('should respond with 416', function (t) {
t.plan(1)
await t.test('should respond with 416', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -942,12 +821,10 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=9-50')
.expect('Content-Range', 'bytes */9')
.expect(416, err => t.error(err))
.expect(416)
})
t.test('should emit error 416 with content-range header', function (t) {
t.plan(1)
await t.test('should emit error 416 with content-range header', async function (t) {
const server = http.createServer(async function (req, res) {

@@ -962,16 +839,14 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(server)
await request(server)
.get('/nums.txt')
.set('Range', 'bytes=9-50')
.expect('X-Content-Range', 'bytes */9')
.expect(416, err => t.error(err))
.expect(416)
})
})
t.test('when syntactically invalid', function (t) {
await t.test('when syntactically invalid', async function (t) {
t.plan(1)
t.test('should respond with 200 and the entire contents', function (t) {
t.plan(1)
await t.test('should respond with 200 and the entire contents', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -983,14 +858,14 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'asdf')
.expect(200, '123456789', err => t.error(err))
.expect(200, '123456789')
})
})
t.test('when multiple ranges', function (t) {
await t.test('when multiple ranges', async function (t) {
t.plan(2)
t.test('should respond with 200 and the entire contents', function (t) {
t.plan(2)
await t.test('should respond with 200 and the entire contents', async function (t) {
t.plan(1)

@@ -1003,12 +878,10 @@ const app = http.createServer(async function (req, res) {

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=1-1,3-')
.expect(shouldNotHaveHeader('Content-Range', t))
.expect(200, '123456789', err => t.error(err))
.expect(200, '123456789')
})
t.test('should respond with 206 is all ranges can be combined', function (t) {
t.plan(1)
await t.test('should respond with 206 is all ranges can be combined', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -1020,16 +893,14 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('Range', 'bytes=1-2,3-5')
.expect('Content-Range', 'bytes 1-5/9')
.expect(206, '23456', err => t.error(err))
.expect(206, '23456')
})
})
t.test('when if-range present', function (t) {
await t.test('when if-range present', async function (t) {
t.plan(5)
t.test('should respond with parts when etag unchanged', function (t) {
t.plan(2)
await t.test('should respond with parts when etag unchanged', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -1041,19 +912,16 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/nums.txt')
.expect(200, function (err, res) {
t.error(err)
const etag = res.headers.etag
.expect(200)
request(app)
.get('/nums.txt')
.set('If-Range', etag)
.set('Range', 'bytes=0-0')
.expect(206, '1', err => t.error(err))
})
const etag = res.headers.etag
await request(app)
.get('/nums.txt')
.set('If-Range', etag)
.set('Range', 'bytes=0-0')
.expect(206, '1')
})
t.test('should respond with 200 when etag changed', function (t) {
t.plan(2)
await t.test('should respond with 200 when etag changed', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -1065,19 +933,16 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/nums.txt')
.expect(200, function (err, res) {
t.error(err)
const etag = res.headers.etag.replace(/"(.)/, '"0$1')
.expect(200)
request(app)
.get('/nums.txt')
.set('If-Range', etag)
.set('Range', 'bytes=0-0')
.expect(200, '123456789', err => t.error(err))
})
const etag = res.headers.etag.replace(/"(.)/, '"0$1')
await request(app)
.get('/nums.txt')
.set('If-Range', etag)
.set('Range', 'bytes=0-0')
.expect(200, '123456789')
})
t.test('should respond with parts when modified unchanged', function (t) {
t.plan(2)
await t.test('should respond with parts when modified unchanged', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -1089,19 +954,16 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/nums.txt')
.expect(200, function (err, res) {
t.error(err)
const modified = res.headers['last-modified']
.expect(200)
request(app)
.get('/nums.txt')
.set('If-Range', modified)
.set('Range', 'bytes=0-0')
.expect(206, '1', err => t.error(err))
})
const modified = res.headers['last-modified']
await request(app)
.get('/nums.txt')
.set('If-Range', modified)
.set('Range', 'bytes=0-0')
.expect(206, '1')
})
t.test('should respond with 200 when modified changed', function (t) {
t.plan(2)
await t.test('should respond with 200 when modified changed', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -1113,19 +975,16 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
const res = await request(app)
.get('/nums.txt')
.expect(200, function (err, res) {
t.error(err)
const modified = Date.parse(res.headers['last-modified']) - 20000
.expect(200)
request(app)
.get('/nums.txt')
.set('If-Range', new Date(modified).toUTCString())
.set('Range', 'bytes=0-0')
.expect(200, '123456789', err => t.error(err))
})
const modified = Date.parse(res.headers['last-modified']) - 20000
await request(app)
.get('/nums.txt')
.set('If-Range', new Date(modified).toUTCString())
.set('Range', 'bytes=0-0')
.expect(200, '123456789')
})
t.test('should respond with 200 when invalid value', function (t) {
t.plan(1)
await t.test('should respond with 200 when invalid value', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -1137,7 +996,7 @@ const { statusCode, headers, stream } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/nums.txt')
.set('If-Range', 'foo')
.set('Range', 'bytes=0-0')
.expect(200, '123456789', err => t.error(err))
.expect(200, '123456789')
})

@@ -1147,40 +1006,32 @@ })

t.test('when "options" is specified', function (t) {
await t.test('when "options" is specified', async function (t) {
t.plan(4)
t.test('should support start/end', function (t) {
t.plan(1)
request(createServer({ root: fixtures, start: 3, end: 5 }))
await t.test('should support start/end', async function (t) {
await request(createServer({ root: fixtures, start: 3, end: 5 }))
.get('/nums.txt')
.expect(200, '456', err => t.error(err))
.expect(200, '456')
})
t.test('should adjust too large end', function (t) {
t.plan(1)
request(createServer({ root: fixtures, start: 3, end: 90 }))
await t.test('should adjust too large end', async function (t) {
await request(createServer({ root: fixtures, start: 3, end: 90 }))
.get('/nums.txt')
.expect(200, '456789', err => t.error(err))
.expect(200, '456789')
})
t.test('should support start/end with Range request', function (t) {
t.plan(1)
request(createServer({ root: fixtures, start: 0, end: 2 }))
await t.test('should support start/end with Range request', async function (t) {
await request(createServer({ root: fixtures, start: 0, end: 2 }))
.get('/nums.txt')
.set('Range', 'bytes=-2')
.expect(206, '23', err => t.error(err))
.expect(206, '23')
})
t.test('should support start/end with unsatisfiable Range request', function (t) {
t.plan(1)
request(createServer({ root: fixtures, start: 0, end: 2 }))
await t.test('should support start/end with unsatisfiable Range request', async function (t) {
await request(createServer({ root: fixtures, start: 0, end: 2 }))
.get('/nums.txt')
.set('Range', 'bytes=5-9')
.expect('Content-Range', 'bytes */3')
.expect(416, err => t.error(err))
.expect(416)
})
})
})
'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const http = require('node:http')

@@ -12,15 +12,15 @@ const path = require('node:path')

test('send(file)', function (t) {
test('send(file)', async function (t) {
t.plan(5)
t.test('file type', function (t) {
t.plan(6)
await t.test('file type', async function (t) {
t.plan(5)
const app = http.createServer(async function (req, res) {
const { statusCode, headers, stream, type, metadata } = await send(req, req.url, { root: fixtures })
t.equal(type, 'file')
t.ok(metadata.path)
t.ok(metadata.stat)
t.notOk(metadata.error)
t.notOk(metadata.requestPath)
t.assert.deepStrictEqual(type, 'file')
t.assert.ok(metadata.path)
t.assert.ok(metadata.stat)
t.assert.ok(!metadata.error)
t.assert.ok(!metadata.requestPath)
res.writeHead(statusCode, headers)

@@ -30,18 +30,18 @@ stream.pipe(res)

request(app)
await request(app)
.get('/name.txt')
.expect('Content-Length', '4')
.expect(200, 'tobi', err => t.error(err))
.expect(200, 'tobi')
})
t.test('directory type', function (t) {
t.plan(6)
await t.test('directory type', async function (t) {
t.plan(5)
const app = http.createServer(async function (req, res) {
const { statusCode, headers, stream, type, metadata } = await send(req, req.url, { root: fixtures })
t.equal(type, 'directory')
t.ok(metadata.path)
t.notOk(metadata.stat)
t.notOk(metadata.error)
t.ok(metadata.requestPath)
t.assert.deepStrictEqual(type, 'directory')
t.assert.ok(metadata.path)
t.assert.ok(!metadata.stat)
t.assert.ok(!metadata.error)
t.assert.ok(metadata.requestPath)
res.writeHead(statusCode, headers)

@@ -51,18 +51,18 @@ stream.pipe(res)

request(app)
await request(app)
.get('/pets')
.expect('Location', '/pets/')
.expect(301, err => t.error(err))
.expect(301)
})
t.test('error type', function (t) {
t.plan(6)
await t.test('error type', async function (t) {
t.plan(5)
const app = http.createServer(async function (req, res) {
const { statusCode, headers, stream, type, metadata } = await send(req, req.url, { root: fixtures })
t.equal(type, 'error')
t.notOk(metadata.path)
t.notOk(metadata.stat)
t.ok(metadata.error)
t.notOk(metadata.requestPath)
t.assert.deepStrictEqual(type, 'error')
t.assert.ok(!metadata.path)
t.assert.ok(!metadata.stat)
t.assert.ok(metadata.error)
t.assert.ok(!metadata.requestPath)
res.writeHead(statusCode, headers)

@@ -73,10 +73,8 @@ stream.pipe(res)

const path = Array(100).join('foobar')
request(app)
await request(app)
.get('/' + path)
.expect(404, err => t.error(err))
.expect(404)
})
t.test('custom directory index view', function (t) {
t.plan(1)
await t.test('custom directory index view', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -94,11 +92,9 @@ const { statusCode, headers, stream, type, metadata } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/pets')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, '.hidden\nindex.html\n', err => t.error(err))
.expect(200, '.hidden.txt\nindex.html\n')
})
t.test('serving from a root directory with custom error-handling', function (t) {
t.plan(3)
await t.test('serving from a root directory with custom error-handling', async function (t) {
const app = http.createServer(async function (req, res) {

@@ -128,16 +124,16 @@ const { statusCode, headers, stream, type, metadata } = await send(req, req.url, { root: fixtures })

request(app)
await request(app)
.get('/pets')
.expect('Location', '/pets/')
.expect(301, err => t.error(err))
.expect(301)
request(app)
await request(app)
.get('/not-exists')
.expect(404, err => t.error(err))
.expect(404)
request(app)
await request(app)
.get('/pets/index.html')
.expect('Content-Disposition', 'attachment')
.expect(200, err => t.error(err))
.expect(200)
})
})

@@ -8,3 +8,3 @@ 'use strict'

return function (res) {
t.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)
t.assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)
}

@@ -15,3 +15,3 @@ }

return function (res) {
t.ok((header.toLowerCase() in res.headers), 'should have header ' + header)
t.assert.ok((header.toLowerCase() in res.headers), 'should have header ' + header)
}

@@ -23,3 +23,3 @@ }

try {
fn && fn(req, res)
fn?.(req, res)
const { statusCode, headers, stream } = await send(req, req.url, opts)

@@ -37,4 +37,4 @@ res.writeHead(statusCode, headers)

return function (res) {
t.ok(res.text === '' || res.text === undefined)
t.assert.ok(res.text === '' || res.text === undefined)
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet