Skip to content

Commit f88f14d

Browse files
committed
Update test project to .NET 6
Also update all the test dependencies to their latest versions and adapt the code.
1 parent fca59d3 commit f88f14d

File tree

5 files changed

+44
-50
lines changed

5 files changed

+44
-50
lines changed

HttpRecorder.Tests/ContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async Task ItShouldWorkWithHttpRecorderContextWhenNotRecording()
9191
{
9292
var client = services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>().CreateClient("TheClient");
9393
Func<Task> act = async () => await client.GetAsync(ApiController.JsonUri);
94-
act.Should().Throw<HttpRecorderException>();
94+
await act.Should().ThrowAsync<HttpRecorderException>();
9595
}
9696
}
9797

HttpRecorder.Tests/HttpRecorder.Tests.csproj

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="FluentAssertions" Version="5.6.0" />
10-
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
11-
<PackageReference Include="Microsoft.AspNetCore.App" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
13-
<PackageReference Include="Moq" Version="4.10.1" />
14-
<PackageReference Include="xunit" Version="2.4.1" />
15-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
9+
<PackageReference Include="FluentAssertions" Version="6.10.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
11+
<PackageReference Include="Moq" Version="4.18.4" />
12+
<PackageReference Include="xunit" Version="2.4.2" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
1614
</ItemGroup>
1715

1816
<ItemGroup>

HttpRecorder.Tests/HttpRecorderIntegrationTests.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Net;
45
using System.Net.Http;
6+
using System.Net.Http.Json;
57
using System.Runtime.CompilerServices;
68
using System.Threading;
79
using System.Threading.Tasks;
@@ -38,12 +40,12 @@ await ExecuteModeIterations(async (client, mode) =>
3840
{
3941
var response = await client.GetAsync(ApiController.JsonUri);
4042

41-
response.EnsureSuccessStatusCode();
43+
response.Should().BeSuccessful();
4244
response.Headers.Remove("Date");
4345
if (mode == HttpRecorderMode.Passthrough)
4446
{
4547
passthroughResponse = response;
46-
var result = await response.Content.ReadAsAsync<SampleModel>();
48+
var result = await response.Content.ReadFromJsonAsync<SampleModel>();
4749
result.Name.Should().Be(SampleModel.DefaultName);
4850
}
4951
else
@@ -63,12 +65,12 @@ await ExecuteModeIterations(async (client, mode) =>
6365
{
6466
var response = await client.GetAsync($"{ApiController.JsonUri}?name={name}");
6567

66-
response.EnsureSuccessStatusCode();
68+
response.Should().BeSuccessful();
6769
response.Headers.Remove("Date");
6870
if (mode == HttpRecorderMode.Passthrough)
6971
{
7072
passthroughResponse = response;
71-
var result = await response.Content.ReadAsAsync<SampleModel>();
73+
var result = await response.Content.ReadFromJsonAsync<SampleModel>();
7274
result.Name.Should().Be(name);
7375
}
7476
else
@@ -88,13 +90,13 @@ await ExecuteModeIterations(async (client, mode) =>
8890
{
8991
var response = await client.PostAsJsonAsync(ApiController.JsonUri, sampleModel);
9092

91-
response.EnsureSuccessStatusCode();
93+
response.Should().BeSuccessful();
9294
response.Headers.Remove("Date");
9395

9496
if (mode == HttpRecorderMode.Passthrough)
9597
{
9698
passthroughResponse = response;
97-
var result = await response.Content.ReadAsAsync<SampleModel>();
99+
var result = await response.Content.ReadFromJsonAsync<SampleModel>();
98100
result.Name.Should().Be(sampleModel.Name);
99101
}
100102
else
@@ -119,12 +121,12 @@ await ExecuteModeIterations(async (client, mode) =>
119121

120122
var response = await client.PostAsync(ApiController.FormDataUri, formContent);
121123

122-
response.EnsureSuccessStatusCode();
124+
response.Should().BeSuccessful();
123125
response.Headers.Remove("Date");
124126
if (mode == HttpRecorderMode.Passthrough)
125127
{
126128
passthroughResponse = response;
127-
var result = await response.Content.ReadAsAsync<SampleModel>();
129+
var result = await response.Content.ReadFromJsonAsync<SampleModel>();
128130
result.Name.Should().Be(sampleModel.Name);
129131
}
130132
else
@@ -161,8 +163,8 @@ await ExecuteModeIterations(async (client, mode) =>
161163
for (var i = 0; i < Concurrency; i++)
162164
{
163165
var response = responses[i];
164-
response.EnsureSuccessStatusCode();
165-
var result = await response.Content.ReadAsAsync<SampleModel>();
166+
response.Should().BeSuccessful();
167+
var result = await response.Content.ReadFromJsonAsync<SampleModel>();
166168
result.Name.Should().Be($"{i}");
167169
}
168170
}
@@ -188,10 +190,10 @@ public async Task ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto(
188190
nameof(ItShouldExecuteMultipleRequestsInSequenceWithRecorderModeAuto));
189191
var response1 = await client.GetAsync($"{ApiController.JsonUri}?name=1");
190192
var response2 = await client.GetAsync($"{ApiController.JsonUri}?name=2");
191-
var result1 = await response1.Content.ReadAsAsync<SampleModel>();
193+
var result1 = await response1.Content.ReadFromJsonAsync<SampleModel>();
192194
result1.Name.Should().Be("1");
193195

194-
var result2 = await response2.Content.ReadAsAsync<SampleModel>();
196+
var result2 = await response2.Content.ReadFromJsonAsync<SampleModel>();
195197
result2.Name.Should().Be("2");
196198

197199
// We resolve to replay at this point.
@@ -215,7 +217,7 @@ await ExecuteModeIterations(async (client, mode) =>
215217
{
216218
var response = await client.GetAsync(ApiController.BinaryUri);
217219

218-
response.EnsureSuccessStatusCode();
220+
response.Should().BeSuccessful();
219221
response.Headers.Remove("Date");
220222

221223
if (mode == HttpRecorderMode.Passthrough)
@@ -239,7 +241,7 @@ public async Task ItShouldThrowIfDoesNotFindFile()
239241

240242
Func<Task> act = async () => await client.GetAsync(ApiController.JsonUri);
241243

242-
act.Should().Throw<HttpRecorderException>()
244+
await act.Should().ThrowAsync<HttpRecorderException>()
243245
.WithMessage($"*{TestFile}*");
244246
}
245247

@@ -251,7 +253,7 @@ public async Task ItShouldThrowIfFileIsCorrupted()
251253

252254
Func<Task> act = async () => await client.GetAsync(ApiController.JsonUri);
253255

254-
act.Should().Throw<HttpRecorderException>()
256+
await act.Should().ThrowAsync<HttpRecorderException>()
255257
.WithMessage($"*{file}*");
256258
}
257259

@@ -268,25 +270,25 @@ public async Task ItShouldThrowIfNoRequestCanBeMatched()
268270

269271
Func<Task> act = async () => await client.GetAsync(ApiController.JsonUri);
270272

271-
act.Should().Throw<HttpRecorderException>()
273+
await act.Should().ThrowAsync<HttpRecorderException>()
272274
.WithMessage($"*{ApiController.JsonUri}*");
273275
}
274276

275277
[Theory]
276-
[InlineData(202)]
277-
[InlineData(301)]
278-
[InlineData(303)]
279-
[InlineData(404)]
280-
[InlineData(500)]
281-
[InlineData(502)]
282-
public async Task ItShouldGetStatus(int statusCode)
278+
[InlineData(HttpStatusCode.Accepted)]
279+
[InlineData(HttpStatusCode.MovedPermanently)]
280+
[InlineData(HttpStatusCode.RedirectMethod)]
281+
[InlineData(HttpStatusCode.NotFound)]
282+
[InlineData(HttpStatusCode.InternalServerError)]
283+
[InlineData(HttpStatusCode.BadGateway)]
284+
public async Task ItShouldGetStatus(HttpStatusCode statusCode)
283285
{
284286
HttpResponseMessage passthroughResponse = null;
285287

286288
await ExecuteModeIterations(async (client, mode) =>
287289
{
288-
var response = await client.GetAsync($"{ApiController.StatusCodeUri}?statusCode={statusCode}");
289-
response.StatusCode.Should().Be(statusCode);
290+
var response = await client.GetAsync($"{ApiController.StatusCodeUri}?statusCode={(int)statusCode}");
291+
response.Should().HaveStatusCode(statusCode);
290292
response.Headers.Remove("Date");
291293

292294
if (mode == HttpRecorderMode.Passthrough)
@@ -310,7 +312,7 @@ public async Task ItShouldOverrideModeWithEnvironmentVariable()
310312

311313
Func<Task> act = () => client.GetAsync(ApiController.JsonUri);
312314

313-
act.Should().Throw<HttpRecorderException>();
315+
await act.Should().ThrowAsync<HttpRecorderException>();
314316
}
315317
finally
316318
{
@@ -326,8 +328,8 @@ public async Task ItShouldAnonymize()
326328
HttpRecorderMode.Record,
327329
repository: repositoryMock.Object,
328330
anonymizer: RulesInteractionAnonymizer.Default.AnonymizeRequestQueryStringParameter("key"));
329-
Func<Task> act = async () => await client.GetAsync($"{ApiController.JsonUri}?key=foo");
330-
act.Should().Throw<InvalidOperationException>(); // Because we don't act on the stream in the repository. That's fine.
331+
332+
await client.GetAsync($"{ApiController.JsonUri}?key=foo");
331333

332334
repositoryMock.Verify(
333335
x => x.StoreAsync(

HttpRecorder.Tests/Server/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Startup
99
{
1010
public void ConfigureServices(IServiceCollection services)
1111
{
12-
services.AddMvc();
12+
services.AddMvc(o => o.EnableEndpointRouting = false);
1313
}
1414

1515
public void Configure(IApplicationBuilder app)

HttpRecorder/HttpRecorderDelegatingHandler.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics;
3-
using System.IO;
43
using System.Linq;
54
using System.Net.Http;
65
using System.Threading;
@@ -190,17 +189,12 @@ private async Task ResolveExecutionMode(CancellationToken cancellationToken)
190189
/// <returns>The <see cref="HttpResponseMessage"/> returned as convenience.</returns>
191190
private async Task<HttpResponseMessage> PostProcessResponse(HttpResponseMessage response)
192191
{
193-
if (response.Content != null)
192+
// Trick to make sure a fake ContentLength is not artificially added by the HttpClient if none was provided by the server.
193+
// Indeed, the ContentLength is _set_ in the _getter_, but explicitly setting a value opts out of this (undocumented) behaviour.
194+
// See https://github.com/dotnet/runtime/blob/ebdb045532190ffc664bba9a0a1e3f2ce35cf23f/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpContentHeaders.cs#L51
195+
if (!response.Content.Headers.Contains("Content-Length"))
194196
{
195-
var stream = await response.Content.ReadAsStreamAsync();
196-
if (stream.CanSeek)
197-
{
198-
// The HTTP Client is adding the content length header on HttpConnectionResponseContent even when the server does not have a header.
199-
response.Content.Headers.ContentLength = stream.Length;
200-
201-
// We do reset the stream in case it needs to be re-read.
202-
stream.Seek(0, SeekOrigin.Begin);
203-
}
197+
response.Content.Headers.ContentLength = null;
204198
}
205199

206200
return response;

0 commit comments

Comments
 (0)