From 4aa63ffe0a34c63ee1f6026ca68bcd6f89bb6021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Mon, 7 Aug 2023 20:38:41 +0200 Subject: [PATCH 1/2] Update test project to .NET 6 Also update all the test dependencies to their latest versions and adapt the code. --- HttpRecorder.Tests/ContextTests.cs | 2 +- HttpRecorder.Tests/HttpRecorder.Tests.csproj | 14 ++--- .../HttpRecorderIntegrationTests.cs | 60 ++++++++++--------- HttpRecorder.Tests/Server/Startup.cs | 2 +- HttpRecorder/HttpRecorderDelegatingHandler.cs | 16 ++--- 5 files changed, 44 insertions(+), 50 deletions(-) diff --git a/HttpRecorder.Tests/ContextTests.cs b/HttpRecorder.Tests/ContextTests.cs index b2ebb0b..0dac551 100644 --- a/HttpRecorder.Tests/ContextTests.cs +++ b/HttpRecorder.Tests/ContextTests.cs @@ -91,7 +91,7 @@ public async Task ItShouldWorkWithHttpRecorderContextWhenNotRecording() { var client = services.BuildServiceProvider().GetRequiredService().CreateClient("TheClient"); Func act = async () => await client.GetAsync(ApiController.JsonUri); - act.Should().Throw(); + await act.Should().ThrowAsync(); } } diff --git a/HttpRecorder.Tests/HttpRecorder.Tests.csproj b/HttpRecorder.Tests/HttpRecorder.Tests.csproj index 35b3764..014b3e5 100644 --- a/HttpRecorder.Tests/HttpRecorder.Tests.csproj +++ b/HttpRecorder.Tests/HttpRecorder.Tests.csproj @@ -1,18 +1,16 @@ - netcoreapp2.2 + net6.0 false - - - - - - - + + + + + diff --git a/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs b/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs index 8e87117..3578195 100644 --- a/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs +++ b/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs @@ -1,7 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.IO; +using System.Net; using System.Net.Http; +using System.Net.Http.Json; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -38,12 +40,12 @@ await ExecuteModeIterations(async (client, mode) => { var response = await client.GetAsync(ApiController.JsonUri); - response.EnsureSuccessStatusCode(); + response.Should().BeSuccessful(); response.Headers.Remove("Date"); if (mode == HttpRecorderMode.Passthrough) { passthroughResponse = response; - var result = await response.Content.ReadAsAsync(); + var result = await response.Content.ReadFromJsonAsync(); result.Name.Should().Be(SampleModel.DefaultName); } else @@ -63,12 +65,12 @@ await ExecuteModeIterations(async (client, mode) => { var response = await client.GetAsync($"{ApiController.JsonUri}?name={name}"); - response.EnsureSuccessStatusCode(); + response.Should().BeSuccessful(); response.Headers.Remove("Date"); if (mode == HttpRecorderMode.Passthrough) { passthroughResponse = response; - var result = await response.Content.ReadAsAsync(); + var result = await response.Content.ReadFromJsonAsync(); result.Name.Should().Be(name); } else @@ -88,13 +90,13 @@ await ExecuteModeIterations(async (client, mode) => { var response = await client.PostAsJsonAsync(ApiController.JsonUri, sampleModel); - response.EnsureSuccessStatusCode(); + response.Should().BeSuccessful(); response.Headers.Remove("Date"); if (mode == HttpRecorderMode.Passthrough) { passthroughResponse = response; - var result = await response.Content.ReadAsAsync(); + var result = await response.Content.ReadFromJsonAsync(); result.Name.Should().Be(sampleModel.Name); } else @@ -119,12 +121,12 @@ await ExecuteModeIterations(async (client, mode) => var response = await client.PostAsync(ApiController.FormDataUri, formContent); - response.EnsureSuccessStatusCode(); + response.Should().BeSuccessful(); response.Headers.Remove("Date"); if (mode == HttpRecorderMode.Passthrough) { passthroughResponse = response; - var result = await response.Content.ReadAsAsync(); + var result = await response.Content.ReadFromJsonAsync(); result.Name.Should().Be(sampleModel.Name); } else @@ -161,8 +163,8 @@ await ExecuteModeIterations(async (client, mode) => for (var i = 0; i < Concurrency; i++) { var response = responses[i]; - response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadAsAsync(); + response.Should().BeSuccessful(); + var result = await response.Content.ReadFromJsonAsync(); result.Name.Should().Be($"{i}"); } } @@ -188,10 +190,10 @@ public async Task ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto( nameof(ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto)); var response1 = await client.GetAsync($"{ApiController.JsonUri}?name=1"); var response2 = await client.GetAsync($"{ApiController.JsonUri}?name=2"); - var result1 = await response1.Content.ReadAsAsync(); + var result1 = await response1.Content.ReadFromJsonAsync(); result1.Name.Should().Be("1"); - var result2 = await response2.Content.ReadAsAsync(); + var result2 = await response2.Content.ReadFromJsonAsync(); result2.Name.Should().Be("2"); // We resolve to replay at this point. @@ -215,7 +217,7 @@ await ExecuteModeIterations(async (client, mode) => { var response = await client.GetAsync(ApiController.BinaryUri); - response.EnsureSuccessStatusCode(); + response.Should().BeSuccessful(); response.Headers.Remove("Date"); if (mode == HttpRecorderMode.Passthrough) @@ -239,7 +241,7 @@ public async Task ItShouldThrowIfDoesNotFindFile() Func act = async () => await client.GetAsync(ApiController.JsonUri); - act.Should().Throw() + await act.Should().ThrowAsync() .WithMessage($"*{TestFile}*"); } @@ -251,7 +253,7 @@ public async Task ItShouldThrowIfFileIsCorrupted() Func act = async () => await client.GetAsync(ApiController.JsonUri); - act.Should().Throw() + await act.Should().ThrowAsync() .WithMessage($"*{file}*"); } @@ -268,25 +270,25 @@ public async Task ItShouldThrowIfNoRequestCanBeMatched() Func act = async () => await client.GetAsync(ApiController.JsonUri); - act.Should().Throw() + await act.Should().ThrowAsync() .WithMessage($"*{ApiController.JsonUri}*"); } [Theory] - [InlineData(202)] - [InlineData(301)] - [InlineData(303)] - [InlineData(404)] - [InlineData(500)] - [InlineData(502)] - public async Task ItShouldGetStatus(int statusCode) + [InlineData(HttpStatusCode.Accepted)] + [InlineData(HttpStatusCode.MovedPermanently)] + [InlineData(HttpStatusCode.RedirectMethod)] + [InlineData(HttpStatusCode.NotFound)] + [InlineData(HttpStatusCode.InternalServerError)] + [InlineData(HttpStatusCode.BadGateway)] + public async Task ItShouldGetStatus(HttpStatusCode statusCode) { HttpResponseMessage passthroughResponse = null; await ExecuteModeIterations(async (client, mode) => { - var response = await client.GetAsync($"{ApiController.StatusCodeUri}?statusCode={statusCode}"); - response.StatusCode.Should().Be(statusCode); + var response = await client.GetAsync($"{ApiController.StatusCodeUri}?statusCode={(int)statusCode}"); + response.Should().HaveStatusCode(statusCode); response.Headers.Remove("Date"); if (mode == HttpRecorderMode.Passthrough) @@ -310,7 +312,7 @@ public async Task ItShouldOverrideModeWithEnvironmentVariable() Func act = () => client.GetAsync(ApiController.JsonUri); - act.Should().Throw(); + await act.Should().ThrowAsync(); } finally { @@ -326,8 +328,8 @@ public async Task ItShouldAnonymize() HttpRecorderMode.Record, repository: repositoryMock.Object, anonymizer: RulesInteractionAnonymizer.Default.AnonymizeRequestQueryStringParameter("key")); - Func act = async () => await client.GetAsync($"{ApiController.JsonUri}?key=foo"); - act.Should().Throw(); // Because we don't act on the stream in the repository. That's fine. + + await client.GetAsync($"{ApiController.JsonUri}?key=foo"); repositoryMock.Verify( x => x.StoreAsync( diff --git a/HttpRecorder.Tests/Server/Startup.cs b/HttpRecorder.Tests/Server/Startup.cs index bf74a66..a61e482 100644 --- a/HttpRecorder.Tests/Server/Startup.cs +++ b/HttpRecorder.Tests/Server/Startup.cs @@ -9,7 +9,7 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { - services.AddMvc(); + services.AddMvc(o => o.EnableEndpointRouting = false); } public void Configure(IApplicationBuilder app) diff --git a/HttpRecorder/HttpRecorderDelegatingHandler.cs b/HttpRecorder/HttpRecorderDelegatingHandler.cs index f6bb849..a9ec59d 100644 --- a/HttpRecorder/HttpRecorderDelegatingHandler.cs +++ b/HttpRecorder/HttpRecorderDelegatingHandler.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.IO; using System.Linq; using System.Net.Http; using System.Threading; @@ -190,17 +189,12 @@ private async Task ResolveExecutionMode(CancellationToken cancellationToken) /// The returned as convenience. private async Task PostProcessResponse(HttpResponseMessage response) { - if (response.Content != null) + // Trick to make sure a fake ContentLength is not artificially added by the HttpClient if none was provided by the server. + // Indeed, the ContentLength is _set_ in the _getter_, but explicitly setting a value opts out of this (undocumented) behaviour. + // See https://github.com/dotnet/runtime/blob/ebdb045532190ffc664bba9a0a1e3f2ce35cf23f/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpContentHeaders.cs#L51 + if (!response.Content.Headers.Contains("Content-Length")) { - var stream = await response.Content.ReadAsStreamAsync(); - if (stream.CanSeek) - { - // The HTTP Client is adding the content length header on HttpConnectionResponseContent even when the server does not have a header. - response.Content.Headers.ContentLength = stream.Length; - - // We do reset the stream in case it needs to be re-read. - stream.Seek(0, SeekOrigin.Begin); - } + response.Content.Headers.ContentLength = null; } return response; From 43b56990112bc5b42797ba4826e2a4602440c59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Thu, 25 Aug 2022 19:56:21 +0200 Subject: [PATCH 2/2] Fix warnings --- Directory.Build.props | 2 +- .../RulesInteractionAnonymizerTests.cs | 9 ++++--- HttpRecorder.Tests/ContextTests.cs | 14 +++++------ HttpRecorder.Tests/HttpClientFactoryTests.cs | 4 +--- .../HttpRecorderIntegrationTests.cs | 22 +++++++---------- .../Matchers/RulesMatcherUnitTests.cs | 24 +++++++++---------- HttpRecorder.Tests/ServerFixture.cs | 2 +- HttpRecorder/HttpClientBuilderExtensions.cs | 2 +- HttpRecorder/Matchers/RulesMatcher.cs | 12 +++++----- HttpRecorder/Repositories/HAR/Header.cs | 1 - 10 files changed, 42 insertions(+), 50 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index e138ee7..f01ec3e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -6,7 +6,7 @@ Copyright (c) 2020, nventive Apache-2.0 HttpRecorder - 8.0 + 10.0 true 1701;1702;1998 diff --git a/HttpRecorder.Tests/Anonymizers/RulesInteractionAnonymizerTests.cs b/HttpRecorder.Tests/Anonymizers/RulesInteractionAnonymizerTests.cs index dc7d36c..dd92e90 100644 --- a/HttpRecorder.Tests/Anonymizers/RulesInteractionAnonymizerTests.cs +++ b/HttpRecorder.Tests/Anonymizers/RulesInteractionAnonymizerTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -34,8 +33,8 @@ public async Task ItShouldAnonymizeRequestQueryStringParameter() .AnonymizeRequestQueryStringParameter("key"); var result = await anonymizer.Anonymize(interaction); - result.Messages[0].Response.RequestMessage.RequestUri.ToString().Should().Be("http://first/"); - result.Messages[1].Response.RequestMessage.RequestUri.ToString().Should().Be($"https://second/?key={RulesInteractionAnonymizer.DefaultAnonymizerReplaceValue}&value=bar"); + result.Messages[0].Response.RequestMessage!.RequestUri!.ToString().Should().Be("http://first/"); + result.Messages[1].Response.RequestMessage!.RequestUri!.ToString().Should().Be($"https://second/?key={RulesInteractionAnonymizer.DefaultAnonymizerReplaceValue}&value=bar"); } [Fact] @@ -51,10 +50,10 @@ public async Task ItShouldAnonymizeRequestHeader() .AnonymizeRequestHeader("X-RequestHeader"); var result = await anonymizer.Anonymize(interaction); - result.Messages[0].Response.RequestMessage.Headers.GetValues("X-RequestHeader").First().Should().Be(RulesInteractionAnonymizer.DefaultAnonymizerReplaceValue); + result.Messages[0].Response.RequestMessage!.Headers.GetValues("X-RequestHeader").First().Should().Be(RulesInteractionAnonymizer.DefaultAnonymizerReplaceValue); } - private Interaction BuildInteraction(params HttpRequestMessage[] requests) + private static Interaction BuildInteraction(params HttpRequestMessage[] requests) { return new Interaction( "test", diff --git a/HttpRecorder.Tests/ContextTests.cs b/HttpRecorder.Tests/ContextTests.cs index 0dac551..3721c87 100644 --- a/HttpRecorder.Tests/ContextTests.cs +++ b/HttpRecorder.Tests/ContextTests.cs @@ -32,8 +32,8 @@ public async Task ItShouldWorkWithHttpRecorderContext() options.BaseAddress = _fixture.ServerUri; }); - HttpResponseMessage passthroughResponse = null; - using (var context = new HttpRecorderContext((sp, builder) => new HttpRecorderConfiguration + HttpResponseMessage passthroughResponse; + using (new HttpRecorderContext((_, _) => new HttpRecorderConfiguration { Mode = HttpRecorderMode.Record, InteractionName = nameof(ItShouldWorkWithHttpRecorderContext), @@ -44,7 +44,7 @@ public async Task ItShouldWorkWithHttpRecorderContext() passthroughResponse.EnsureSuccessStatusCode(); } - using (var context = new HttpRecorderContext((sp, builder) => new HttpRecorderConfiguration + using (new HttpRecorderContext((_, _) => new HttpRecorderConfiguration { Mode = HttpRecorderMode.Replay, InteractionName = nameof(ItShouldWorkWithHttpRecorderContext), @@ -70,8 +70,8 @@ public async Task ItShouldWorkWithHttpRecorderContextWhenNotRecording() options.BaseAddress = _fixture.ServerUri; }); - HttpResponseMessage passthroughResponse = null; - using (var context = new HttpRecorderContext((sp, builder) => new HttpRecorderConfiguration + HttpResponseMessage passthroughResponse; + using (new HttpRecorderContext((_, _) => new HttpRecorderConfiguration { Enabled = false, Mode = HttpRecorderMode.Record, @@ -83,7 +83,7 @@ public async Task ItShouldWorkWithHttpRecorderContextWhenNotRecording() passthroughResponse.EnsureSuccessStatusCode(); } - using (var context = new HttpRecorderContext((sp, builder) => new HttpRecorderConfiguration + using (new HttpRecorderContext((_, _) => new HttpRecorderConfiguration { Mode = HttpRecorderMode.Replay, InteractionName = nameof(ItShouldWorkWithHttpRecorderContextWhenNotRecording), @@ -99,7 +99,7 @@ public async Task ItShouldWorkWithHttpRecorderContextWhenNotRecording() public void ItShouldNotAllowMultipleContexts() { using var context = new HttpRecorderContext(); - Action act = () => { var ctx2 = new HttpRecorderContext(); }; + Action act = () => { _ = new HttpRecorderContext(); }; act.Should().Throw().WithMessage("*multiple*"); } } diff --git a/HttpRecorder.Tests/HttpClientFactoryTests.cs b/HttpRecorder.Tests/HttpClientFactoryTests.cs index 7f44cfe..e474d26 100644 --- a/HttpRecorder.Tests/HttpClientFactoryTests.cs +++ b/HttpRecorder.Tests/HttpClientFactoryTests.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; using HttpRecorder.Tests.Server; diff --git a/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs b/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs index 3578195..353f3f1 100644 --- a/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs +++ b/HttpRecorder.Tests/HttpRecorderIntegrationTests.cs @@ -46,7 +46,7 @@ await ExecuteModeIterations(async (client, mode) => { passthroughResponse = response; var result = await response.Content.ReadFromJsonAsync(); - result.Name.Should().Be(SampleModel.DefaultName); + result!.Name.Should().Be(SampleModel.DefaultName); } else { @@ -71,7 +71,7 @@ await ExecuteModeIterations(async (client, mode) => { passthroughResponse = response; var result = await response.Content.ReadFromJsonAsync(); - result.Name.Should().Be(name); + result!.Name.Should().Be(name); } else { @@ -97,7 +97,7 @@ await ExecuteModeIterations(async (client, mode) => { passthroughResponse = response; var result = await response.Content.ReadFromJsonAsync(); - result.Name.Should().Be(sampleModel.Name); + result!.Name.Should().Be(sampleModel.Name); } else { @@ -127,7 +127,7 @@ await ExecuteModeIterations(async (client, mode) => { passthroughResponse = response; var result = await response.Content.ReadFromJsonAsync(); - result.Name.Should().Be(sampleModel.Name); + result!.Name.Should().Be(sampleModel.Name); } else { @@ -165,7 +165,7 @@ await ExecuteModeIterations(async (client, mode) => var response = responses[i]; response.Should().BeSuccessful(); var result = await response.Content.ReadFromJsonAsync(); - result.Name.Should().Be($"{i}"); + result!.Name.Should().Be($"{i}"); } } else @@ -185,21 +185,17 @@ public async Task ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto( File.Delete(recordedFileName); } - var client = CreateHttpClient( - HttpRecorderMode.Auto, - nameof(ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto)); + var client = CreateHttpClient(HttpRecorderMode.Auto); var response1 = await client.GetAsync($"{ApiController.JsonUri}?name=1"); var response2 = await client.GetAsync($"{ApiController.JsonUri}?name=2"); var result1 = await response1.Content.ReadFromJsonAsync(); - result1.Name.Should().Be("1"); + result1!.Name.Should().Be("1"); var result2 = await response2.Content.ReadFromJsonAsync(); - result2.Name.Should().Be("2"); + result2!.Name.Should().Be("2"); // We resolve to replay at this point. - client = CreateHttpClient( - HttpRecorderMode.Auto, - nameof(ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto)); + client = CreateHttpClient(HttpRecorderMode.Auto); var response2_1 = await client.GetAsync($"{ApiController.JsonUri}?name=1"); var response2_2 = await client.GetAsync($"{ApiController.JsonUri}?name=2"); diff --git a/HttpRecorder.Tests/Matchers/RulesMatcherUnitTests.cs b/HttpRecorder.Tests/Matchers/RulesMatcherUnitTests.cs index 55f8326..ec1e442 100644 --- a/HttpRecorder.Tests/Matchers/RulesMatcherUnitTests.cs +++ b/HttpRecorder.Tests/Matchers/RulesMatcherUnitTests.cs @@ -22,12 +22,12 @@ public void ItShouldMatchOnce() var result = matcher.Match(request, interaction); - result.Response.RequestMessage.RequestUri.Should().BeEquivalentTo(new Uri("http://first")); + result.Response.RequestMessage!.RequestUri.Should().BeEquivalentTo(new Uri("http://first")); result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.RequestUri.Should().BeEquivalentTo(new Uri("http://second")); + result.Response.RequestMessage!.RequestUri.Should().BeEquivalentTo(new Uri("http://second")); } [Fact] @@ -45,7 +45,7 @@ public void ItShouldMatchOnceByHttpMethod() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.Method.Should().BeEquivalentTo(HttpMethod.Head); + result.Response.RequestMessage!.Method.Should().BeEquivalentTo(HttpMethod.Head); } [Fact] @@ -63,7 +63,7 @@ public void ItShouldMatchOnceByCompleteRequestUri() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.RequestUri.Should().BeEquivalentTo(new Uri("http://first?name=bar")); + result.Response.RequestMessage!.RequestUri.Should().BeEquivalentTo(new Uri("http://first?name=bar")); } [Fact] @@ -81,7 +81,7 @@ public void ItShouldMatchOnceByPartialRequestUri() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.RequestUri.Should().BeEquivalentTo(new Uri("http://first?name=foo")); + result.Response.RequestMessage!.RequestUri.Should().BeEquivalentTo(new Uri("http://first?name=foo")); } [Fact] @@ -102,7 +102,7 @@ public void ItShouldMatchOnceByHeader() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.Headers.IfNoneMatch.ToString().Should().Be("second"); + result.Response.RequestMessage!.Headers.IfNoneMatch.ToString().Should().Be("second"); } [Fact] @@ -122,7 +122,7 @@ public void ItShouldMatchOnceByContentWithSameSize() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.Content.Should().BeEquivalentTo(secondContent); + result.Response.RequestMessage!.Content.Should().BeEquivalentTo(secondContent); } [Fact] @@ -142,7 +142,7 @@ public void ItShouldMatchOnceByContentWithDifferentSizes() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.Content.Should().BeEquivalentTo(secondContent); + result.Response.RequestMessage!.Content.Should().BeEquivalentTo(secondContent); } [Fact] @@ -165,7 +165,7 @@ public void ItShouldMatchOnceByJsonContent() var result = matcher.Match(request, interaction); result.Should().NotBeNull(); - result.Response.RequestMessage.Content.Should().BeEquivalentTo(secondContent); + result.Response.RequestMessage!.Content.Should().BeEquivalentTo(secondContent); } [Fact] @@ -210,10 +210,10 @@ public void ItShouldMatchWithCombination() .ByRequestUri(); var result = matcher.Match(request, interaction); - result.Response.RequestMessage.RequestUri.Should().BeEquivalentTo(new Uri("http://second")); + result.Response.RequestMessage!.RequestUri.Should().BeEquivalentTo(new Uri("http://second")); } - private Interaction BuildInteraction(params HttpRequestMessage[] requests) + private static Interaction BuildInteraction(params HttpRequestMessage[] requests) { return new Interaction( "test", @@ -224,7 +224,7 @@ private Interaction BuildInteraction(params HttpRequestMessage[] requests) private class Model { - public string Name { get; set; } + public string Name { get; init; } public override bool Equals(object obj) { diff --git a/HttpRecorder.Tests/ServerFixture.cs b/HttpRecorder.Tests/ServerFixture.cs index 57dd65d..6a7e491 100644 --- a/HttpRecorder.Tests/ServerFixture.cs +++ b/HttpRecorder.Tests/ServerFixture.cs @@ -30,7 +30,7 @@ public Uri ServerUri get { var serverAddressesFeature = ServerWebHost.ServerFeatures.Get(); - return new Uri(serverAddressesFeature.Addresses.First()); + return new Uri(serverAddressesFeature!.Addresses.First()); } } diff --git a/HttpRecorder/HttpClientBuilderExtensions.cs b/HttpRecorder/HttpClientBuilderExtensions.cs index 2e6af04..f3ab1f9 100644 --- a/HttpRecorder/HttpClientBuilderExtensions.cs +++ b/HttpRecorder/HttpClientBuilderExtensions.cs @@ -51,7 +51,7 @@ public static IHttpClientBuilder AddHttpRecorder( repository: repository, anonymizer: anonymizer); - return httpClientBuilder.AddHttpMessageHandler((sp) => recorder); + return httpClientBuilder.AddHttpMessageHandler(_ => recorder); } } } diff --git a/HttpRecorder/Matchers/RulesMatcher.cs b/HttpRecorder/Matchers/RulesMatcher.cs index 0dc7e9e..557ff83 100644 --- a/HttpRecorder/Matchers/RulesMatcher.cs +++ b/HttpRecorder/Matchers/RulesMatcher.cs @@ -128,8 +128,8 @@ public RulesMatcher ByHeader(string headerName, StringComparison stringCompariso public RulesMatcher ByContent() => By((request, message) => { - var requestContent = request.Content?.ReadAsByteArrayAsync()?.ConfigureAwait(false).GetAwaiter().GetResult(); - var messageContent = message.Response.RequestMessage.Content?.ReadAsByteArrayAsync()?.ConfigureAwait(false).GetAwaiter().GetResult(); + var requestContent = request.Content?.ReadAsByteArrayAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + var messageContent = message.Response.RequestMessage.Content?.ReadAsByteArrayAsync().ConfigureAwait(false).GetAwaiter().GetResult(); if (requestContent is null) { @@ -149,8 +149,8 @@ public RulesMatcher ByContent() } return StructuralComparisons.StructuralComparer.Compare( - request.Content?.ReadAsByteArrayAsync()?.Result, - message.Response.RequestMessage.Content?.ReadAsByteArrayAsync()?.Result) == 0; + request.Content?.ReadAsByteArrayAsync().Result, + message.Response.RequestMessage.Content?.ReadAsByteArrayAsync().Result) == 0; }); /// @@ -165,10 +165,10 @@ public RulesMatcher ByJsonContent( JsonSerializerOptions jsonSerializerOptions = null) => By((request, message) => { - var requestContent = request.Content?.ReadAsStringAsync()?.Result; + var requestContent = request.Content?.ReadAsStringAsync().Result; var requestJson = !string.IsNullOrEmpty(requestContent) ? JsonSerializer.Deserialize(requestContent, jsonSerializerOptions) : default(T); - var interactionContent = message.Response.RequestMessage.Content?.ReadAsStringAsync()?.Result; + var interactionContent = message.Response.RequestMessage.Content?.ReadAsStringAsync().Result; var interactionJson = !string.IsNullOrEmpty(interactionContent) ? JsonSerializer.Deserialize(interactionContent, jsonSerializerOptions) : default(T); if (equalityComparer == null) diff --git a/HttpRecorder/Repositories/HAR/Header.cs b/HttpRecorder/Repositories/HAR/Header.cs index 8887282..b282eb0 100644 --- a/HttpRecorder/Repositories/HAR/Header.cs +++ b/HttpRecorder/Repositories/HAR/Header.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Net.Http.Headers; namespace HttpRecorder.Repositories.HAR {