diff --git a/test/cache-interceptor/cache-store-test-utils.js b/test/cache-interceptor/cache-store-test-utils.js index 54616b89744..4ed243cf49e 100644 --- a/test/cache-interceptor/cache-store-test-utils.js +++ b/test/cache-interceptor/cache-store-test-utils.js @@ -1,6 +1,5 @@ 'use strict' -const { equal, notEqual, deepStrictEqual } = require('node:assert') const { describe, test, after } = require('node:test') const { Readable } = require('node:stream') const { once } = require('node:events') @@ -15,13 +14,13 @@ const FakeTimers = require('@sinonjs/fake-timers') */ function cacheStoreTests (CacheStore, options) { describe(CacheStore.prototype.constructor.name, () => { - test('matches interface', options, () => { - equal(typeof CacheStore.prototype.get, 'function') - equal(typeof CacheStore.prototype.createWriteStream, 'function') - equal(typeof CacheStore.prototype.delete, 'function') + test('matches interface', options, (t) => { + t.assert.strictEqual(typeof CacheStore.prototype.get, 'function') + t.assert.strictEqual(typeof CacheStore.prototype.createWriteStream, 'function') + t.assert.strictEqual(typeof CacheStore.prototype.delete, 'function') }) - test('caches request', options, async () => { + test('caches request', options, async (t) => { /** * @type {import('../../types/cache-interceptor.d.ts').default.CacheKey} */ @@ -50,20 +49,20 @@ function cacheStoreTests (CacheStore, options) { const store = new CacheStore() // Sanity check - equal(await store.get(key), undefined) + t.assert.strictEqual(await store.get(key), undefined) // Write response to store { const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } // Now let's try fetching the response from the store { const result = await store.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } /** @@ -93,22 +92,22 @@ function cacheStoreTests (CacheStore, options) { const anotherBody = [Buffer.from('asd'), Buffer.from('123')] - equal(store.get(anotherKey), undefined) + t.assert.strictEqual(store.get(anotherKey), undefined) { const writable = store.createWriteStream(anotherKey, anotherValue) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, anotherBody) } { const result = await store.get(structuredClone(anotherKey)) - notEqual(result, undefined) - await compareGetResults(result, anotherValue, anotherBody) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, anotherValue, anotherBody) } }) - test('returns stale response before deleteAt', options, async () => { + test('returns stale response before deleteAt', options, async (t) => { const clock = FakeTimers.install({ shouldClearNativeTimers: true }) @@ -144,11 +143,11 @@ function cacheStoreTests (CacheStore, options) { const store = new CacheStore() // Sanity check - equal(store.get(key), undefined) + t.assert.strictEqual(store.get(key), undefined) { const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } @@ -156,17 +155,17 @@ function cacheStoreTests (CacheStore, options) { { const result = await store.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } clock.tick(6000) // Past deleteAt, shouldn't be returned - equal(await store.get(key), undefined) + t.assert.strictEqual(await store.get(key), undefined) }) - test('a stale request is overwritten', options, async () => { + test('a stale request is overwritten', options, async (t) => { const clock = FakeTimers.install({ shouldClearNativeTimers: true }) @@ -202,11 +201,11 @@ function cacheStoreTests (CacheStore, options) { const store = new CacheStore() // Sanity check - equal(store.get(key), undefined) + t.assert.strictEqual(store.get(key), undefined) { const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } @@ -214,8 +213,8 @@ function cacheStoreTests (CacheStore, options) { { const result = await store.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } /** @@ -236,18 +235,18 @@ function cacheStoreTests (CacheStore, options) { { const writable = store.createWriteStream(key, value2) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body2) } { const result = await store.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value2, body2) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value2, body2) } }) - test('vary directives used to decide which response to use', options, async () => { + test('vary directives used to decide which response to use', options, async (t) => { /** * @type {import('../../types/cache-interceptor.d.ts').default.CacheKey} */ @@ -281,18 +280,18 @@ function cacheStoreTests (CacheStore, options) { const store = new CacheStore() // Sanity check - equal(store.get(key), undefined) + t.assert.strictEqual(store.get(key), undefined) { const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } { const result = await store.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } /** @@ -327,24 +326,24 @@ function cacheStoreTests (CacheStore, options) { const anotherBody = [Buffer.from('asd'), Buffer.from('123')] - equal(await store.get(anotherKey), undefined) + t.assert.strictEqual(await store.get(anotherKey), undefined) { const writable = store.createWriteStream(anotherKey, anotherValue) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, anotherBody) } { const result = await store.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } { const result = await store.get(structuredClone(anotherKey)) - notEqual(result, undefined) - await compareGetResults(result, anotherValue, anotherBody) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, anotherValue, anotherBody) } }) }) @@ -417,15 +416,15 @@ function joinBufferArray (buffers) { * @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} expected * @param {Buffer[]} expectedBody */ -async function compareGetResults (actual, expected, expectedBody) { +async function compareGetResults (t, actual, expected, expectedBody) { const actualBody = await readBody(actual) - deepStrictEqual( + t.assert.deepStrictEqual( actualBody ? joinBufferArray(actualBody) : undefined, joinBufferArray(expectedBody) ) for (const key of Object.keys(expected)) { - deepStrictEqual(actual[key], expected[key]) + t.assert.deepStrictEqual(actual[key], expected[key]) } } diff --git a/test/cache-interceptor/memory-cache-store-tests.js b/test/cache-interceptor/memory-cache-store-tests.js index e41fb9ad256..7f7708e157e 100644 --- a/test/cache-interceptor/memory-cache-store-tests.js +++ b/test/cache-interceptor/memory-cache-store-tests.js @@ -1,13 +1,12 @@ 'use strict' const { test } = require('node:test') -const { equal } = require('node:assert') const MemoryCacheStore = require('../../lib/cache/memory-cache-store') const { cacheStoreTests } = require('./cache-store-test-utils.js') cacheStoreTests(MemoryCacheStore) -test('default limits prevent memory leaks', async () => { +test('default limits prevent memory leaks', async (t) => { const store = new MemoryCacheStore() // Uses new defaults // Test that maxCount default (1024) is enforced @@ -28,10 +27,10 @@ test('default limits prevent memory leaks', async () => { } // Should be full after exceeding maxCount default of 1024 - equal(store.isFull(), true, 'Store should be full after exceeding maxCount default') + t.assert.strictEqual(store.isFull(), true, 'Store should be full after exceeding maxCount default') }) -test('default maxEntrySize prevents large entries', async () => { +test('default maxEntrySize prevents large entries', async (t) => { const store = new MemoryCacheStore() // Uses new defaults // Create entry larger than default maxEntrySize (5MB) @@ -54,14 +53,14 @@ test('default maxEntrySize prevents large entries', async () => { // Entry should not be cached due to maxEntrySize limit const result = store.get({ origin: 'test', path: '/large', method: 'GET', headers: {} }) - equal(result, undefined, 'Large entry should not be cached due to maxEntrySize limit') + t.assert.strictEqual(result, undefined, 'Large entry should not be cached due to maxEntrySize limit') }) -test('size getter returns correct total size', async () => { +test('size getter returns correct total size', async (t) => { const store = new MemoryCacheStore() const testData = 'test data' - equal(store.size, 0, 'Initial size should be 0') + t.assert.strictEqual(store.size, 0, 'Initial size should be 0') const writeStream = store.createWriteStream( { origin: 'test', path: '/', method: 'GET' }, @@ -78,19 +77,19 @@ test('size getter returns correct total size', async () => { writeStream.write(testData) writeStream.end() - equal(store.size, testData.length, 'Size should match written data length') + t.assert.strictEqual(store.size, testData.length, 'Size should match written data length') }) -test('isFull returns false when under limits', () => { +test('isFull returns false when under limits', (t) => { const store = new MemoryCacheStore({ maxSize: 1000, maxCount: 10 }) - equal(store.isFull(), false, 'Should not be full when empty') + t.assert.strictEqual(store.isFull(), false, 'Should not be full when empty') }) -test('isFull returns true when maxSize reached', async () => { +test('isFull returns true when maxSize reached', async (t) => { const maxSize = 10 const store = new MemoryCacheStore({ maxSize }) const testData = 'x'.repeat(maxSize + 1) // Exceed maxSize @@ -110,10 +109,10 @@ test('isFull returns true when maxSize reached', async () => { writeStream.write(testData) writeStream.end() - equal(store.isFull(), true, 'Should be full when maxSize exceeded') + t.assert.strictEqual(store.isFull(), true, 'Should be full when maxSize exceeded') }) -test('isFull returns true when maxCount reached', async () => { +test('isFull returns true when maxCount reached', async (t) => { const maxCount = 2 const store = new MemoryCacheStore({ maxCount }) @@ -133,10 +132,10 @@ test('isFull returns true when maxCount reached', async () => { writeStream.end('test') } - equal(store.isFull(), true, 'Should be full when maxCount exceeded') + t.assert.strictEqual(store.isFull(), true, 'Should be full when maxCount exceeded') }) -test('emits maxSizeExceeded event when limits exceeded', async () => { +test('emits maxSizeExceeded event when limits exceeded', async (t) => { const maxSize = 10 const store = new MemoryCacheStore({ maxSize }) @@ -165,10 +164,10 @@ test('emits maxSizeExceeded event when limits exceeded', async () => { writeStream.write(testData) writeStream.end() - equal(eventFired, true, 'maxSizeExceeded event should fire') - equal(typeof eventPayload, 'object', 'Event should have payload') - equal(typeof eventPayload.size, 'number', 'Payload should have size') - equal(typeof eventPayload.maxSize, 'number', 'Payload should have maxSize') - equal(typeof eventPayload.count, 'number', 'Payload should have count') - equal(typeof eventPayload.maxCount, 'number', 'Payload should have maxCount') + t.assert.strictEqual(eventFired, true, 'maxSizeExceeded event should fire') + t.assert.strictEqual(typeof eventPayload, 'object', 'Event should have payload') + t.assert.strictEqual(typeof eventPayload.size, 'number', 'Payload should have size') + t.assert.strictEqual(typeof eventPayload.maxSize, 'number', 'Payload should have maxSize') + t.assert.strictEqual(typeof eventPayload.count, 'number', 'Payload should have count') + t.assert.strictEqual(typeof eventPayload.maxCount, 'number', 'Payload should have maxCount') }) diff --git a/test/cache-interceptor/sqlite-cache-store-tests.js b/test/cache-interceptor/sqlite-cache-store-tests.js index 47cf5651687..325cffc8948 100644 --- a/test/cache-interceptor/sqlite-cache-store-tests.js +++ b/test/cache-interceptor/sqlite-cache-store-tests.js @@ -1,7 +1,6 @@ 'use strict' const { test } = require('node:test') -const { notEqual, strictEqual, deepStrictEqual } = require('node:assert') const { rm } = require('node:fs/promises') const { cacheStoreTests, writeBody, compareGetResults } = require('./cache-store-test-utils.js') const { runtimeFeatures } = require('../../lib/util/runtime-features.js') @@ -53,26 +52,26 @@ test('SqliteCacheStore works nicely with multiple stores', { skip: runtimeFeatur { const writable = storeA.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } // Make sure we got the expected response from store a { const result = storeA.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } // Make sure we got the expected response from store b { const result = storeB.get(structuredClone(key)) - notEqual(result, undefined) - await compareGetResults(result, value, body) + t.assert.notEqual(result, undefined) + await compareGetResults(t, result, value, body) } }) -test('SqliteCacheStore maxEntries', { skip: runtimeFeatures.has('sqlite') === false }, async () => { +test('SqliteCacheStore maxEntries', { skip: runtimeFeatures.has('sqlite') === false }, async (t) => { const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js') const store = new SqliteCacheStore({ @@ -105,14 +104,14 @@ test('SqliteCacheStore maxEntries', { skip: runtimeFeatures.has('sqlite') === fa const body = ['asd', '123'] const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } - strictEqual(store.size <= 11, true) + t.assert.strictEqual(store.size <= 11, true) }) -test('SqliteCacheStore two writes', { skip: runtimeFeatures.has('sqlite') === false }, async () => { +test('SqliteCacheStore two writes', { skip: runtimeFeatures.has('sqlite') === false }, async (t) => { const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js') const store = new SqliteCacheStore({ @@ -145,18 +144,18 @@ test('SqliteCacheStore two writes', { skip: runtimeFeatures.has('sqlite') === fa { const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } { const writable = store.createWriteStream(key, value) - notEqual(writable, undefined) + t.assert.notEqual(writable, undefined) writeBody(writable, body) } }) -test('SqliteCacheStore write & read', { skip: runtimeFeatures.has('sqlite') === false }, async () => { +test('SqliteCacheStore write & read', { skip: runtimeFeatures.has('sqlite') === false }, async (t) => { const SqliteCacheStore = require('../../lib/cache/sqlite-cache-store.js') const store = new SqliteCacheStore({ @@ -191,5 +190,5 @@ test('SqliteCacheStore write & read', { skip: runtimeFeatures.has('sqlite') === store.set(key, value) - deepStrictEqual(store.get(key), value) + t.assert.deepStrictEqual(store.get(key), value) }) diff --git a/test/cache-interceptor/utils.js b/test/cache-interceptor/utils.js index 2ae75d15489..a760ad807b1 100644 --- a/test/cache-interceptor/utils.js +++ b/test/cache-interceptor/utils.js @@ -1,15 +1,14 @@ 'use strict' const { describe, test } = require('node:test') -const { deepStrictEqual, equal } = require('node:assert') const { parseCacheControlHeader, parseVaryHeader, isEtagUsable } = require('../../lib/util/cache') describe('parseCacheControlHeader', () => { - test('all directives are parsed properly when in their correct format', () => { + test('all directives are parsed properly when in their correct format', (t) => { const directives = parseCacheControlHeader( 'max-stale=1, min-fresh=1, max-age=1, s-maxage=1, stale-while-revalidate=1, stale-if-error=1, public, private, no-store, no-cache, must-revalidate, proxy-revalidate, immutable, no-transform, must-understand, only-if-cached' ) - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-stale': 1, 'min-fresh': 1, 'max-age': 1, @@ -29,11 +28,11 @@ describe('parseCacheControlHeader', () => { }) }) - test('handles weird spacings', () => { + test('handles weird spacings', (t) => { const directives = parseCacheControlHeader( 'max-stale=1, min-fresh=1, max-age=1,s-maxage=1, stale-while-revalidate=1,stale-if-error=1,public,private' ) - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-stale': 1, 'min-fresh': 1, 'max-age': 1, @@ -45,29 +44,29 @@ describe('parseCacheControlHeader', () => { }) }) - test('unknown directives are ignored', () => { + test('unknown directives are ignored', (t) => { const directives = parseCacheControlHeader('max-age=123, something-else=456') - deepStrictEqual(directives, { 'max-age': 123 }) + t.assert.deepStrictEqual(directives, { 'max-age': 123 }) }) - test('directives with incorrect types are ignored', () => { + test('directives with incorrect types are ignored', (t) => { const directives = parseCacheControlHeader('max-age=true, only-if-cached=123') - deepStrictEqual(directives, {}) + t.assert.deepStrictEqual(directives, {}) }) - test('the last instance of a directive takes precedence', () => { + test('the last instance of a directive takes precedence', (t) => { const directives = parseCacheControlHeader('max-age=1, max-age=2') - deepStrictEqual(directives, { 'max-age': 2 }) + t.assert.deepStrictEqual(directives, { 'max-age': 2 }) }) - test('case insensitive', () => { + test('case insensitive', (t) => { const directives = parseCacheControlHeader('Max-Age=123') - deepStrictEqual(directives, { 'max-age': 123 }) + t.assert.deepStrictEqual(directives, { 'max-age': 123 }) }) - test('no-cache with headers', () => { + test('no-cache with headers', (t) => { let directives = parseCacheControlHeader('max-age=10, no-cache=some-header, only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, 'no-cache': [ 'some-header' @@ -76,7 +75,7 @@ describe('parseCacheControlHeader', () => { }) directives = parseCacheControlHeader('max-age=10, no-cache="some-header", only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, 'no-cache': [ 'some-header' @@ -85,7 +84,7 @@ describe('parseCacheControlHeader', () => { }) directives = parseCacheControlHeader('max-age=10, no-cache="some-header, another-one", only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, 'no-cache': [ 'some-header', @@ -95,9 +94,9 @@ describe('parseCacheControlHeader', () => { }) }) - test('private with headers', () => { + test('private with headers', (t) => { let directives = parseCacheControlHeader('max-age=10, private=some-header, only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, private: [ 'some-header' @@ -106,7 +105,7 @@ describe('parseCacheControlHeader', () => { }) directives = parseCacheControlHeader('max-age=10, private="some-header", only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, private: [ 'some-header' @@ -115,7 +114,7 @@ describe('parseCacheControlHeader', () => { }) directives = parseCacheControlHeader('max-age=10, private="some-header, another-one", only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, private: [ 'some-header', @@ -126,13 +125,13 @@ describe('parseCacheControlHeader', () => { // Missing ending quote, invalid & should be skipped directives = parseCacheControlHeader('max-age=10, private="some-header, another-one, only-if-cached') - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-age': 10, 'only-if-cached': true }) }) - test('handles multiple headers correctly', () => { + test('handles multiple headers correctly', (t) => { // For requests like // cache-control: max-stale=1 // cache-control: min-fresh-1 @@ -155,7 +154,7 @@ describe('parseCacheControlHeader', () => { 'must-understand', 'only-if-cached' ]) - deepStrictEqual(directives, { + t.assert.deepStrictEqual(directives, { 'max-stale': 1, 'min-fresh': 1, 'max-age': 1, @@ -177,76 +176,76 @@ describe('parseCacheControlHeader', () => { }) describe('parseVaryHeader', () => { - test('basic usage', () => { + test('basic usage', (t) => { const output = parseVaryHeader('some-header, another-one', { 'some-header': 'asd', 'another-one': '123', 'third-header': 'cool' }) - deepStrictEqual(output, { + t.assert.deepStrictEqual(output, { 'some-header': 'asd', 'another-one': '123' }) }) - test('handles weird spacings', () => { + test('handles weird spacings', (t) => { const output = parseVaryHeader('some-header, another-one,something-else', { 'some-header': 'asd', 'another-one': '123', 'something-else': 'asd123', 'third-header': 'cool' }) - deepStrictEqual(output, { + t.assert.deepStrictEqual(output, { 'some-header': 'asd', 'another-one': '123', 'something-else': 'asd123' }) }) - test('handles multiple headers correctly', () => { + test('handles multiple headers correctly', (t) => { const output = parseVaryHeader(['some-header', 'another-one'], { 'some-header': 'asd', 'another-one': '123', 'third-header': 'cool' }) - deepStrictEqual(output, { + t.assert.deepStrictEqual(output, { 'some-header': 'asd', 'another-one': '123' }) }) - test('handles missing headers with null', () => { + test('handles missing headers with null', (t) => { const result = parseVaryHeader('Accept-Encoding, Authorization', {}) - deepStrictEqual(result, { + t.assert.deepStrictEqual(result, { 'accept-encoding': null, authorization: null }) }) - test('handles mix of present and missing headers', () => { + test('handles mix of present and missing headers', (t) => { const result = parseVaryHeader('Accept-Encoding, Authorization', { authorization: 'example-value' }) - deepStrictEqual(result, { + t.assert.deepStrictEqual(result, { 'accept-encoding': null, authorization: 'example-value' }) }) - test('handles array input', () => { + test('handles array input', (t) => { const result = parseVaryHeader(['Accept-Encoding', 'Authorization'], { 'accept-encoding': 'gzip' }) - deepStrictEqual(result, { + t.assert.deepStrictEqual(result, { 'accept-encoding': 'gzip', authorization: null }) }) - test('preserves existing * behavior', () => { + test('preserves existing * behavior', (t) => { const headers = { accept: 'text/html' } const result = parseVaryHeader('*', headers) - deepStrictEqual(result, headers) + t.assert.deepStrictEqual(result, headers) }) }) @@ -269,8 +268,8 @@ describe('isEtagUsable', () => { for (const key in valuesToTest) { const expectedValue = valuesToTest[key] - test(`\`${key}\` = ${expectedValue}`, () => { - equal(isEtagUsable(key), expectedValue) + test(`\`${key}\` = ${expectedValue}`, (t) => { + t.assert.strictEqual(isEtagUsable(key), expectedValue) }) } })