feat(etag): export `RETAINED_304_HEADERS` (#2763)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(etag): export `RETAINED_304_HEADERS` * denoify
authored by Yusuke Wada · committed by GitHub · parent 9deb12f · Browse files · 3 files·+60 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+60 added−24 removed
- tests:+58 added−22 removed
- other:+2 added−2 removed
Provenance
- Commit
af7cd01- Tree
9e2bc4f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
9deb12f
3 files changed+60 added−24 removed
modified:deno_dist/middleware/etag/index.ts+1 added-1 removed
10 unmodified lines1112131415151617181944 unmodified lines10 unmodified lines * > The response must not contain a body and must include the headers that * > would have been sent in an equivalent 200 OK response: Cache-Control, * > Content-Location, Date, ETag, Expires, and Vary. */const RETAINED_304_HEADERS = [export const RETAINED_304_HEADERS = [ 'cache-control', 'content-location', 'date', 'etag',44 unmodified linesmodified:src/middleware/etag/index.test.ts+58 added-22 removed
1 import { Hono } from '../../hono'2-import { etag } from '.'2+import { etag, RETAINED_304_HEADERS } from '.'3 4 describe('Etag Middleware', () => {5- let app: Hono6-7- beforeEach(() => {8- app = new Hono()5+ it('Should return etag header', async () => {6+ const app = new Hono()7 app.use('/etag/*', etag())8 app.get('/etag/abc', (c) => {9 return c.text('Hono is cool')10 })11 app.get('/etag/def', (c) => {12 return c.json({ message: 'Hono is cool' })13 })16- })17-18- it('Should return etag header', async () => {14 let res = await app.request('http://localhost/etag/abc')15 expect(res.headers.get('ETag')).not.toBeFalsy()16 expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')17 2 unmodified lines20 expect(res.headers.get('ETag')).toBe('"4515561204e8269cb4468d5b39288d8f2482dcfe"')21 })22 23 it('Should return etag header - binary', async () => {24+ const app = new Hono()29- app.use('/etag-binary/*', etag())30- app.get('/etag-binary', async (c) => {25+ app.use('/etag/*', etag())26+ app.get('/etag', async (c) => {27 return c.body(new Uint8Array(1))28 })29 34- const res = await app.request('http://localhost/etag-binary')30+ const res = await app.request('http://localhost/etag')31 expect(res.headers.get('ETag')).not.toBeFalsy()32 const etagHeader = res.headers.get('ETag')33 expect(etagHeader).toBe('"5ba93c9db0cff93f52b521d7420e43f6eda2784f"')34 })35 36 it('Should not be the same etag - arrayBuffer', async () => {37+ const app = new Hono()38+ app.use('/etag/*', etag())39 app.get('/etag/ab1', (c) => {40 return c.body(new ArrayBuffer(1))41 })42 app.get('/etag/ab2', (c) => {6 unmodified lines49 expect(res.headers.get('ETag')).not.toBe(hash)50 })51 52 it('Should not be the same etag - Uint8Array', async () => {53+ const app = new Hono()54+ app.use('/etag/*', etag())55 app.get('/etag/ui1', (c) => {56 return c.body(new Uint8Array([1, 2, 3]))57 })58 app.get('/etag/ui2', (c) => {6 unmodified lines65 expect(res.headers.get('ETag')).not.toBe(hash)66 })67 68 it('Should return etag header - weak', async () => {69+ const app = new Hono()69- app.use('/etag-weak/*', etag({ weak: true }))70- app.get('/etag-weak/abc', (c) => {70+ app.use('/etag/*', etag({ weak: true }))71+ app.get('/etag/abc', (c) => {72 return c.text('Hono is cool')73 })74 74- const res = await app.request('http://localhost/etag-weak/abc')75+ const res = await app.request('http://localhost/etag/abc')76 expect(res.headers.get('ETag')).not.toBeFalsy()77 expect(res.headers.get('ETag')).toBe('W/"4e32298b1cb4edc595237405e5b696e105c2399a"')78 })79 80 it('Should handle conditional GETs', async () => {81+ const app = new Hono()82+ app.use('/etag/*', etag())83 app.get('/etag/ghi', (c) =>84 c.text('Hono is great', 200, {85 'cache-control': 'public, max-age=120',86 date: 'Mon, Feb 27 2023 12:08:36 GMT',6 unmodified lines93 // unconditional GET94 let res = await app.request('http://localhost/etag/ghi')95 expect(res.status).toBe(200)96 expect(res.headers.get('ETag')).not.toBeFalsy()94- const etag = res.headers.get('ETag') || ''97+ const etagHeaderValue = res.headers.get('ETag') || ''98 99 // conditional GET with the wrong ETag:100 res = await app.request('http://localhost/etag/ghi', {101 headers: {4 unmodified lines106 107 // conditional GET with matching ETag:108 res = await app.request('http://localhost/etag/ghi', {109 headers: {107- 'If-None-Match': etag,110+ 'If-None-Match': etagHeaderValue,111 },112 })113 expect(res.status).toBe(304)111- expect(res.headers.get('Etag')).toBe(etag)114+ expect(res.headers.get('Etag')).toBe(etagHeaderValue)115 expect(await res.text()).toBe('')116 expect(res.headers.get('cache-control')).toBe('public, max-age=120')117 expect(res.headers.get('date')).toBe('Mon, Feb 27 2023 12:08:36 GMT')118 expect(res.headers.get('expires')).toBe('Mon, Feb 27 2023 12:10:36 GMT')2 unmodified lines121 122 // conditional GET with matching ETag among list:123 res = await app.request('http://localhost/etag/ghi', {124 headers: {122- 'If-None-Match': `"mismatch 1", ${etag}, "mismatch 2"`,125+ 'If-None-Match': `"mismatch 1", ${etagHeaderValue}, "mismatch 2"`,126 },127 })128 expect(res.status).toBe(304)129 })130 131 it('Should not return duplicate etag header values', async () => {132+ const app = new Hono()129- app.use('/etag2/*', etag())130- app.use('/etag2/*', etag())131- app.get('/etag2/abc', (c) => c.text('Hono is cool'))133+ app.use('/etag/*', etag())134+ app.use('/etag/*', etag())135+ app.get('/etag/abc', (c) => c.text('Hono is cool'))136 133- const res = await app.request('http://localhost/etag2/abc')137+ const res = await app.request('http://localhost/etag/abc')138 expect(res.headers.get('ETag')).not.toBeFalsy()139 expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')140 })141 142 it('Should not override ETag headers from upstream', async () => {143+ const app = new Hono()144+ app.use('/etag/*', etag())145 app.get('/etag/predefined', (c) =>146 c.text('This response has an ETag', 200, { ETag: '"f-0194-d"' })147 )148 149 const res = await app.request('http://localhost/etag/predefined')150 expect(res.headers.get('ETag')).toBe('"f-0194-d"')151 })152+153+ it('Should retain the default and the specified headers', async () => {154+ const cacheControl = 'public, max-age=120'155+ const message = 'Hello!'156+ const app = new Hono()157+ app.use(158+ '/etag/*',159+ etag({160+ retainedHeaders: ['x-message-retain', ...RETAINED_304_HEADERS],161+ })162+ )163+ app.get('/etag', (c) => {164+ return c.text('Hono is cool', 200, {165+ 'cache-control': cacheControl,166+ 'x-message-retain': message,167+ 'x-message': message,168+ })169+ })170+ const res = await app.request('/etag', {171+ headers: {172+ 'If-None-Match': '"4e32298b1cb4edc595237405e5b696e105c2399a"',173+ },174+ })175+ expect(res.status).toBe(304)176+ expect(res.headers.get('ETag')).not.toBeFalsy()177+ expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')178+ expect(res.headers.get('Cache-Control')).toBe(cacheControl)179+ expect(res.headers.get('x-message-retain')).toBe(message)180+ expect(res.headers.get('x-message')).toBeFalsy()181+ })182 })modified:src/middleware/etag/index.ts+1 added-1 removed
10 unmodified lines1112131415151617181944 unmodified lines10 unmodified lines * > The response must not contain a body and must include the headers that * > would have been sent in an equivalent 200 OK response: Cache-Control, * > Content-Location, Date, ETag, Expires, and Vary. */const RETAINED_304_HEADERS = [export const RETAINED_304_HEADERS = [ 'cache-control', 'content-location', 'date', 'etag',44 unmodified lines