Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CookieContainer.GetAllCookies #53441

Merged
merged 1 commit into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}
}