@@ -323,7 +323,7 @@ describe("cookie path option", () => {
323
323
"/x/y" : {
324
324
GET ( r ) {
325
325
r . cookies . set ( "user" , "a" , { maxAge : 3600 , path : "/" } ) ;
326
- const cookie = r . cookies . toSetCookieHeaders ( ) . at ( 0 ) ;
326
+ const cookie = r . cookies . toSetCookieHeaders ( ) . at ( 0 ) ! ;
327
327
return new Response ( "ok" , {
328
328
headers : { "set-cookie" : cookie } ,
329
329
} ) ;
@@ -371,3 +371,45 @@ test("delete cookie invalid path option", () => {
371
371
`"Invalid cookie name: contains invalid characters"` ,
372
372
) ;
373
373
} ) ;
374
+
375
+ describe ( "Bun.CookieMap constructor" , ( ) => {
376
+ test ( "throws for invalid array" , ( ) => {
377
+ expect ( ( ) => new Bun . CookieMap ( [ [ "abc defg =fhaingj809读写汉字学中文" ] ] ) ) . toThrowErrorMatchingInlineSnapshot (
378
+ `"Expected arrays of exactly two strings"` ,
379
+ ) ;
380
+ } ) ;
381
+ test ( "accepts unicode cookie value in object" , ( ) => {
382
+ const map = new Bun . CookieMap ( {
383
+ "cookie key" : "读写汉字学中文" ,
384
+ } ) ;
385
+ expect ( map . get ( "cookie key" ) ) . toBe ( "读写汉字学中文" ) ;
386
+ } ) ;
387
+ test ( "accepts unicode cookie value in array" , ( ) => {
388
+ const map = new Bun . CookieMap ( [ [ "cookie key" , "读写汉字学中文" ] ] ) ;
389
+ expect ( map . get ( "cookie key" ) ) . toBe ( "读写汉字学中文" ) ;
390
+ } ) ;
391
+ test ( "accepts unicode cookie value in string" , ( ) => {
392
+ const map = new Bun . CookieMap ( "cookie key=读写汉字学中文" ) ;
393
+ expect ( map . get ( "cookie key" ) ) . toBe ( "读写汉字学中文" ) ;
394
+ } ) ;
395
+ test ( "serializes unicode cookie value" , ( ) => {
396
+ const map = new Bun . CookieMap ( ) ;
397
+ map . set ( "cookiekey" , "读写汉字学中文" ) ;
398
+ expect ( map . toSetCookieHeaders ( ) ) . toMatchInlineSnapshot ( `
399
+ [
400
+ "cookiekey=%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87; Path=/; SameSite=Lax",
401
+ ]
402
+ ` ) ;
403
+ // re-parse
404
+ const reparsed = new Bun . CookieMap ( map . toSetCookieHeaders ( ) [ 0 ] . split ( ";" ) [ 0 ] ! ) ;
405
+ expect ( reparsed . get ( "cookiekey" ) ) . toBe ( "读写汉字学中文" ) ;
406
+ } ) ;
407
+ test ( "doesn't parse percent encoded value in object or array" , ( ) => {
408
+ const map = new Bun . CookieMap ( {
409
+ "cookiekey" : "%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87" ,
410
+ } ) ;
411
+ const map2 = new Bun . CookieMap ( [ [ "cookiekey" , "%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87" ] ] ) ;
412
+ expect ( map . get ( "cookiekey" ) ) . toBe ( "%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87" ) ;
413
+ expect ( map2 . get ( "cookiekey" ) ) . toBe ( "%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87" ) ;
414
+ } ) ;
415
+ } ) ;
0 commit comments