Skip to content

[MVC.Testing] Fix NullReferenceException #62231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public virtual IServiceProvider Services
StartServer();
if (_useKestrel)
{
return _webHost!.Services;
return _webHost?.Services ?? _host!.Services;
}

return _host?.Services ?? _server!.Host.Services;
Expand Down Expand Up @@ -263,8 +263,8 @@ public void StartServer()
{
var deferredHostBuilder = new DeferredHostBuilder();
deferredHostBuilder.UseEnvironment(Environments.Development);
// There's no helper for UseApplicationName, but we need to
// set the application name to the target entry point
// There's no helper for UseApplicationName, but we need to
// set the application name to the target entry point
// assembly name.
deferredHostBuilder.ConfigureHostConfiguration(config =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Mvc.Testing;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

public class KestrelBasedWebApplicationFactoryForMinimal : WebApplicationFactory<SimpleWebSiteWithWebApplicationBuilder.Program>
{
public KestrelBasedWebApplicationFactoryForMinimal() : base()
{
// Use dynamically assigned port to avoid test conflicts in CI.
this.UseKestrel(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

Expand Down Expand Up @@ -40,9 +42,6 @@ public async Task RetrievesDataFromRealServer()
[Fact]
public async Task ServerReachableViaGenericHttpClient()
{
// Arrange
var baseAddress = new Uri("http://localhost:5000");

// Act
using var factoryClient = Factory.CreateClient();
using var client = new HttpClient() { BaseAddress = factoryClient.BaseAddress };
Expand All @@ -52,4 +51,15 @@ public async Task ServerReachableViaGenericHttpClient()
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public void CanResolveServices()
{
// Act
var server = Factory.Services.GetRequiredService<IServer>();

// Assert
Assert.NotNull(server);
Assert.Contains("Kestrel", server.GetType().FullName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

public class RealServerUsingMinimalBackedIntegrationTests : IClassFixture<KestrelBasedWebApplicationFactoryForMinimal>
{
public KestrelBasedWebApplicationFactoryForMinimal Factory { get; }

public RealServerUsingMinimalBackedIntegrationTests(KestrelBasedWebApplicationFactoryForMinimal factory)
{
Factory = factory;
}

[Fact]
public async Task RetrievesDataFromRealServer()
{
// Arrange
var expectedMediaType = MediaTypeHeaderValue.Parse("text/plain; charset=utf-8");

// Act
var client = Factory.CreateClient();
var response = await client.GetAsync("/");
var responseContent = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);

Assert.Contains("Hello World", responseContent);
}

[Fact]
public async Task ServerReachableViaGenericHttpClient()
{
// Act
using var factoryClient = Factory.CreateClient();
using var client = new HttpClient() { BaseAddress = factoryClient.BaseAddress };

using var response = await client.GetAsync("/");

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public void CanResolveServices()
{
// Act
var server = Factory.Services.GetRequiredService<IServer>();

// Assert
Assert.NotNull(server);
Assert.Contains("Kestrel", server.GetType().FullName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc.Testing;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
Expand Down
Loading