@@ -467,39 +467,6 @@ public void GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDoma
467
467
Assert . Equal ( 1 , cookies . Count ) ;
468
468
}
469
469
470
- [ Fact ]
471
- public void GetCookies_AddCookieVersion1WithExplicitDomain_CookieReturnedForDomainAndOneLevelSubDomain ( )
472
- {
473
- const string SchemePrefix = "http://" ;
474
- const string OriginalDomain = "contoso.com" ;
475
- const string OriginalDomainWithLeadingDot = "." + OriginalDomain ;
476
-
477
- var container = new CookieContainer ( ) ;
478
- var cookie1 = new Cookie ( CookieName1 , CookieValue1 ) { Domain = OriginalDomainWithLeadingDot , Version = 1 } ;
479
- container . Add ( new Uri ( SchemePrefix + OriginalDomain ) , cookie1 ) ;
480
-
481
- var uri = new Uri ( SchemePrefix + OriginalDomain ) ;
482
- CookieCollection cookies = container . GetCookies ( uri ) ;
483
- Assert . Equal ( 1 , cookies . Count ) ;
484
- Assert . Equal ( OriginalDomainWithLeadingDot , cookies [ CookieName1 ] . Domain ) ;
485
-
486
- uri = new Uri ( SchemePrefix + "www." + OriginalDomain ) ;
487
- cookies = container . GetCookies ( uri ) ;
488
- Assert . Equal ( 1 , cookies . Count ) ;
489
-
490
- uri = new Uri ( SchemePrefix + "x.www." + OriginalDomain ) ;
491
- cookies = container . GetCookies ( uri ) ;
492
- Assert . Equal ( 0 , cookies . Count ) ;
493
-
494
- uri = new Uri ( SchemePrefix + "y.x.www." + OriginalDomain ) ;
495
- cookies = container . GetCookies ( uri ) ;
496
- Assert . Equal ( 0 , cookies . Count ) ;
497
-
498
- uri = new Uri ( SchemePrefix + "z.y.x.www." + OriginalDomain ) ;
499
- cookies = container . GetCookies ( uri ) ;
500
- Assert . Equal ( 0 , cookies . Count ) ;
501
- }
502
-
503
470
[ Fact ]
504
471
public void Ctor_Capacity_Success ( )
505
472
{
@@ -590,13 +557,20 @@ public void Add_SameCookieDifferentVairants_OverridesOlderVariant()
590
557
{
591
558
Uri uri = new Uri ( "http://domain.com" ) ;
592
559
560
+ Cookie c0 = new Cookie ( "name1" , "value" , "" , "domain.com" ) ;
593
561
Cookie c1 = new Cookie ( "name1" , "value" , "" , ".domain.com" ) ; // Variant = Plain
594
562
Cookie c2 = new Cookie ( "name1" , "value" , "" , ".domain.com" ) { Port = "\" 80\" " } ; // Variant = RFC2965 (should override)
595
563
Cookie c3 = new Cookie ( "name1" , "value" , "" , ".domain.com" ) { Port = "\" 80, 90\" " } ; // Variant = RFC2965 (should override)
596
564
Cookie c4 = new Cookie ( "name1" , "value" , "" , ".domain.com" ) { Version = 1 } ; // Variant = RFC2109 (should be rejected)
597
565
598
566
CookieContainer cc = new CookieContainer ( ) ;
567
+
568
+ cc . Add ( c0 ) ;
569
+ Assert . Equal ( "domain.com" , cc . GetCookies ( uri ) [ 0 ] . Domain ) ;
570
+
599
571
cc . Add ( c1 ) ;
572
+ Assert . Equal ( 1 , cc . Count ) ;
573
+ Assert . Equal ( ".domain.com" , cc . GetCookies ( uri ) [ 0 ] . Domain ) ;
600
574
601
575
// Adding a newer variant should override an older one
602
576
cc . Add ( c2 ) ;
@@ -614,6 +588,24 @@ public void Add_SameCookieDifferentVairants_OverridesOlderVariant()
614
588
Assert . Equal ( 1 , cc . Count ) ;
615
589
}
616
590
591
+ [ Fact ]
592
+ public static void Add_SetCookies_SameCookieDifferentVairants_OverridesOlderVariant ( )
593
+ {
594
+ Uri uri = new Uri ( "http://domain.com" ) ;
595
+ CookieContainer cc = new ( ) ;
596
+ Cookie a = new ( )
597
+ {
598
+ Domain = "domain.com" ,
599
+ Name = "lol" ,
600
+ Value = "0"
601
+ } ;
602
+ cc . Add ( uri , a ) ;
603
+ cc . SetCookies ( uri , "lol=42" ) ;
604
+
605
+ Assert . Equal ( 1 , cc . Count ) ;
606
+ Assert . Equal ( "42" , cc . GetCookies ( uri ) . Single ( ) . Value ) ;
607
+ }
608
+
617
609
[ Fact ]
618
610
public void Add_Cookie_Invalid ( )
619
611
{
@@ -781,10 +773,10 @@ private void VerifyGetCookies(CookieContainer cc1, Uri uri, Cookie[] expected)
781
773
782
774
for ( int i = 0 ; i < expected . Length ; i ++ )
783
775
{
784
- Cookie c1 = expected [ i ] ;
785
776
Cookie c2 = cc2 [ i ] ;
786
- Assert . Equal ( c1 . Name , c2 . Name ) ; // Primitive check for equality
787
- Assert . Equal ( c1 . Value , c2 . Value ) ;
777
+ Cookie c1 = expected . Single ( c => c . Name == c2 . Name ) ;
778
+
779
+ Assert . Equal ( c1 . Value , c2 . Value ) ; // Primitive check for equality
788
780
}
789
781
}
790
782
@@ -983,5 +975,100 @@ public void GetCookies_PathMatchingFollowsRfc6265(bool useDefaultPath, string[]
983
975
CookieCollection collection = container . GetCookies ( requestUri ) ;
984
976
Assert . Equal ( expectedMatches , collection . Count ) ;
985
977
}
978
+
979
+ [ Fact ]
980
+ public void Add_ImplicitDomainOfIPv6Hostname_Success ( )
981
+ {
982
+ CookieContainer container = new CookieContainer ( ) ;
983
+ Cookie cookie = new Cookie ( "lol" , "haha" ) ;
984
+ Uri uri = new Uri ( "https://[::FFFF:192.168.0.1]/test" ) ;
985
+ container . Add ( uri , cookie ) ;
986
+ Assert . Equal ( uri . Host , container . GetCookies ( uri ) . Single ( ) . Domain ) ;
987
+ }
988
+
989
+ public static TheoryData < string , string > DomainMatching_WhenMatches_Success_Data = new TheoryData < string , string > ( )
990
+ {
991
+ { "https://q" , "q" } ,
992
+ { "https://localhost/" , "localhost" } ,
993
+ { "https://test.com" , "test.com" } ,
994
+ { "https://test.COM" , "tEsT.com" } ,
995
+ { "https://test.com" , ".test.com" } ,
996
+ { "https://yay.test.com" , "yay.test.com" } ,
997
+ { "https://yay.test.com" , ".yay.test.com" } ,
998
+ { "https://yay.test.com" , ".test.com" } ,
999
+ { "https://42.aaaa.bbb.cc.d.test.com" , "d.test.com" } ,
1000
+ { "https://yay.test.com/foo/bar" , "test.com" } ,
1001
+ { "https://127.0.1.1" , "127.0.1.1" } ,
1002
+ { "https://42.42.100.100" , "42.42.100.100" } ,
1003
+ } ;
1004
+
1005
+ [ Theory ]
1006
+ [ MemberData ( nameof ( DomainMatching_WhenMatches_Success_Data ) ) ]
1007
+ public void DomainMatching_WhenMatches_Success ( string uriString , string domain )
1008
+ {
1009
+ CookieContainer container = new CookieContainer ( ) ;
1010
+ Cookie cookie = new Cookie ( "lol" , "haha" )
1011
+ {
1012
+ Domain = domain
1013
+ } ;
1014
+
1015
+ Uri uri = new Uri ( uriString ) ;
1016
+ container . Add ( uri , cookie ) ;
1017
+ Assert . Equal ( 1 , container . Count ) ;
1018
+
1019
+ CookieCollection collection = container . GetCookies ( uri ) ;
1020
+ Assert . Equal ( 1 , collection . Count ) ;
1021
+ }
1022
+
1023
+ [ Fact ]
1024
+ public void DomainMatching_ExplicitDomain_MatchesMultiple ( )
1025
+ {
1026
+ CookieContainer container = new CookieContainer ( ) ;
1027
+ Cookie a = new Cookie ( "a" , "aa" , null , "a.com" ) ;
1028
+ Cookie b = new Cookie ( "b" , "bb" , null , "b.a.com" ) ;
1029
+ Cookie c = new Cookie ( "c" , "cc" , null , "c.b.a.com" ) ;
1030
+
1031
+ container . Add ( new Uri ( "http://a.com" ) , a ) ;
1032
+ container . Add ( new Uri ( "http://b.a.com" ) , b ) ;
1033
+ container . Add ( new Uri ( "http://c.b.a.com" ) , c ) ;
1034
+
1035
+ CookieCollection matches = container . GetCookies ( new Uri ( "http://c.b.a.com" ) ) ;
1036
+ Assert . Equal ( 3 , matches . Count ) ;
1037
+ }
1038
+
1039
+ public static TheoryData < string , string > DomainMatching_WhenDoesNotMatch_ThrowsCookieException_Data = new TheoryData < string , string > ( )
1040
+ {
1041
+ { "https://test.com" , "test.co" } , // Domain is not a suffix
1042
+ { "https://test.com" , "x.test.com" } , // Domain is not a suffix (extra chars at start)
1043
+ { "https://test.com" , "ttest.com" } , // Suffix but not separated by dot
1044
+ { "https://test.com" , "test.com." } , // Trailing dot
1045
+ { "https://test.com" , "..test.com" } , // 2 leading dots
1046
+ { "https://foo.test.com" , "yay.test.com" } , // subdomain mismatch
1047
+ { "https://42.42.100.100" , "41.42.100.100" } , // different IP
1048
+
1049
+ // Single label domain without a full match.
1050
+ { "https://test.com" , ".com" } ,
1051
+ { "https://test.com" , "com" } ,
1052
+
1053
+ // If Host is an IP address, it should be equal to the domain.
1054
+ // See https://issues.chromium.org/issues/40126142 and the last condition in
1055
+ // https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3
1056
+ { "https://1.2.3.4" , ".2.3.4" }
1057
+ } ;
1058
+
1059
+
1060
+ [ Theory ]
1061
+ [ MemberData ( nameof ( DomainMatching_WhenDoesNotMatch_ThrowsCookieException_Data ) ) ]
1062
+ public void DomainMatching_WhenDoesNotMatch_ThrowsCookieException ( string uriString , string domain )
1063
+ {
1064
+ CookieContainer container = new CookieContainer ( ) ;
1065
+ Cookie cookie = new Cookie ( "lol" , "haha" )
1066
+ {
1067
+ Domain = domain
1068
+ } ;
1069
+
1070
+ Uri uri = new Uri ( uriString ) ;
1071
+ Assert . Throws < CookieException > ( ( ) => container . Add ( uri , cookie ) ) ;
1072
+ }
986
1073
}
987
1074
}
0 commit comments