public static string ToCookieHeader(CookieCollection cookies) { if (cookies is null || cookies.Count == 0) { return(string.Empty); } #if DotNetCore30Plus return(string.Join("; ", cookies.Select(t => t.Name + "=" + t.Value))); #else return(string.Join("; ", cookies.Cast <Cookie>().Select(t => t.Name + "=" + t.Value))); #endif }
public void GetAllCookies_NonEmpty_AllCookiesReturned() { var container = new CookieContainer(); container.PerDomainCapacity = 100; container.Capacity = 100; Cookie[] cookies = Enumerable.Range(0, 100).Select(i => new Cookie($"name{i}", $"value{i}")).ToArray(); Uri[] uris = new[] { new Uri("https://dot.net"), new Uri("https://source.dot.net"), new Uri("https://microsoft.com") }; for (int i = 0; i < cookies.Length; i++) { container.Add(uris[i % uris.Length], cookies[i]); } CookieCollection actual = container.GetAllCookies(); Assert.Equal(cookies.Length, actual.Count); Assert.Equal( cookies.Select(c => c.Name + "=" + c.Value).ToHashSet(), actual.Select(c => c.Name + "=" + c.Value).ToHashSet()); }