@@ -106,4 +106,129 @@ describe('Cookie - Unchanged Values', () => {
106106
107107 expect ( response . headers . getAll ( 'set-cookie' ) . length ) . toBeGreaterThan ( 0 )
108108 } )
109+
110+ it ( 'should not send set-cookie header when setting same object value as incoming cookie' , async ( ) => {
111+ const app = new Elysia ( ) . post ( '/update' , ( { cookie : { data } } ) => {
112+ // Set to same value as incoming cookie
113+ data . value = { id : 123 , name : 'test' }
114+ return 'ok'
115+ } )
116+
117+ // First request: set the cookie
118+ const firstRes = await app . handle (
119+ new Request ( 'http://localhost/update' , { method : 'POST' } )
120+ )
121+ const setCookie = firstRes . headers . get ( 'set-cookie' )
122+ expect ( setCookie ) . toBeTruthy ( )
123+
124+ // Second request: send cookie back and set to same value
125+ const secondRes = await app . handle (
126+ new Request ( 'http://localhost/update' , {
127+ method : 'POST' ,
128+ headers : {
129+ cookie : setCookie ! . split ( ';' ) [ 0 ]
130+ }
131+ } )
132+ )
133+
134+ // Should not send Set-Cookie since value didn't change
135+ expect ( secondRes . headers . getAll ( 'set-cookie' ) . length ) . toBe ( 0 )
136+ } )
137+
138+ it ( 'should not send set-cookie header for large unchanged object values' , async ( ) => {
139+ const large = {
140+ users : Array . from ( { length : 100 } , ( _ , i ) => ( {
141+ id : i ,
142+ name : `User ${ i } `
143+ } ) )
144+ }
145+
146+ const app = new Elysia ( ) . post ( '/update' , ( { cookie : { data } } ) => {
147+ data . value = large
148+ return 'ok'
149+ } )
150+
151+ // First request: set the cookie
152+ const firstRes = await app . handle (
153+ new Request ( 'http://localhost/update' , { method : 'POST' } )
154+ )
155+ const setCookie = firstRes . headers . get ( 'set-cookie' )
156+ expect ( setCookie ) . toBeTruthy ( )
157+
158+ // Second request: send cookie back and set to same value
159+ const secondRes = await app . handle (
160+ new Request ( 'http://localhost/update' , {
161+ method : 'POST' ,
162+ headers : {
163+ cookie : setCookie ! . split ( ';' ) [ 0 ]
164+ }
165+ } )
166+ )
167+
168+ // Should not send Set-Cookie since value didn't change
169+ expect ( secondRes . headers . getAll ( 'set-cookie' ) . length ) . toBe ( 0 )
170+ } )
171+
172+ it ( 'should optimize multiple assignments of same object in single request' , async ( ) => {
173+ const app = new Elysia ( ) . post ( '/multi' , ( { cookie : { data } } ) => {
174+ // Multiple assignments of the same value
175+ data . value = { id : 123 , name : 'test' }
176+ data . value = { id : 123 , name : 'test' }
177+ data . value = { id : 123 , name : 'test' }
178+ return 'ok'
179+ } )
180+
181+ const res = await app . handle (
182+ new Request ( 'http://localhost/multi' , { method : 'POST' } )
183+ )
184+
185+ // Should only produce one Set-Cookie header
186+ expect ( res . headers . getAll ( 'set-cookie' ) . length ) . toBe ( 1 )
187+ } )
188+
189+ it ( 'should invalidate hash cache when using update() method' , async ( ) => {
190+ const app = new Elysia ( ) . post ( '/cache-invalidation' , ( { cookie : { data } } ) => {
191+ // Set initial value
192+ data . value = { id : 1 , name : 'first' }
193+
194+ // Modify via update() - should invalidate cache
195+ data . update ( { value : { id : 2 , name : 'second' } } )
196+
197+ // Set to the updated value again - should detect as unchanged
198+ data . value = { id : 2 , name : 'second' }
199+
200+ return 'ok'
201+ } )
202+
203+ const res = await app . handle (
204+ new Request ( 'http://localhost/cache-invalidation' , { method : 'POST' } )
205+ )
206+
207+ // Should only have one Set-Cookie header (for final value)
208+ const setCookieHeaders = res . headers . getAll ( 'set-cookie' )
209+ expect ( setCookieHeaders . length ) . toBe ( 1 )
210+ expect ( setCookieHeaders [ 0 ] ) . toContain ( 'id' )
211+ } )
212+
213+ it ( 'should invalidate hash cache when using set() method' , async ( ) => {
214+ const app = new Elysia ( ) . post ( '/cache-set' , ( { cookie : { data } } ) => {
215+ // Set initial value
216+ data . value = { id : 1 }
217+
218+ // Modify via set() - should invalidate cache
219+ data . set ( { value : { id : 2 } } )
220+
221+ // Set to the updated value again - should detect as unchanged
222+ data . value = { id : 2 }
223+
224+ return 'ok'
225+ } )
226+
227+ const res = await app . handle (
228+ new Request ( 'http://localhost/cache-set' , { method : 'POST' } )
229+ )
230+
231+ // Should only have one Set-Cookie header
232+ expect ( res . headers . getAll ( 'set-cookie' ) . length ) . toBe ( 1 )
233+ } )
109234} )
0 commit comments