@@ -107,26 +107,82 @@ describe('Cookie - Unchanged Values', () => {
107107 expect ( response . headers . getAll ( 'set-cookie' ) . length ) . toBeGreaterThan ( 0 )
108108 } )
109109
110- it ( 'should handle repeated identical object values ' , async ( ) => {
111- const app = new Elysia ( ) . post ( '/' , ( { cookie : { data } } ) => {
112- data . value = { id : 123 , name : 'test' }
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
113113 data . value = { id : 123 , name : 'test' }
114114 return 'ok'
115115 } )
116116
117- const res = await app . handle ( new Request ( 'http://localhost/' , { method : 'POST' } ) )
118- expect ( res . headers . getAll ( 'set-cookie' ) . length ) . toBe ( 1 )
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 )
119136 } )
120137
121- it ( 'should handle large object values efficiently' , async ( ) => {
122- const large = { users : Array . from ( { length : 100 } , ( _ , i ) => ( { id : i , name : `User ${ i } ` } ) ) }
123- const app = new Elysia ( ) . post ( '/' , ( { cookie : { data } } ) => {
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 } } ) => {
124147 data . value = large
125- data . value = JSON . parse ( JSON . stringify ( large ) )
126148 return 'ok'
127149 } )
128150
129- const res = await app . handle ( new Request ( 'http://localhost/' , { method : 'POST' } ) )
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
130186 expect ( res . headers . getAll ( 'set-cookie' ) . length ) . toBe ( 1 )
131187 } )
132188} )
0 commit comments