11import { Hono } from '../../hono'
22import { RETAINED_304_HEADERS , etag } from '.'
33
4+ const createPatternedBody = ( length : number ) => {
5+ const body = new Uint8Array ( length )
6+ for ( let i = 0 ; i < body . byteLength ; i ++ ) {
7+ body [ i ] = i % 256
8+ }
9+ return body
10+ }
11+
412describe ( 'Etag Middleware' , ( ) => {
513 it ( 'Should return etag header' , async ( ) => {
614 const app = new Hono ( )
@@ -133,11 +141,12 @@ describe('Etag Middleware', () => {
133141 it ( 'Should return the same etag regardless of ReadableStream chunk boundaries' , async ( ) => {
134142 const app = new Hono ( )
135143 app . use ( '/etag/*' , etag ( ) )
144+ const body = createPatternedBody ( 1_000_000 )
136145 app . get ( '/etag/rs1' , ( c ) => {
137146 return c . body (
138147 new ReadableStream ( {
139148 start ( controller ) {
140- controller . enqueue ( new Uint8Array ( 1_000_000 ) )
149+ controller . enqueue ( body )
141150 controller . close ( )
142151 } ,
143152 } )
@@ -147,9 +156,9 @@ describe('Etag Middleware', () => {
147156 return c . body (
148157 new ReadableStream ( {
149158 start ( controller ) {
150- controller . enqueue ( new Uint8Array ( 1 ) )
151- controller . enqueue ( new Uint8Array ( 32_768 ) )
152- controller . enqueue ( new Uint8Array ( 967_231 ) )
159+ controller . enqueue ( body . slice ( 0 , 1 ) )
160+ controller . enqueue ( body . slice ( 1 , 32_769 ) )
161+ controller . enqueue ( body . slice ( 32_769 ) )
153162 controller . close ( )
154163 } ,
155164 } )
@@ -158,7 +167,18 @@ describe('Etag Middleware', () => {
158167
159168 const res1 = await app . request ( 'http://localhost/etag/rs1' )
160169 const res2 = await app . request ( 'http://localhost/etag/rs2' )
161- expect ( res2 . headers . get ( 'ETag' ) ) . toBe ( res1 . headers . get ( 'ETag' ) )
170+ const expected = '"550563e0460b33a3540278a8446a70bb96eb5172"'
171+ expect ( res1 . headers . get ( 'ETag' ) ) . toBe ( expected )
172+ expect ( res2 . headers . get ( 'ETag' ) ) . toBe ( expected )
173+ } )
174+
175+ it ( 'Should preserve the SHA-1 digest for bodies up to 256 KiB' , async ( ) => {
176+ const app = new Hono ( )
177+ app . use ( '/etag/*' , etag ( ) )
178+ app . get ( '/etag' , ( c ) => c . body ( createPatternedBody ( 256 * 1024 ) ) )
179+
180+ const res = await app . request ( 'http://localhost/etag' )
181+ expect ( res . headers . get ( 'ETag' ) ) . toBe ( '"37ef77696fc255bf53b4cdd014b223676f2dc8bb"' )
162182 } )
163183
164184 it ( 'Should not return etag header when the stream is empty' , async ( ) => {
0 commit comments