Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions test/cache-interceptor/cache-store-test-utils.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -144,29 +143,29 @@ 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)
}

clock.tick(1500)

{
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
})
Expand Down Expand Up @@ -202,20 +201,20 @@ 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)
}

clock.tick(1500)

{
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)
}

/**
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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)
}
})
})
Expand Down Expand Up @@ -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])
}
}

Expand Down
41 changes: 20 additions & 21 deletions test/cache-interceptor/memory-cache-store-tests.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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' },
Expand All @@ -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
Expand All @@ -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 })

Expand All @@ -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 })

Expand Down Expand Up @@ -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')
})
Loading
Loading