Skip to content

Commit 71df2cc

Browse files
[MVC.Testing] Fix NullReferenceException (#62231)
1 parent e232d58 commit 71df2cc

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public virtual IServiceProvider Services
103103
StartServer();
104104
if (_useKestrel)
105105
{
106-
return _webHost!.Services;
106+
return _webHost?.Services ?? _host!.Services;
107107
}
108108

109109
return _host?.Services ?? _server!.Host.Services;
@@ -263,8 +263,8 @@ public void StartServer()
263263
{
264264
var deferredHostBuilder = new DeferredHostBuilder();
265265
deferredHostBuilder.UseEnvironment(Environments.Development);
266-
// There's no helper for UseApplicationName, but we need to
267-
// set the application name to the target entry point
266+
// There's no helper for UseApplicationName, but we need to
267+
// set the application name to the target entry point
268268
// assembly name.
269269
deferredHostBuilder.ConfigureHostConfiguration(config =>
270270
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 KestrelBasedWebApplicationFactoryForMinimal : WebApplicationFactory<SimpleWebSiteWithWebApplicationBuilder.Program>
9+
{
10+
public KestrelBasedWebApplicationFactoryForMinimal() : base()
11+
{
12+
// Use dynamically assigned port to avoid test conflicts in CI.
13+
this.UseKestrel(0);
14+
}
15+
}

src/Mvc/test/Mvc.FunctionalTests/RealServerBackedIntegrationTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Net;
55
using System.Net.Http;
66
using System.Net.Http.Headers;
7+
using Microsoft.AspNetCore.Hosting.Server;
8+
using Microsoft.Extensions.DependencyInjection;
79

810
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
911

@@ -40,9 +42,6 @@ public async Task RetrievesDataFromRealServer()
4042
[Fact]
4143
public async Task ServerReachableViaGenericHttpClient()
4244
{
43-
// Arrange
44-
var baseAddress = new Uri("http://localhost:5000");
45-
4645
// Act
4746
using var factoryClient = Factory.CreateClient();
4847
using var client = new HttpClient() { BaseAddress = factoryClient.BaseAddress };
@@ -52,4 +51,15 @@ public async Task ServerReachableViaGenericHttpClient()
5251
// Assert
5352
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
5453
}
54+
55+
[Fact]
56+
public void CanResolveServices()
57+
{
58+
// Act
59+
var server = Factory.Services.GetRequiredService<IServer>();
60+
61+
// Assert
62+
Assert.NotNull(server);
63+
Assert.Contains("Kestrel", server.GetType().FullName);
64+
}
5565
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;
6+
using System.Net.Http.Headers;
7+
using Microsoft.AspNetCore.Hosting.Server;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
11+
12+
public class RealServerUsingMinimalBackedIntegrationTests : IClassFixture<KestrelBasedWebApplicationFactoryForMinimal>
13+
{
14+
public KestrelBasedWebApplicationFactoryForMinimal Factory { get; }
15+
16+
public RealServerUsingMinimalBackedIntegrationTests(KestrelBasedWebApplicationFactoryForMinimal factory)
17+
{
18+
Factory = factory;
19+
}
20+
21+
[Fact]
22+
public async Task RetrievesDataFromRealServer()
23+
{
24+
// Arrange
25+
var expectedMediaType = MediaTypeHeaderValue.Parse("text/plain; charset=utf-8");
26+
27+
// Act
28+
var client = Factory.CreateClient();
29+
var response = await client.GetAsync("/");
30+
var responseContent = await response.Content.ReadAsStringAsync();
31+
32+
// Assert
33+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
34+
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
35+
36+
Assert.Contains("Hello World", responseContent);
37+
}
38+
39+
[Fact]
40+
public async Task ServerReachableViaGenericHttpClient()
41+
{
42+
// Act
43+
using var factoryClient = Factory.CreateClient();
44+
using var client = new HttpClient() { BaseAddress = factoryClient.BaseAddress };
45+
46+
using var response = await client.GetAsync("/");
47+
48+
// Assert
49+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
50+
}
51+
52+
[Fact]
53+
public void CanResolveServices()
54+
{
55+
// Act
56+
var server = Factory.Services.GetRequiredService<IServer>();
57+
58+
// Assert
59+
Assert.NotNull(server);
60+
Assert.Contains("Kestrel", server.GetType().FullName);
61+
}
62+
}

src/Mvc/test/Mvc.FunctionalTests/RealServerWithDynamicPortIntegrationTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Net;
55
using System.Net.Http;
6-
using System.Net.Http.Headers;
76
using Microsoft.AspNetCore.Mvc.Testing;
87

98
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

0 commit comments

Comments
 (0)