Skip to content

Commit 2fa614a

Browse files
committed
Added a test utilizing UseKestrel option of WebApplicationFactory.
1 parent d65d22b commit 2fa614a

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public partial class WebApplicationFactory<TEntryPoint> : IDisposable, IAsyncDis
3131
private IHost? _host;
3232
private Action<IWebHostBuilder> _configuration;
3333
private IWebHost? _webHost;
34+
private Uri _webHostAddress;
3435
private readonly List<HttpClient> _clients = new();
3536
private readonly List<WebApplicationFactory<TEntryPoint>> _derivedFactories = new();
3637

@@ -221,6 +222,14 @@ private void EnsureServer()
221222
if (_useKestrel)
222223
{
223224
_webHost = CreateKestrelServer(builder);
225+
226+
var serverAddressFeature = _webHost.ServerFeatures.Get<IServerAddressesFeature>();
227+
if (serverAddressFeature?.Addresses.Count > 0)
228+
{
229+
// Store the web host address as it's going to be used every time a client is created to communicate to the server
230+
_webHostAddress = new Uri(serverAddressFeature.Addresses.Last());
231+
ClientOptions.BaseAddress = _webHostAddress;
232+
}
224233
}
225234
else
226235
{
@@ -492,8 +501,19 @@ protected virtual void ConfigureWebHost(IWebHostBuilder builder)
492501
/// redirects and handles cookies.
493502
/// </summary>
494503
/// <returns>The <see cref="HttpClient"/>.</returns>
495-
public HttpClient CreateClient() =>
496-
CreateClient(ClientOptions);
504+
public HttpClient CreateClient()
505+
{
506+
var client = CreateClient(ClientOptions);
507+
508+
if (_useKestrel)
509+
{
510+
// Have to do this, as the ClientOptions.BaseAddress will be set to poitn to the kestrel server,
511+
// and it may not match the original base address value.
512+
client.BaseAddress = ClientOptions.BaseAddress;
513+
}
514+
515+
return client;
516+
}
497517

498518
/// <summary>
499519
/// Creates an instance of <see cref="HttpClient"/> that automatically follows
@@ -586,12 +606,7 @@ protected virtual void ConfigureClient(HttpClient client)
586606
throw new InvalidOperationException(Resources.ServerNotInitialized);
587607
}
588608

589-
// The server should be started at this point already.
590-
var serverAddressFeature = _webHost.ServerFeatures.Get<IServerAddressesFeature>();
591-
if (serverAddressFeature?.Addresses.Count > 0)
592-
{
593-
client.BaseAddress = new Uri(serverAddressFeature.Addresses.Last());
594-
}
609+
client.BaseAddress = _webHostAddress;
595610
}
596611
else
597612
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Mvc.Testing;
5+
6+
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
7+
8+
public class KestrelBasedWapFactory : WebApplicationFactory<SimpleWebSite.Startup>
9+
{
10+
public KestrelBasedWapFactory() : base()
11+
{
12+
this.UseKestrel();
13+
}
14+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Net;
5+
using System.Net.Http.Headers;
6+
7+
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
8+
9+
public class RealServerBackedIntegrationTests : IClassFixture<KestrelBasedWapFactory>
10+
{
11+
public KestrelBasedWapFactory Factory { get; }
12+
13+
public RealServerBackedIntegrationTests(KestrelBasedWapFactory factory)
14+
{
15+
Factory = factory;
16+
}
17+
18+
[Fact]
19+
public async Task RetrievesDataFromRealServer()
20+
{
21+
// Arrange
22+
var expectedMediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
23+
24+
// Act
25+
var client = Factory.CreateClient();
26+
var response = await client.GetAsync("/");
27+
var responseContent = await response.Content.ReadAsStringAsync();
28+
29+
// Assert
30+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
31+
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
32+
33+
Assert.Equal(5000, client.BaseAddress.Port);
34+
35+
Assert.Contains("first", responseContent);
36+
Assert.Contains("second", responseContent);
37+
Assert.Contains("wall", responseContent);
38+
Assert.Contains("floor", responseContent);
39+
}
40+
}

0 commit comments

Comments
 (0)