Skip to content
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
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;CS8632;NU1608;NU1109</NoWarn>
<Version>8.0.0</Version>
<Version>8.0.1</Version>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Http, Verify</PackageTags>
Expand All @@ -14,4 +14,4 @@
<Verify_SponsorshipExemption>OpenSource</Verify_SponsorshipExemption>
<Verify_SponsorshipExemptionUntil>2027-08</Verify_SponsorshipExemptionUntil>
</PropertyGroup>
</Project>
</Project>
60 changes: 0 additions & 60 deletions src/Verify.Http/Extensions.cs

This file was deleted.

58 changes: 58 additions & 0 deletions src/Verify.Http/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,65 @@
using System.Net.Http.Json;
using Microsoft.Net.Http.Headers;
using MediaTypeHeaderValue = System.Net.Http.Headers.MediaTypeHeaderValue;

static class HttpExtensions
{
internal static string ReadAsString(this HttpContent content)
{
if (content is FileContent fileContent)
{
return fileContent.ReadFileAsString();
}

return content.ReadAsStringAsync().GetAwaiter().GetResult();
}

public static Dictionary<string, string> ToDictionary(this HttpHeaders headers) =>
headers
.ToDictionary(_ => _.Key, _ => string.Join('|', _.Value));

public static Dictionary<string, object> Simplify(this HttpHeaders headers) =>
headers
.OrderBy(_ => _.Key.ToLowerInvariant())
.ToDictionary(
_ => _.Key,
object (_) =>
{
var values = _.Value.ToList();
var key = _.Key.ToLowerInvariant();
if (key is "date" or "expires" or "last-modified")
{
if (DateTime.TryParse(values.First(), out var date))
{
return date;
}
}

if (key is "authorization")
{
return "{Scrubbed}";
}

return string.Join(',', values);
});

public static Dictionary<string, object> NotCookies(this HttpHeaders headers) =>
headers
.Simplify()
.Where(_ => _.Key != "Set-Cookie")
.ToDictionary(_ => _.Key, _ => _.Value);

public static Dictionary<string, string?> Cookies(this HttpHeaders headers) =>
headers
.Simplify()
.Where(_ => _.Key == "Set-Cookie")
.Select(_ =>
{
var stringSegment = (string)_.Value;
return SetCookieHeaderValue.Parse(stringSegment);
})
.ToDictionary(_ => _.Name.Value!, _ => _.Value.Value);

public static string StatusText(this HttpResponseMessage instance)
{
var status = instance.StatusCode;
Expand Down
3 changes: 2 additions & 1 deletion src/Verify.Http/HttpRecording/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public HttpResponse(HttpResponseMessage response)
Headers = headers;
}

// ReSharper disable once CanReplaceCastWithVariableType
var content = (HttpContent?)response.Content;
if (content != null)
{
Expand Down Expand Up @@ -40,4 +41,4 @@ public HttpResponse(HttpResponseMessage response)

[JsonIgnore]
public string? ContentString { get; }
}
}
Loading