@@ -231,12 +231,204 @@ describe("Chunked Cookie Utils", () => {
231231 // It is called 3 times.
232232 // 2 times for the chunks
233233 // 1 time for the non chunked cookie
234- expect ( reqCookies . delete ) . toHaveBeenCalledTimes ( 3 ) ;
234+ expect ( reqCookies . delete ) . toHaveBeenCalledTimes ( 3 ) ;
235235 expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __3` ) ;
236236 expect ( reqCookies . delete ) . toHaveBeenCalledWith ( `${ name } __4` ) ;
237237 expect ( reqCookies . delete ) . toHaveBeenCalledWith ( name ) ;
238238 } ) ;
239239
240+ // New tests for domain and transient options
241+ it ( "should set the domain property for a single cookie" , ( ) => {
242+ const name = "domainCookie" ;
243+ const value = "small value" ;
244+ const options : CookieOptions = {
245+ path : "/" ,
246+ domain : "example.com" ,
247+ httpOnly : true ,
248+ secure : true ,
249+ sameSite : "lax"
250+ } ;
251+
252+ setChunkedCookie ( name , value , options , reqCookies , resCookies ) ;
253+
254+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
255+ expect ( resCookies . set ) . toHaveBeenCalledWith (
256+ name ,
257+ value ,
258+ expect . objectContaining ( { domain : "example.com" } )
259+ ) ;
260+ } ) ;
261+
262+ it ( "should set the domain property for chunked cookies" , ( ) => {
263+ const name = "largeDomainCookie" ;
264+ const largeValue = "a" . repeat ( 8000 ) ;
265+ const options : CookieOptions = {
266+ path : "/" ,
267+ domain : "example.com" ,
268+ httpOnly : true ,
269+ secure : true ,
270+ sameSite : "lax"
271+ } ;
272+
273+ setChunkedCookie ( name , largeValue , options , reqCookies , resCookies ) ;
274+
275+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 3 ) ; // 3 chunks
276+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
277+ 1 ,
278+ `${ name } __0` ,
279+ expect . any ( String ) ,
280+ expect . objectContaining ( { domain : "example.com" } )
281+ ) ;
282+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
283+ 2 ,
284+ `${ name } __1` ,
285+ expect . any ( String ) ,
286+ expect . objectContaining ( { domain : "example.com" } )
287+ ) ;
288+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
289+ 3 ,
290+ `${ name } __2` ,
291+ expect . any ( String ) ,
292+ expect . objectContaining ( { domain : "example.com" } )
293+ ) ;
294+ } ) ;
295+
296+ it ( "should omit maxAge for a single transient cookie" , ( ) => {
297+ const name = "transientCookie" ;
298+ const value = "small value" ;
299+ const options : CookieOptions = {
300+ path : "/" ,
301+ maxAge : 3600 ,
302+ transient : true ,
303+ httpOnly : true ,
304+ secure : true ,
305+ sameSite : "lax"
306+ } ;
307+ const expectedOptions = { ...options } ;
308+ delete expectedOptions . maxAge ; // maxAge should be removed
309+ delete expectedOptions . transient ; // transient flag itself is not part of the cookie options
310+
311+ setChunkedCookie ( name , value , options , reqCookies , resCookies ) ;
312+
313+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
314+ expect ( resCookies . set ) . toHaveBeenCalledWith ( name , value , expectedOptions ) ;
315+ expect ( resCookies . set ) . not . toHaveBeenCalledWith (
316+ name ,
317+ value ,
318+ expect . objectContaining ( { maxAge : 3600 } )
319+ ) ;
320+ } ) ;
321+
322+ it ( "should omit maxAge for chunked transient cookies" , ( ) => {
323+ const name = "largeTransientCookie" ;
324+ const largeValue = "a" . repeat ( 8000 ) ;
325+ const options : CookieOptions = {
326+ path : "/" ,
327+ maxAge : 3600 ,
328+ transient : true ,
329+ httpOnly : true ,
330+ secure : true ,
331+ sameSite : "lax"
332+ } ;
333+ const expectedOptions = { ...options } ;
334+ delete expectedOptions . maxAge ; // maxAge should be removed
335+ delete expectedOptions . transient ; // transient flag itself is not part of the cookie options
336+
337+ setChunkedCookie ( name , largeValue , options , reqCookies , resCookies ) ;
338+
339+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 3 ) ; // 3 chunks
340+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
341+ 1 ,
342+ `${ name } __0` ,
343+ expect . any ( String ) ,
344+ expectedOptions
345+ ) ;
346+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
347+ 2 ,
348+ `${ name } __1` ,
349+ expect . any ( String ) ,
350+ expectedOptions
351+ ) ;
352+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
353+ 3 ,
354+ `${ name } __2` ,
355+ expect . any ( String ) ,
356+ expectedOptions
357+ ) ;
358+ expect ( resCookies . set ) . not . toHaveBeenCalledWith (
359+ expect . any ( String ) ,
360+ expect . any ( String ) ,
361+ expect . objectContaining ( { maxAge : 3600 } )
362+ ) ;
363+ } ) ;
364+
365+ it ( "should include maxAge for a single non-transient cookie" , ( ) => {
366+ const name = "nonTransientCookie" ;
367+ const value = "small value" ;
368+ const options : CookieOptions = {
369+ path : "/" ,
370+ maxAge : 3600 ,
371+ transient : false ,
372+ httpOnly : true ,
373+ secure : true ,
374+ sameSite : "lax"
375+ } ;
376+ const expectedOptions = { ...options } ;
377+ delete expectedOptions . transient ; // transient flag itself is not part of the cookie options
378+
379+ setChunkedCookie ( name , value , options , reqCookies , resCookies ) ;
380+
381+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 1 ) ;
382+ expect ( resCookies . set ) . toHaveBeenCalledWith ( name , value , expectedOptions ) ;
383+ expect ( resCookies . set ) . toHaveBeenCalledWith (
384+ name ,
385+ value ,
386+ expect . objectContaining ( { maxAge : 3600 } )
387+ ) ;
388+ } ) ;
389+
390+ it ( "should include maxAge for chunked non-transient cookies" , ( ) => {
391+ const name = "largeNonTransientCookie" ;
392+ const largeValue = "a" . repeat ( 8000 ) ;
393+ const options : CookieOptions = {
394+ path : "/" ,
395+ maxAge : 3600 ,
396+ transient : false ,
397+ httpOnly : true ,
398+ secure : true ,
399+ sameSite : "lax"
400+ } ;
401+ const expectedOptions = { ...options } ;
402+ delete expectedOptions . transient ; // transient flag itself is not part of the cookie options
403+
404+ setChunkedCookie ( name , largeValue , options , reqCookies , resCookies ) ;
405+
406+ expect ( resCookies . set ) . toHaveBeenCalledTimes ( 3 ) ; // 3 chunks
407+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
408+ 1 ,
409+ `${ name } __0` ,
410+ expect . any ( String ) ,
411+ expectedOptions
412+ ) ;
413+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
414+ 2 ,
415+ `${ name } __1` ,
416+ expect . any ( String ) ,
417+ expectedOptions
418+ ) ;
419+ expect ( resCookies . set ) . toHaveBeenNthCalledWith (
420+ 3 ,
421+ `${ name } __2` ,
422+ expect . any ( String ) ,
423+ expectedOptions
424+ ) ;
425+ expect ( resCookies . set ) . toHaveBeenCalledWith (
426+ expect . any ( String ) ,
427+ expect . any ( String ) ,
428+ expect . objectContaining ( { maxAge : 3600 } )
429+ ) ;
430+ } ) ;
431+
240432 describe ( "getChunkedCookie" , ( ) => {
241433 it ( "should return undefined when cookie does not exist" , ( ) => {
242434 const result = getChunkedCookie ( "nonexistent" , reqCookies ) ;
0 commit comments