Skip to content

Commit

Permalink
also test compression on cached api handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dulnan committed Aug 25, 2024
1 parent f614321 commit b100484
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
10 changes: 10 additions & 0 deletions playground/server/api/testApiCompression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineEventHandler } from 'h3'
import { useRouteCache } from '#nuxt-multi-cache/composables'

export default defineEventHandler((event) => {
useRouteCache((helper) => {
helper.setCacheable().setMaxAge(234234)
}, event)
const number = Math.round(Math.random() * 1000000000)
return 'This is a compressed API response: ' + number
})
5 changes: 4 additions & 1 deletion playground/server/plugins/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { defineNitroPlugin } from 'nitropack/runtime'

export default defineNitroPlugin((nitro) => {
nitro.hooks.hook('beforeResponse', async (event, response) => {
if (!event.path.startsWith('/testCompression')) {
if (
!event.path.startsWith('/testCompression') &&
!event.path.startsWith('/api/testApiCompression')
) {
return
}

Expand Down
66 changes: 37 additions & 29 deletions test/routeCacheWithCompression.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,50 @@ await setup({
nuxtConfig,
})

describe.only('The route cache with compression enabled', () => {
test('caches a page', async () => {
await purgeAll()
async function testResponse(path: string) {
// First call puts it into cache.
const first = await fetch(path, {
method: 'get',
headers: {
'accept-encoding': 'br',
},
})

// Get the encoding.
const firstEncoding = first.headers.get('content-encoding')

// First call puts it into cache.
const first = await fetch('/testCompression', {
method: 'get',
headers: {
'accept-encoding': 'br',
},
})
// Test that the compression feature has actually compressed the response.
expect(firstEncoding).toEqual('br')

// Get the encoding.
const firstEncoding = first.headers.get('content-encoding')
// Second call should get it from cache.
const second = await fetch(path, {
method: 'get',
headers: {
'accept-encoding': 'br',
},
})

// Test that the compression feature has actually compressed the response.
expect(firstEncoding).toEqual('br')
const responseFirst = await first.text()
const responseSecond = await second.text()

// Second call should get it from cache.
const second = await fetch('/testCompression', {
method: 'get',
headers: {
'accept-encoding': 'br',
},
})
// Response should be identical (contains a random number).
expect(responseFirst).toEqual(responseSecond)

const responseFirst = await first.text()
const responseSecond = await second.text()
const secondEncoding = second.headers.get('content-encoding')

// Response should be identical (contains a random number).
expect(responseFirst).toEqual(responseSecond)
// The encoding should be the same, because the compression feature was
// able to compress the cached response again.
expect(secondEncoding).toEqual(firstEncoding)
}

const secondEncoding = second.headers.get('content-encoding')
describe.only('The route cache with compression enabled', () => {
test('caches a page', async () => {
await purgeAll()
await testResponse('/testCompression')
})

// The encoding should be the same, because the compression feature was
// able to compress the cached response again.
expect(secondEncoding).toEqual(firstEncoding)
test('caches the result of an API handler', async () => {
await purgeAll()
await testResponse('/api/testApiCompression')
})
})

0 comments on commit b100484

Please sign in to comment.