From 997caeb3233f529e67e8062f28db1300bd276748 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 9 Oct 2026 10:02:53 +1100 Subject: [PATCH] Merge Extensions into HttpExtensions --- src/Directory.Build.props | 4 +- src/Verify.Http/Extensions.cs | 60 ------------------- src/Verify.Http/HttpExtensions.cs | 58 ++++++++++++++++++ src/Verify.Http/HttpRecording/HttpResponse.cs | 3 +- 4 files changed, 62 insertions(+), 63 deletions(-) delete mode 100644 src/Verify.Http/Extensions.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index f0c01ab5..f828fd57 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;CS8632;NU1608;NU1109 - 8.0.0 + 8.0.1 preview 1.0.0 Http, Verify @@ -14,4 +14,4 @@ OpenSource 2027-08 - \ No newline at end of file + diff --git a/src/Verify.Http/Extensions.cs b/src/Verify.Http/Extensions.cs deleted file mode 100644 index e9efe864..00000000 --- a/src/Verify.Http/Extensions.cs +++ /dev/null @@ -1,60 +0,0 @@ -using Microsoft.Net.Http.Headers; - -static class Extensions -{ - internal static string ReadAsString(this HttpContent content) - { - if (content is FileContent fileContent) - { - return fileContent.ReadFileAsString(); - } - - return content.ReadAsStringAsync().GetAwaiter().GetResult(); - } - - public static Dictionary ToDictionary(this HttpHeaders headers) => - headers - .ToDictionary(_ => _.Key, _ => string.Join('|', _.Value)); - - public static Dictionary 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 NotCookies(this HttpHeaders headers) => - headers - .Simplify() - .Where(_ => _.Key != "Set-Cookie") - .ToDictionary(_ => _.Key, _ => _.Value); - - public static Dictionary Cookies(this HttpHeaders headers) => - headers - .Simplify() - .Where(_ => _.Key == "Set-Cookie") - .Select(_ => - { - var stringSegment = (string)_.Value; - return SetCookieHeaderValue.Parse(stringSegment); - }) - .ToDictionary(_ => _.Name.Value!, _ => _.Value.Value); -} diff --git a/src/Verify.Http/HttpExtensions.cs b/src/Verify.Http/HttpExtensions.cs index 06e42e41..330b8d59 100644 --- a/src/Verify.Http/HttpExtensions.cs +++ b/src/Verify.Http/HttpExtensions.cs @@ -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 ToDictionary(this HttpHeaders headers) => + headers + .ToDictionary(_ => _.Key, _ => string.Join('|', _.Value)); + + public static Dictionary 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 NotCookies(this HttpHeaders headers) => + headers + .Simplify() + .Where(_ => _.Key != "Set-Cookie") + .ToDictionary(_ => _.Key, _ => _.Value); + + public static Dictionary 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; diff --git a/src/Verify.Http/HttpRecording/HttpResponse.cs b/src/Verify.Http/HttpRecording/HttpResponse.cs index b2be201f..df237984 100644 --- a/src/Verify.Http/HttpRecording/HttpResponse.cs +++ b/src/Verify.Http/HttpRecording/HttpResponse.cs @@ -11,6 +11,7 @@ public HttpResponse(HttpResponseMessage response) Headers = headers; } + // ReSharper disable once CanReplaceCastWithVariableType var content = (HttpContent?)response.Content; if (content != null) { @@ -40,4 +41,4 @@ public HttpResponse(HttpResponseMessage response) [JsonIgnore] public string? ContentString { get; } -} \ No newline at end of file +}