Implemented HopFrame.Web tests

This commit is contained in:
2024-12-10 16:30:46 +01:00
parent a4d1d3227b
commit 4d91ce1819
7 changed files with 503 additions and 8 deletions

View File

@@ -0,0 +1,29 @@
using System.Web;
using Microsoft.AspNetCore.Http;
namespace HopFrame.Api.Tests.Extensions;
internal static class HttpContextExtensions {
/// <summary>Extracts the partial cookie value from the header section.</summary>
/// <param name="headers"><inheritdoc cref="IHeaderDictionary" path="/summary"/></param>
/// <param name="key">The key for identifying the cookie.</param>
/// <returns>The value of the cookie.</returns>
public static string FindCookie(this IHeaderDictionary headers, string key)
{
string headerKey = $"{key}=";
var cookies = headers.Values
.SelectMany(h => h)
.Where(header => header.StartsWith(headerKey))
.Select(header => header.Substring(headerKey.Length).Split(';').First())
.ToArray();
//Note: cookie values in a header are encoded like a uri parameter value.
var value = cookies.LastOrDefault();//and the last set value, is the relevant one.
if (string.IsNullOrEmpty(value))
return null;
//That's why we should decode that last value, before we return it.
var decoded = HttpUtility.UrlDecode(value);
return decoded;
}
}