Skip to content

Add tests for WebApplicationFactory<T> issues with minimal hosting #33890

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

Closed
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
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{
public class SimpleWithWebApplicationBuilderTests : IClassFixture<MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.FakeStartup>>
{
private readonly MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.FakeStartup> _fixture;

public SimpleWithWebApplicationBuilderTests(MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.FakeStartup> fixture)
{
Client = fixture.CreateDefaultClient();
_fixture = fixture;
}

public HttpClient Client { get; }

[Fact]
public async Task HelloWorld()
{
// Arrange
var expected = "Hello World";
using var client = _fixture.CreateDefaultClient();

// Act
var content = await Client.GetStringAsync("http://localhost/");
var content = await client.GetStringAsync("http://localhost/");

// Assert
Assert.Equal(expected, content);
Expand All @@ -35,9 +41,10 @@ public async Task JsonResult_Works()
{
// Arrange
var expected = "{\"name\":\"John\",\"age\":42}";
using var client = _fixture.CreateDefaultClient();

// Act
var response = await Client.GetAsync("/json");
var response = await client.GetAsync("/json");

// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
Expand All @@ -50,9 +57,10 @@ public async Task OkObjectResult_Works()
{
// Arrange
var expected = "{\"name\":\"John\",\"age\":42}";
using var client = _fixture.CreateDefaultClient();

// Act
var response = await Client.GetAsync("/ok-object");
var response = await client.GetAsync("/ok-object");

// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
Expand All @@ -65,9 +73,10 @@ public async Task AcceptedObjectResult_Works()
{
// Arrange
var expected = "{\"name\":\"John\",\"age\":42}";
using var client = _fixture.CreateDefaultClient();

// Act
var response = await Client.GetAsync("/accepted-object");
var response = await client.GetAsync("/accepted-object");

// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.Accepted);
Expand All @@ -79,8 +88,11 @@ public async Task AcceptedObjectResult_Works()
[Fact]
public async Task ActionReturningMoreThanOneResult_NotFound()
{
// Arrange
using var client = _fixture.CreateDefaultClient();

// Act
var response = await Client.GetAsync("/many-results?id=-1");
var response = await client.GetAsync("/many-results?id=-1");

// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.NotFound);
Expand All @@ -89,12 +101,77 @@ public async Task ActionReturningMoreThanOneResult_NotFound()
[Fact]
public async Task ActionReturningMoreThanOneResult_Found()
{
// Arrange
using var client = _fixture.CreateDefaultClient();

// Act
var response = await Client.GetAsync("/many-results?id=7");
var response = await client.GetAsync("/many-results?id=7");

// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.MovedPermanently);
Assert.Equal("/json", response.Headers.Location.ToString());
}

[Fact]
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/33889")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if this was the right syntax or not for a test known to be failing due to an active issue so it gets ignored and doesn't break the build.

public async Task DefaultEnvironment_Is_Development()
{
// Arrange
var expected = "Development";
using var client = _fixture.CreateDefaultClient();

// Act
var content = await client.GetStringAsync("http://localhost/environment");

// Assert
Assert.Equal(expected, content);
}

[Fact]
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/33876")]
public async Task Configuration_Can_Be_Overridden()
{
// Arrange
var fixture = _fixture.WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration(builder =>
{
var config = new[]
{
KeyValuePair.Create("Greeting", "Bonjour tout le monde"),
};

builder.AddInMemoryCollection(config);
});
});

var expected = "Bonjour tout le monde";
using var client = fixture.CreateDefaultClient();

// Act
var content = await client.GetStringAsync("http://localhost/greeting");

// Assert
Assert.Equal(expected, content);
}

[Fact]
public async Task Environment_Can_Be_Overridden()
{
// Arrange
var fixture = _fixture.WithWebHostBuilder(builder =>
{
builder.UseEnvironment(Environments.Staging);
});

var expected = "Staging";
using var client = fixture.CreateDefaultClient();

// Act
var content = await client.GetStringAsync("http://localhost/environment");

// Assert
Assert.Equal(expected, content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using static Microsoft.AspNetCore.Http.Results;

var app = WebApplication.Create(args);
Expand All @@ -24,7 +26,10 @@
return RedirectPermanent("/json");
});

app.Run();
app.MapGet("/environment", (IHostEnvironment environment) => environment.EnvironmentName);

app.MapGet("/greeting", (IConfiguration config) => config["Greeting"]);

app.Run();

record Person(string Name, int Age);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Greeting": "Hello World"
}