Skip to content

Commit ccea5ba

Browse files
Refactor acceptance tests to each have their own HttpClient instance
1 parent 6610886 commit ccea5ba

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

src/Common/test/Common.Acceptance.Tests/AcceptanceTest.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,36 @@ namespace Asp.Versioning;
1313
#else
1414
[Trait( "ASP.NET", "Core" )]
1515
#endif
16-
public abstract partial class AcceptanceTest
16+
public abstract partial class AcceptanceTest : IDisposable
1717
{
1818
private static readonly HttpMethod Patch = new( "PATCH" );
19-
private readonly HttpServerFixture fixture;
19+
private readonly Lazy<HttpClient> client;
20+
private bool disposed;
2021

21-
protected AcceptanceTest( HttpServerFixture fixture ) => this.fixture = fixture;
22+
protected AcceptanceTest( HttpServerFixture fixture ) => client = new( fixture.CreateClient );
2223

23-
protected HttpClient Client => fixture.Client;
24+
protected HttpClient Client => client.Value;
25+
26+
public void Dispose()
27+
{
28+
Dispose( true );
29+
GC.SuppressFinalize( this );
30+
}
31+
32+
protected virtual void Dispose( bool disposing )
33+
{
34+
if ( disposed )
35+
{
36+
return;
37+
}
38+
39+
disposed = true;
40+
41+
if ( disposing && client.IsValueCreated )
42+
{
43+
client.Value.Dispose();
44+
}
45+
}
2446

2547
private HttpRequestMessage CreateRequest<TEntity>( string requestUri, TEntity entity, HttpMethod method )
2648
{

src/Common/test/Common.Acceptance.Tests/HttpServerFixture.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ namespace Asp.Versioning;
1111
public abstract partial class HttpServerFixture : IDisposable
1212
{
1313
private readonly Lazy<TestServer> server;
14-
private readonly Lazy<HttpClient> client;
1514
private bool disposed;
1615

17-
protected HttpServerFixture()
18-
{
19-
server = new( CreateServer );
20-
client = new( CreateClient );
21-
}
16+
protected HttpServerFixture() => server = new( CreateServer );
2217

2318
public TestServer Server => server.Value;
2419

25-
public HttpClient Client => client.Value;
26-
2720
public ICollection<Type> FilteredControllerTypes { get; } = new FilteredControllerTypes();
2821

22+
public HttpClient CreateClient()
23+
{
24+
#if NETFRAMEWORK
25+
var newClient = Server.HttpClient;
26+
#else
27+
var newClient = Server.CreateClient();
28+
#endif
29+
newClient.BaseAddress = new Uri( "http://localhost" );
30+
return newClient;
31+
}
32+
2933
public void Dispose()
3034
{
3135
Dispose( true );
@@ -46,27 +50,11 @@ protected virtual void Dispose( bool disposing )
4650
return;
4751
}
4852

49-
if ( client.IsValueCreated )
50-
{
51-
client.Value.Dispose();
52-
}
53-
5453
if ( server.IsValueCreated )
5554
{
5655
server.Value.Dispose();
5756
}
5857
}
5958

6059
protected virtual void OnAddApiVersioning( ApiVersioningOptions options ) { }
61-
62-
private HttpClient CreateClient()
63-
{
64-
#if NETFRAMEWORK
65-
var newClient = Server.HttpClient;
66-
#else
67-
var newClient = Server.CreateClient();
68-
#endif
69-
newClient.BaseAddress = new Uri( "http://localhost" );
70-
return newClient;
71-
}
7260
}

0 commit comments

Comments
 (0)