Skip to content

Commit

Permalink
test(cache): added test case for customizability
Browse files Browse the repository at this point in the history
  • Loading branch information
MathurAditya724 committed May 12, 2024
1 parent 936d691 commit 690910a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/middleware/cache/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,73 @@ class Context implements ExecutionContext {
}
}

describe('Customizing Caching Keys', () => {
const app = new Hono()

const dynamicCacheName = 'dynamic-cache-name'
app.use(
'/dynamic-cache-name/*',
cache({
cacheName: async () => dynamicCacheName,
wait: true,
cacheControl: 'max-age=10',
})
)
app.get('/dynamic-cache-name/', (c) => {
return c.text('cached')
})

const dynamicCacheKey = 'dynamic-cache-key'
app.use(
'/dynamic-cache-key/*',
cache({
cacheName: 'my-app-v1',
wait: true,
cacheControl: 'max-age=10',
keyGenerator: async () => dynamicCacheKey,
})
)
app.get('/dynamic-cache-key/', (c) => {
return c.text('cached')
})

app.use(
'/dynamic-cache/*',
cache({
cacheName: async () => dynamicCacheName,
cacheControl: 'max-age=10',
keyGenerator: async () => dynamicCacheKey,
})
)
app.get('/dynamic-cache/', (c) => {
return c.text('cached')
})

const ctx = new Context()

it('Should use dynamically generated cache name', async () => {
await app.request('http://localhost/dynamic-cache-name/', undefined, ctx)
const cache = await caches.open(dynamicCacheName)
const keys = Array.from(await cache.keys())
expect(keys.length).toBe(1)
})

it('Should use dynamically generated cache key', async () => {
await app.request('http://localhost/dynamic-cache-key/')
const cache = await caches.open('my-app-v1')
const response = await cache.match(dynamicCacheKey)
expect(response).not.toBeNull()
})

it('Should retrieve cached response with dynamic cache name and key', async () => {
await app.request('http://localhost/dynamic-cache/', undefined, undefined, ctx)
const res = await app.request('http://localhost/dynamic-cache/', undefined, undefined, ctx)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('max-age=10')
})
})

describe('Cache Middleware', () => {
const app = new Hono()

Expand Down
4 changes: 4 additions & 0 deletions src/test-utils/setup-vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class MockCache {
return this.store.get(key) || null
}

async keys() {
return this.store.keys()
}

async put(key: Request | string, response: Response): Promise<void> {
this.store.set(key, response)
}
Expand Down

0 comments on commit 690910a

Please sign in to comment.