Skip to content

Commit

Permalink
Merge pull request #5 from vermaysha/fix/multiple-vary-on-wildcard-vary
Browse files Browse the repository at this point in the history
Fix/multiple vary on wildcard vary
  • Loading branch information
vermaysha authored Jun 4, 2024
2 parents 2689a06 + aee4421 commit 9f5cf1a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ export const compression = (
const headerValueArray = Array.isArray(rawHeaderValue)
? rawHeaderValue
: [rawHeaderValue]
if (!headerValueArray.some((h) => h.includes('accept-encoding'))) {

// Add accept-encoding header if it doesn't exist
// and if vary not set to *
if (
!headerValueArray.some((h) => h.includes('accept-encoding')) &&
!headerValueArray.includes('*')
) {
headers.Vary = headerValueArray.concat('accept-encoding').join(', ')
}
} else {
Expand Down
10 changes: 10 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBeNull()
expect(res.headers.get('vary')).toBeNull()
})

it('handle brotli compression', async () => {
Expand All @@ -45,6 +46,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('br')
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('handle deflate compression', async () => {
Expand All @@ -54,6 +56,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('deflate')
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('handle gzip compression', async () => {
Expand All @@ -63,6 +66,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('gzip')
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('accept additional headers', async () => {
Expand All @@ -77,6 +81,7 @@ describe('Compression With Elysia', () => {

expect(res.headers.get('Content-Encoding')).toBe('deflate')
expect(res.headers.get('x-powered-by')).toBe('Elysia')
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('return correct plain/text', async () => {
Expand All @@ -87,6 +92,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Type')).toBe('text/plain')
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('return correct application/json', async () => {
Expand All @@ -99,6 +105,7 @@ describe('Compression With Elysia', () => {
expect(res.headers.get('Content-Type')).toBe(
'application/json;charset=utf-8',
)
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('return correct image type', async () => {
Expand All @@ -109,6 +116,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Type')).toBe('image/png')
expect(res.headers.get('vary')).toBeNull()
})

it('must be redirected to /not-found', async () => {
Expand Down Expand Up @@ -154,6 +162,7 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('Content-Encoding')).toBe('gzip')
expect(res.headers.get('vary')).toBe('accept-encoding')
})

it('cors should be enable when threshold 1024', async () => {
Expand Down Expand Up @@ -202,5 +211,6 @@ describe('Compression With Elysia', () => {
const res = await app.handle(req())

expect(res.headers.get('access-control-allow-origin')).toBe('*')
expect(res.headers.get('vary')).toBe('*')
})
})

0 comments on commit 9f5cf1a

Please sign in to comment.