Skip to content

Commit 951eea1

Browse files
authored
Upgrade xUnit to v3 (#295)
1 parent 40d95e0 commit 951eea1

19 files changed

+118
-85
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"rollForward": false
1111
},
1212
"nbgv": {
13-
"version": "3.7.112",
13+
"version": "3.7.115",
1414
"commands": [
1515
"nbgv"
1616
],

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
77
## [0.12] - unplanned
88
### Removed
99
- .NET 6.0 target, since it is no longer supported
10+
- .NET Framework 4.6.2, 4.7.0 and 4.7.2, since these can't be tested using xUnit v3
1011
### Added
1112
- Support for .NET 9.0
1213

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ The options include:
8181
TestableHttpClient is build as a netstandard2.0 library, so theoretically it can work on every .NET version that support netstandard2.0.
8282
The following versions are being actively tested and thus supported:
8383

84-
- .NET Framework 4.6, 4.7 and 4.8
85-
- .NET 6.0
84+
- .NET Framework 4.7.2 and 4.8
8685
- .NET 8.0
8786
- .NET 9.0
8887

test/Directory.Build.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project>
22
<ItemGroup>
33
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
4-
<PackageReference Include="xunit" Version="2.9.3" />
5-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
4+
<PackageReference Include="xunit.v3" Version="1.0.1" />
5+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
66
<PrivateAssets>all</PrivateAssets>
77
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
88
</PackageReference>

test/TestableHttpClient.IntegrationTests/AssertingRequests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public async Task WhenAssertingCallsAreNotMade_AndCallsWereMade_AssertionExcepti
1818
using TestableHttpMessageHandler testHandler = new();
1919
using HttpClient client = new(testHandler);
2020

21-
_ = await client.GetAsync("https://httpbin.org/get");
21+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
2222

2323
testHandler.ShouldHaveMadeRequests();
2424
Assert.Throws<HttpRequestMessageAssertionException>(() => testHandler.ShouldHaveMadeRequests(0));
@@ -30,7 +30,7 @@ public async Task AssertingCallsAreNotMadeToSpecificUri()
3030
using TestableHttpMessageHandler testHandler = new();
3131
using HttpClient client = new(testHandler);
3232

33-
_ = await client.GetAsync("https://httpbin.org/get");
33+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
3434

3535
testHandler.ShouldHaveMadeRequestsTo("https://example.org", 0);
3636
Assert.Throws<HttpRequestMessageAssertionException>(() => testHandler.ShouldHaveMadeRequestsTo("https://httpbin.org/get", 0));
@@ -42,7 +42,7 @@ public async Task AssertingCallsAreMadeToSpecificUriPattern()
4242
using TestableHttpMessageHandler testHandler = new();
4343
using HttpClient client = new(testHandler);
4444

45-
_ = await client.GetAsync("https://httpbin.org/get");
45+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
4646

4747
testHandler.ShouldHaveMadeRequestsTo("https://*");
4848
testHandler.ShouldHaveMadeRequestsTo("https://*.org/get");
@@ -61,7 +61,7 @@ public async Task AssertingCallsUsingUriPattern()
6161
using TestableHttpMessageHandler testHandler = new();
6262
using HttpClient client = new(testHandler);
6363

64-
_ = await client.GetAsync("https://httpbin.org/get");
64+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
6565

6666
testHandler.ShouldHaveMadeRequests().WithRequestUri("https://*");
6767
testHandler.ShouldHaveMadeRequests().WithRequestUri("https://*.org/get");
@@ -80,7 +80,7 @@ public async Task ChainUriPatternAssertions()
8080
using TestableHttpMessageHandler testHandler = new();
8181
using HttpClient client = new(testHandler);
8282

83-
_ = await client.GetAsync("https://httpbin.org/get");
83+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
8484

8585
testHandler.ShouldHaveMadeRequestsTo("https://*")
8686
.WithRequestUri("*://httpbin.org/*")
@@ -93,7 +93,7 @@ public async Task AssertingCallWithQueryParameters()
9393
using TestableHttpMessageHandler testHandler = new();
9494
using HttpClient client = new(testHandler);
9595

96-
_ = await client.GetAsync("https://httpbin.org/[email protected]");
96+
_ = await client.GetAsync("https://httpbin.org/[email protected]", TestContext.Current.CancellationToken);
9797

9898
testHandler.ShouldHaveMadeRequests().WithRequestUri("[email protected]");
9999
testHandler.ShouldHaveMadeRequests().WithRequestUri("?email=*");
@@ -106,9 +106,9 @@ public async Task AssertingHttpMethods()
106106
using TestableHttpMessageHandler testHandler = new();
107107
using HttpClient client = new(testHandler);
108108

109-
_ = await client.GetAsync("https://httpbin.org/get");
109+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
110110
using StringContent content = new("");
111-
_ = await client.PostAsync("https://httpbin.org/post", content);
111+
_ = await client.PostAsync("https://httpbin.org/post", content, TestContext.Current.CancellationToken);
112112

113113
testHandler.ShouldHaveMadeRequestsTo("*/get").WithHttpMethod(HttpMethod.Get);
114114
Assert.Throws<HttpRequestMessageAssertionException>(() => testHandler.ShouldHaveMadeRequestsTo("*/get").WithHttpMethod(HttpMethod.Post));
@@ -122,7 +122,7 @@ public async Task AssertingRequestHeaders()
122122
using TestableHttpMessageHandler testHandler = new();
123123
using HttpClient client = new(testHandler);
124124
client.DefaultRequestHeaders.Add("api-version", "1.0");
125-
_ = await client.GetAsync("https://httpbin.org/get");
125+
_ = await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
126126

127127
testHandler.ShouldHaveMadeRequests().WithRequestHeader("api-version");
128128
testHandler.ShouldHaveMadeRequests().WithRequestHeader("api-version", "1.0");
@@ -140,7 +140,7 @@ public async Task AssertingContentHeaders()
140140
using HttpClient client = new(testHandler);
141141

142142
using StringContent content = new("", Encoding.UTF8, "application/json");
143-
_ = await client.PostAsync("https://httpbin.org/post", content);
143+
_ = await client.PostAsync("https://httpbin.org/post", content, TestContext.Current.CancellationToken);
144144

145145
testHandler.ShouldHaveMadeRequests().WithContentHeader("content-type");
146146
testHandler.ShouldHaveMadeRequests().WithContentHeader("Content-Type");
@@ -160,7 +160,7 @@ public async Task AssertingContent()
160160
using HttpClient client = new(testHandler);
161161

162162
using StringContent content = new("my special content");
163-
_ = await client.PostAsync("https://httpbin.org/post", content);
163+
_ = await client.PostAsync("https://httpbin.org/post", content, TestContext.Current.CancellationToken);
164164

165165
#if NETFRAMEWORK
166166
// On .NET Framework the HttpClient disposes the content automatically. So we can't perform the same test.
@@ -182,7 +182,7 @@ public async Task AssertJsonContent()
182182
using HttpClient client = new(testHandler);
183183

184184
using StringContent content = new("{}", Encoding.UTF8, "application/json");
185-
_ = await client.PostAsync("https://httpbin.org/post", content);
185+
_ = await client.PostAsync("https://httpbin.org/post", content, TestContext.Current.CancellationToken);
186186

187187
#if NETFRAMEWORK
188188
// On .NET Framework the HttpClient disposes the content automatically. So we can't perform the same test.
@@ -199,7 +199,7 @@ public async Task CustomAssertions()
199199
using HttpClient client = new(testHandler);
200200

201201
using StringContent content = new("", Encoding.UTF8, "application/json");
202-
_ = await client.PostAsync("https://httpbin.org/post", content);
202+
_ = await client.PostAsync("https://httpbin.org/post", content, TestContext.Current.CancellationToken);
203203

204204
testHandler.ShouldHaveMadeRequests().WithFilter(x => x.Content is not null && x.Content.Headers.ContentType?.MediaType == "application/json", "");
205205
}

test/TestableHttpClient.IntegrationTests/ConfigureResponses.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public async Task UsingTestHandler_WithoutSettingUpResponse_Returns200OKWithoutC
1010
using TestableHttpMessageHandler testHandler = new();
1111

1212
using HttpClient httpClient = new(testHandler);
13-
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/200");
13+
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/200", TestContext.Current.CancellationToken);
1414

1515
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
16-
Assert.Equal(string.Empty, await result.Content.ReadAsStringAsync());
16+
Assert.Equal(string.Empty, await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
1717
}
1818

1919
[Fact]
@@ -23,10 +23,10 @@ public async Task UsingTestHandlerWithCustomResponse_ReturnsCustomResponse()
2323
testHandler.RespondWith(Text("HttpClient testing is easy"));
2424

2525
using HttpClient httpClient = new(testHandler);
26-
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/200");
26+
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/200", TestContext.Current.CancellationToken);
2727

2828
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
29-
Assert.Equal("HttpClient testing is easy", await result.Content.ReadAsStringAsync());
29+
Assert.Equal("HttpClient testing is easy", await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
3030
}
3131

3232
[Fact]
@@ -37,10 +37,10 @@ public async Task UsingTestHandlerWithMultipleCustomResponse_ReturnsLastCustomRe
3737
testHandler.RespondWith(Json("Not Found", HttpStatusCode.NotFound));
3838

3939
using HttpClient httpClient = new(testHandler);
40-
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/201");
40+
HttpResponseMessage result = await httpClient.GetAsync("http://httpbin.org/status/201", TestContext.Current.CancellationToken);
4141

4242
Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
43-
Assert.Equal("\"Not Found\"", await result.Content.ReadAsStringAsync());
43+
Assert.Equal("\"Not Found\"", await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
4444
}
4545

4646
[Fact]
@@ -60,10 +60,10 @@ public async Task UsingTestHandlerWithCustomResponse_AlwaysReturnsSameCustomResp
6060

6161
foreach (string? url in urls)
6262
{
63-
HttpResponseMessage result = await httpClient.GetAsync(url);
63+
HttpResponseMessage result = await httpClient.GetAsync(url, TestContext.Current.CancellationToken);
6464

6565
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
66-
Assert.Equal("HttpClient testing is easy", await result.Content.ReadAsStringAsync());
66+
Assert.Equal("HttpClient testing is easy", await result.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
6767
}
6868
}
6969

@@ -85,13 +85,13 @@ static IResponse PathBasedResponse(HttpResponseContext context)
8585
testHandler.RespondWith(SelectResponse(PathBasedResponse));
8686

8787
using HttpClient httpClient = new(testHandler);
88-
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin/status/200");
88+
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin/status/200", TestContext.Current.CancellationToken);
8989
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
9090

91-
response = await httpClient.GetAsync("http://httpbin.org/status/400");
91+
response = await httpClient.GetAsync("http://httpbin.org/status/400", TestContext.Current.CancellationToken);
9292
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
9393

94-
response = await httpClient.GetAsync("http://httpbin.org/status/500");
94+
response = await httpClient.GetAsync("http://httpbin.org/status/500", TestContext.Current.CancellationToken);
9595
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
9696
}
9797

@@ -108,16 +108,16 @@ public async Task UsingTestHandlerWithRoute_AllowsForRoutingUseCases()
108108
}));
109109

110110
using HttpClient httpClient = new(testHandler);
111-
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin/status/200");
111+
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin/status/200", TestContext.Current.CancellationToken);
112112
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
113113

114-
response = await httpClient.GetAsync("https://httpbin/status/200");
114+
response = await httpClient.GetAsync("https://httpbin/status/200", TestContext.Current.CancellationToken);
115115
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
116116

117-
response = await httpClient.GetAsync("https://httpbin.org/status/400");
117+
response = await httpClient.GetAsync("https://httpbin.org/status/400", TestContext.Current.CancellationToken);
118118
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
119119

120-
response = await httpClient.GetAsync("https://httpbin.org/status/500");
120+
response = await httpClient.GetAsync("https://httpbin.org/status/500", TestContext.Current.CancellationToken);
121121
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
122122
}
123123

@@ -128,7 +128,7 @@ public async Task SimulateTimeout_WillThrowExceptionSimulatingTheTimeout()
128128
testHandler.RespondWith(Timeout());
129129

130130
using HttpClient httpClient = new(testHandler);
131-
await Assert.ThrowsAsync<TaskCanceledException>(() => httpClient.GetAsync("https://httpbin.org/delay/500"));
131+
await Assert.ThrowsAsync<TaskCanceledException>(() => httpClient.GetAsync("https://httpbin.org/delay/500", TestContext.Current.CancellationToken));
132132
}
133133

134134
[Fact]
@@ -143,20 +143,20 @@ public async Task UsingTestHandlerWithSequencedResponses_WillReturnDifferentResp
143143
));
144144

145145
using HttpClient httpClient = new(testHandler);
146-
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything");
146+
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
147147
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
148148

149-
response = await httpClient.GetAsync("http://httpbin.org/anything");
149+
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
150150
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
151151

152-
response = await httpClient.GetAsync("http://httpbin.org/anything");
152+
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
153153
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
154154

155-
response = await httpClient.GetAsync("http://httpbin.org/anything");
155+
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
156156
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
157157

158158
// Last configured response is returned when all other responses are used.
159-
response = await httpClient.GetAsync("http://httpbin.org/anything");
159+
response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
160160
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
161161
}
162162

@@ -167,7 +167,7 @@ public async Task UsingTestHandlerWithDelayedResponses_WillDelayTheResponse()
167167
testHandler.RespondWith(Delayed(StatusCode(HttpStatusCode.OK), TimeSpan.FromSeconds(1)));
168168

169169
using HttpClient httpClient = new(testHandler);
170-
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything");
170+
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
171171
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
172172
}
173173

@@ -178,7 +178,7 @@ public async Task UsingTestHandlerWithConfiguredResponses_WillConfigureTheRespon
178178
testHandler.RespondWith(Configured(StatusCode(HttpStatusCode.NoContent), x => x.Headers.Add("server", "test")));
179179

180180
using HttpClient httpClient = new(testHandler);
181-
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything");
181+
HttpResponseMessage response = await httpClient.GetAsync("http://httpbin.org/anything", TestContext.Current.CancellationToken);
182182
Assert.Equal("test", response.Headers.Server.ToString());
183183
}
184184
}

test/TestableHttpClient.IntegrationTests/CreatingClients.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public async Task CreateASimpleHttpClient()
1010
using TestableHttpMessageHandler testableHttpMessageHandler = new();
1111
using HttpClient client = testableHttpMessageHandler.CreateClient();
1212

13-
await client.GetAsync("https://httpbin.org/get");
13+
await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
1414

1515
testableHttpMessageHandler.ShouldHaveMadeRequestsTo("https://httpbin.org/get");
1616
}
@@ -21,7 +21,7 @@ public async Task CreateClientWithConfiguration()
2121
using TestableHttpMessageHandler testableHttpMessageHandler = new();
2222
using HttpClient client = testableHttpMessageHandler.CreateClient(client => client.DefaultRequestHeaders.Add("test", "test"));
2323

24-
await client.GetAsync("https://httpbin.org/get");
24+
await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
2525

2626
testableHttpMessageHandler.ShouldHaveMadeRequests().WithRequestHeader("test", "test");
2727
}
@@ -33,7 +33,7 @@ public async Task CreateClientWithCustomHandlers()
3333
using TestHandler handler = new();
3434
using HttpClient client = testableHttpMessageHandler.CreateClient(handler);
3535

36-
await client.GetAsync("https://httpbin.org/get");
36+
await client.GetAsync("https://httpbin.org/get", TestContext.Current.CancellationToken);
3737

3838
testableHttpMessageHandler.ShouldHaveMadeRequestsTo("https://httpbin.org/get");
3939
Assert.True(handler.WasCalled);

test/TestableHttpClient.IntegrationTests/CustomizeJsonSerialization.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task Asserting_also_works_this_way()
4949
{
5050
using TestableHttpMessageHandler sut = new();
5151
using HttpClient client = sut.CreateClient();
52-
await client.PostAsJsonAsync("http://localhost", new { Name = "Charlie" });
52+
await client.PostAsJsonAsync("http://localhost", new { Name = "Charlie" }, cancellationToken: TestContext.Current.CancellationToken);
5353

5454
#if NETFRAMEWORK
5555
// Well this doesn't really work on .NET Framework.
@@ -70,7 +70,7 @@ public async Task And_we_can_go_crazy_with_it()
7070
WriteIndented = true
7171
};
7272

73-
await client.PostAsJsonAsync("http://localhost", new { Name = "Charlie" }, options);
73+
await client.PostAsJsonAsync("http://localhost", new { Name = "Charlie" }, options, cancellationToken: TestContext.Current.CancellationToken);
7474

7575
#if NETFRAMEWORK
7676
// Well this doesn't really work on .NET Framework.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading;
2+
3+
namespace TestableHttpClient.IntegrationTests;
4+
5+
#if NETFRAMEWORK
6+
7+
internal static class NetFrameworkPollyFill
8+
{
9+
public static Task<string> ReadAsStringAsync(this HttpContent content, CancellationToken cancellationToken = default)
10+
{
11+
return content.ReadAsStringAsync();
12+
}
13+
}
14+
15+
#endif

test/TestableHttpClient.IntegrationTests/TestableHttpClient.IntegrationTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net462;net47;net48;net8.0;net9.0</TargetFrameworks>
4+
<TargetFrameworks>net472;net48;net8.0;net9.0</TargetFrameworks>
5+
<OutputType>Exe</OutputType>
56
</PropertyGroup>
67

78
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">

0 commit comments

Comments
 (0)