Skip to content

Commit d5c606f

Browse files
Add tests for WebApplicationFactory issues
Add unit tests that replicate #33876 and #33889.
1 parent 1b819d0 commit d5c606f

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

src/Mvc/test/Mvc.FunctionalTests/SimpleWithWebApplicationBuilderTests.cs

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Collections.Generic;
45
using System.Net;
56
using System.Net.Http;
67
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.Testing;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.Hosting;
712
using Xunit;
813

914
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
1015
{
1116
public class SimpleWithWebApplicationBuilderTests : IClassFixture<MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.FakeStartup>>
1217
{
18+
private readonly MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.FakeStartup> _fixture;
19+
1320
public SimpleWithWebApplicationBuilderTests(MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.FakeStartup> fixture)
1421
{
15-
Client = fixture.CreateDefaultClient();
22+
_fixture = fixture;
1623
}
1724

18-
public HttpClient Client { get; }
19-
2025
[Fact]
2126
public async Task HelloWorld()
2227
{
2328
// Arrange
2429
var expected = "Hello World";
30+
using var client = _fixture.CreateDefaultClient();
2531

2632
// Act
27-
var content = await Client.GetStringAsync("http://localhost/");
33+
var content = await client.GetStringAsync("http://localhost/");
2834

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

3946
// Act
40-
var response = await Client.GetAsync("/json");
47+
var response = await client.GetAsync("/json");
4148

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

5462
// Act
55-
var response = await Client.GetAsync("/ok-object");
63+
var response = await client.GetAsync("/ok-object");
5664

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

6978
// Act
70-
var response = await Client.GetAsync("/accepted-object");
79+
var response = await client.GetAsync("/accepted-object");
7180

7281
// Assert
7382
await response.AssertStatusCodeAsync(HttpStatusCode.Accepted);
@@ -79,8 +88,11 @@ public async Task AcceptedObjectResult_Works()
7988
[Fact]
8089
public async Task ActionReturningMoreThanOneResult_NotFound()
8190
{
91+
// Arrange
92+
using var client = _fixture.CreateDefaultClient();
93+
8294
// Act
83-
var response = await Client.GetAsync("/many-results?id=-1");
95+
var response = await client.GetAsync("/many-results?id=-1");
8496

8597
// Assert
8698
await response.AssertStatusCodeAsync(HttpStatusCode.NotFound);
@@ -89,12 +101,77 @@ public async Task ActionReturningMoreThanOneResult_NotFound()
89101
[Fact]
90102
public async Task ActionReturningMoreThanOneResult_Found()
91103
{
104+
// Arrange
105+
using var client = _fixture.CreateDefaultClient();
106+
92107
// Act
93-
var response = await Client.GetAsync("/many-results?id=7");
108+
var response = await client.GetAsync("/many-results?id=7");
94109

95110
// Assert
96111
await response.AssertStatusCodeAsync(HttpStatusCode.MovedPermanently);
97112
Assert.Equal("/json", response.Headers.Location.ToString());
98113
}
114+
115+
[Fact]
116+
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/33889")]
117+
public async Task DefaultEnvironment_Is_Development()
118+
{
119+
// Arrange
120+
var expected = "Development";
121+
using var client = _fixture.CreateDefaultClient();
122+
123+
// Act
124+
var content = await client.GetStringAsync("http://localhost/environment");
125+
126+
// Assert
127+
Assert.Equal(expected, content);
128+
}
129+
130+
[Fact]
131+
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/33876")]
132+
public async Task Configuration_Can_Be_Overridden()
133+
{
134+
// Arrange
135+
var fixture = _fixture.WithWebHostBuilder(builder =>
136+
{
137+
builder.ConfigureAppConfiguration(builder =>
138+
{
139+
var config = new[]
140+
{
141+
KeyValuePair.Create("Greeting", "Bonjour tout le monde"),
142+
};
143+
144+
builder.AddInMemoryCollection(config);
145+
});
146+
});
147+
148+
var expected = "Bonjour tout le monde";
149+
using var client = fixture.CreateDefaultClient();
150+
151+
// Act
152+
var content = await client.GetStringAsync("http://localhost/greeting");
153+
154+
// Assert
155+
Assert.Equal(expected, content);
156+
}
157+
158+
[Fact]
159+
public async Task Environment_Can_Be_Overridden()
160+
{
161+
// Arrange
162+
var fixture = _fixture.WithWebHostBuilder(builder =>
163+
{
164+
builder.UseEnvironment(Environments.Staging);
165+
});
166+
167+
var expected = "Staging";
168+
using var client = fixture.CreateDefaultClient();
169+
170+
// Act
171+
var content = await client.GetStringAsync("http://localhost/environment");
172+
173+
// Assert
174+
Assert.Equal(expected, content);
175+
}
99176
}
100177
}

src/Mvc/test/WebSites/SimpleWebSiteWithWebApplicationBuilder/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Hosting;
57
using static Microsoft.AspNetCore.Http.Results;
68

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

27-
app.Run();
29+
app.MapGet("/environment", (IHostEnvironment environment) => environment.EnvironmentName);
30+
31+
app.MapGet("/greeting", (IConfiguration config) => config["Greeting"]);
2832

33+
app.Run();
2934

3035
record Person(string Name, int Age);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Greeting": "Hello World"
3+
}

0 commit comments

Comments
 (0)