Skip to content

Commit

Permalink
Add CookieContainer.GetAllCookies (#53441)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored May 29, 2021
1 parent 95a4476 commit ff72cc9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void Add(System.Uri uri, System.Net.Cookie cookie) { }
public void Add(System.Uri uri, System.Net.CookieCollection cookies) { }
public string GetCookieHeader(System.Uri uri) { throw null; }
public System.Net.CookieCollection GetCookies(System.Uri uri) { throw null; }
public System.Net.CookieCollection GetAllCookies() { throw null; }
public void SetCookies(System.Uri uri, string cookieHeader) { }
}
public partial class CookieException : System.FormatException, System.Runtime.Serialization.ISerializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,32 @@ public CookieCollection GetCookies(Uri uri)
return InternalGetCookies(uri) ?? new CookieCollection();
}

/// <summary>Gets a <see cref="CookieCollection"/> that contains all of the <see cref="Cookie"/> instances in the container.</summary>
/// <returns>A <see cref="CookieCollection"/> that contains all of the <see cref="Cookie"/> instances in the container.</returns>
public CookieCollection GetAllCookies()
{
var result = new CookieCollection();

lock (m_domainTable.SyncRoot)
{
IDictionaryEnumerator lists = m_domainTable.GetEnumerator();
while (lists.MoveNext())
{
PathList list = (PathList)lists.Value!;
lock (list.SyncRoot)
{
IDictionaryEnumerator collections = list.List.GetEnumerator();
while (collections.MoveNext())
{
result.Add((CookieCollection)collections.Value!);
}
}
}
}

return result;
}

internal CookieCollection? InternalGetCookies(Uri uri)
{
if (m_count == 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Xunit;

namespace System.Net.Primitives.Functional.Tests
Expand Down Expand Up @@ -124,5 +125,37 @@ public void GetCookies_AddCookieVersion1WithExplicitDomain_CookieReturnedForDoma
cookies = container.GetCookies(uri);
Assert.Equal(0, cookies.Count);
}

[Fact]
public void GetAllCookies_Empty_ReturnsEmptyCollection()
{
var container = new CookieContainer();
Assert.NotNull(container.GetAllCookies());
Assert.NotSame(container.GetAllCookies(), container.GetAllCookies());
Assert.Empty(container.GetAllCookies());
}

[Fact]
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());
}
}
}

0 comments on commit ff72cc9

Please sign in to comment.