Skip to content

Commit

Permalink
test: migration to node test runner (#139)
Browse files Browse the repository at this point in the history
* chore: migrated to node test runner

* chore: removed tap

* fix: assertions

* chore: use c8
  • Loading branch information
ilteoood authored Jan 7, 2025
1 parent 9bf95d8 commit 81d6e1a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 122 deletions.
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:unit": "c8 --100 node --test",
"test:typescript": "tsd"
},
"repository": {
Expand Down Expand Up @@ -62,10 +62,10 @@
"devDependencies": {
"@types/node": "^22.0.0",
"autocannon": "^8.0.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"tap": "^18.7.1",
"tsd": "^0.31.0"
},
"dependencies": {
Expand Down
48 changes: 20 additions & 28 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { createHash } = require('node:crypto')
const generic = require('./generic')
const fnv1a = require('../fnv1a')
Expand All @@ -9,35 +9,27 @@ const Etag = require('..')

test('should throw an Error if used non valid algorithm', async t => {
const app = Fastify()
t.rejects(() => Etag(app, { algorithm: 'invalid' }), 'Algorithm invalid not supported.')
await t.assert.rejects(() => Etag(app, { algorithm: 'invalid' }), new TypeError('Algorithm invalid not supported.'))
})

test('strong md5', async (t) => {
generic(t, {
algorithm: 'md5'
}, (body) => {
return '"' + createHash('md5').update(body).digest().toString('base64') + '"'
})
})
test('strong md5', (t) => generic(t, {
algorithm: 'md5'
}, (body) => {
return '"' + createHash('md5').update(body).digest().toString('base64') + '"'
}))

test('strong sha1', async (t) => {
generic(t, {
algorithm: 'sha1'
}, (body) => {
return '"' + createHash('sha1').update(body).digest().toString('base64') + '"'
})
})
test('strong sha1', (t) => generic(t, {
algorithm: 'sha1'
}, (body) => {
return '"' + createHash('sha1').update(body).digest().toString('base64') + '"'
}))

test('weak fnv1a', async (t) => {
generic(t, {
algorithm: 'fnv1a'
}, (body) => {
return '"' + fnv1a(body).toString(36) + '"'
})
})
test('weak fnv1a', (t) => generic(t, {
algorithm: 'fnv1a'
}, (body) => {
return '"' + fnv1a(body).toString(36) + '"'
}))

test('default -> sha1', async (t) => {
generic(t, {}, (body) => {
return '"' + createHash('sha1').update(body).digest().toString('base64') + '"'
})
})
test('default -> sha1', (t) => generic(t, {}, (body) => {
return '"' + createHash('sha1').update(body).digest().toString('base64') + '"'
}))
60 changes: 30 additions & 30 deletions test/fnv1a.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,49 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const { test } = require('tap')
const { test } = require('node:test')
const fnv1a = require('../fnv1a')

test('string', async t => {
t.equal(fnv1a(''), 2166136261)
t.equal(fnv1a('🦄🌈'), 582881315)
t.assert.deepStrictEqual(fnv1a(''), 2166136261)
t.assert.deepStrictEqual(fnv1a('🦄🌈'), 582881315)

t.equal(fnv1a('h'), 3977000791)
t.equal(fnv1a('he'), 1547363254)
t.equal(fnv1a('hel'), 179613742)
t.equal(fnv1a('hell'), 477198310)
t.equal(fnv1a('hello'), 1335831723)
t.equal(fnv1a('hello '), 3801292497)
t.equal(fnv1a('hello w'), 1402552146)
t.equal(fnv1a('hello wo'), 3611200775)
t.equal(fnv1a('hello wor'), 1282977583)
t.equal(fnv1a('hello worl'), 2767971961)
t.equal(fnv1a('hello world'), 3582672807)
t.assert.deepStrictEqual(fnv1a('h'), 3977000791)
t.assert.deepStrictEqual(fnv1a('he'), 1547363254)
t.assert.deepStrictEqual(fnv1a('hel'), 179613742)
t.assert.deepStrictEqual(fnv1a('hell'), 477198310)
t.assert.deepStrictEqual(fnv1a('hello'), 1335831723)
t.assert.deepStrictEqual(fnv1a('hello '), 3801292497)
t.assert.deepStrictEqual(fnv1a('hello w'), 1402552146)
t.assert.deepStrictEqual(fnv1a('hello wo'), 3611200775)
t.assert.deepStrictEqual(fnv1a('hello wor'), 1282977583)
t.assert.deepStrictEqual(fnv1a('hello worl'), 2767971961)
t.assert.deepStrictEqual(fnv1a('hello world'), 3582672807)

t.equal(fnv1a('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.'), 2964896417)
t.assert.deepStrictEqual(fnv1a('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.'), 2964896417)
})

test('buffer', async t => {
t.equal(fnv1a(Buffer.from('')), 2166136261)
t.equal(fnv1a(Buffer.from('🦄🌈')), 2868248295)
t.assert.deepStrictEqual(fnv1a(Buffer.from('')), 2166136261)
t.assert.deepStrictEqual(fnv1a(Buffer.from('🦄🌈')), 2868248295)

t.equal(fnv1a(Buffer.from('h')), 3977000791)
t.equal(fnv1a(Buffer.from('he')), 1547363254)
t.equal(fnv1a(Buffer.from('hel')), 179613742)
t.equal(fnv1a(Buffer.from('hell')), 477198310)
t.equal(fnv1a(Buffer.from('hello')), 1335831723)
t.equal(fnv1a(Buffer.from('hello ')), 3801292497)
t.equal(fnv1a(Buffer.from('hello w')), 1402552146)
t.equal(fnv1a(Buffer.from('hello wo')), 3611200775)
t.equal(fnv1a(Buffer.from('hello wor')), 1282977583)
t.equal(fnv1a(Buffer.from('hello worl')), 2767971961)
t.equal(fnv1a(Buffer.from('hello world')), 3582672807)
t.assert.deepStrictEqual(fnv1a(Buffer.from('h')), 3977000791)
t.assert.deepStrictEqual(fnv1a(Buffer.from('he')), 1547363254)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hel')), 179613742)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hell')), 477198310)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello')), 1335831723)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello ')), 3801292497)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello w')), 1402552146)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello wo')), 3611200775)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello wor')), 1282977583)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello worl')), 2767971961)
t.assert.deepStrictEqual(fnv1a(Buffer.from('hello world')), 3582672807)

t.equal(fnv1a(Buffer.from('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.')), 2964896417)
t.assert.deepStrictEqual(fnv1a(Buffer.from('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.')), 2964896417)
})

test('can\'t hash objects', async t => {
t.throws(() => {
t.assert.throws(() => {
fnv1a({})
})
})
102 changes: 42 additions & 60 deletions test/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Fastify = require('fastify')
const Etag = require('..')
const { createReadStream } = require('node:fs')

module.exports = function ({ test }, etagOpts, hashFn) {
module.exports = async function (t, etagOpts, hashFn) {
function build (opts = {}) {
const app = Fastify()
app.register(Etag, Object.assign({}, opts, etagOpts))
Expand All @@ -27,140 +27,122 @@ module.exports = function ({ test }, etagOpts, hashFn) {

const hash = hashFn(JSON.stringify({ hello: 'world' }))

test('returns an etag for each request', async (t) => {
await t.test('returns an etag for each request', async (t) => {
const res = await build().inject({
url: '/'
})

t.same(JSON.parse(res.body), { hello: 'world' })
t.match(res.headers, {
etag: hash
})
t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' })
t.assert.deepStrictEqual(res.headers.etag, hash)
})

test('returns a 304 if etag matches', async (t) => {
await t.test('returns a 304 if etag matches', async (t) => {
const res = await build().inject({
url: '/',
headers: {
'If-None-Match': hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: hash
})
t.assert.deepStrictEqual(res.statusCode, 304)
t.assert.deepStrictEqual(res.body, '')
t.assert.deepStrictEqual(res.headers.etag, hash)
t.assert.deepStrictEqual(res.headers['content-length'], '0')
})

test('does not return a 304 when behaviour is disabled', async (t) => {
await t.test('does not return a 304 when behaviour is disabled', async (t) => {
const res = await build({ replyWith304: false }).inject({
url: '/',
headers: {
'If-None-Match': hash
}
})

t.same(JSON.parse(res.body), { hello: 'world' })
t.equal(res.statusCode, 200)
t.match(res.headers, {
etag: hash
})
t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' })
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.etag, hash)
})

test('returns an already existing etag', async (t) => {
await t.test('returns an already existing etag', async (t) => {
const res = await build().inject({
url: '/etag'
})

t.equal(res.statusCode, 200)
t.match(res.headers, {
etag: '"foobar"'
})
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.etag, '"foobar"')
})

test('does not generate etags for streams', async (t) => {
await t.test('does not generate etags for streams', async (t) => {
const res = await build().inject({
url: '/stream'
})

t.equal(res.statusCode, 200)
t.equal(res.headers.etag, undefined)
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.headers.etag, undefined)
})

test('returns a 304 if etag matches and it is provided by the route', async (t) => {
await t.test('returns a 304 if etag matches and it is provided by the route', async (t) => {
const res = await build().inject({
url: '/etag',
headers: {
'If-None-Match': '"foobar"'
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: '"foobar"'
})
t.assert.deepStrictEqual(res.statusCode, 304)
t.assert.deepStrictEqual(res.body, '')
t.assert.deepStrictEqual(res.headers.etag, '"foobar"')
t.assert.deepStrictEqual(res.headers['content-length'], '0')
})

test('returns a weak etag for each request when weak is in opts', async (t) => {
await t.test('returns a weak etag for each request when weak is in opts', async (t) => {
const res = await build({ weak: true }).inject({
url: '/'
})

t.same(JSON.parse(res.body), { hello: 'world' })
t.match(res.headers, {
etag: 'W/' + hash
})
t.assert.deepStrictEqual(JSON.parse(res.body), { hello: 'world' })
t.assert.deepStrictEqual(res.headers.etag, 'W/' + hash)
})

test('returns a 304 if weak etag matches', async (t) => {
await t.test('returns a 304 if weak etag matches', async (t) => {
const res = await build({ weak: true }).inject({
url: '/',
headers: {
'If-None-Match': 'W/' + hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: 'W/' + hash
})
t.assert.deepStrictEqual(res.statusCode, 304)
t.assert.deepStrictEqual(res.body, '')
t.assert.deepStrictEqual(res.headers['content-length'], '0')
t.assert.deepStrictEqual(res.headers.etag, 'W/' + hash)
})

test('returns a 304 if etag is strong and If-None-Match is weak', async (t) => {
await t.test('returns a 304 if etag is strong and If-None-Match is weak', async (t) => {
const res = await build().inject({
url: '/',
headers: {
'If-None-Match': 'W/' + hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: hash
})
t.assert.deepStrictEqual(res.statusCode, 304)
t.assert.deepStrictEqual(res.body, '')
t.assert.deepStrictEqual(res.headers['content-length'], '0')
t.assert.deepStrictEqual(res.headers.etag, hash)
})

test('returns a 304 if etag is weak and If-None-Match is strong', async (t) => {
await t.test('returns a 304 if etag is weak and If-None-Match is strong', async (t) => {
const res = await build({ weak: true }).inject({
url: '/',
headers: {
'If-None-Match': hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: 'W/' + hash
})
t.assert.deepStrictEqual(res.statusCode, 304)
t.assert.deepStrictEqual(res.body, '')
t.assert.deepStrictEqual(res.headers['content-length'], '0')
t.assert.deepStrictEqual(res.headers.etag, 'W/' + hash)
})
}

0 comments on commit 81d6e1a

Please sign in to comment.