-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Apply hosting environment via command line args #34794
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
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); | ||
|
@@ -35,9 +42,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); | ||
|
@@ -50,9 +58,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); | ||
|
@@ -65,9 +74,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); | ||
|
@@ -79,8 +89,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); | ||
|
@@ -89,23 +102,75 @@ 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] | ||
public async Task ActionReturningProblemDetails_ConfiguresContentType() | ||
public async Task DefaultEnvironment_Is_Development() | ||
{ | ||
// Arrange | ||
var expected = "Development"; | ||
using var client = new WebApplicationFactory<SimpleWebSiteWithWebApplicationBuilder.FakeStartup>().CreateClient(); | ||
|
||
// Act | ||
var content = await client.GetStringAsync("http://localhost/environment"); | ||
|
||
// Assert | ||
Assert.Equal(expected, content); | ||
} | ||
|
||
[Fact] | ||
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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You kept it 😄 |
||
}; | ||
|
||
builder.AddInMemoryCollection(config); | ||
}); | ||
}); | ||
|
||
var expected = "Bonjour tout le monde"; | ||
using var client = fixture.CreateDefaultClient(); | ||
|
||
// Act | ||
var response = await Client.GetAsync("/problem"); | ||
var content = await client.GetStringAsync("http://localhost/greeting"); | ||
|
||
// Assert | ||
await response.AssertStatusCodeAsync(HttpStatusCode.InternalServerError); | ||
Assert.Equal("application/problem+json", response.Content.Headers.ContentType.ToString()); | ||
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do any fancy escaping for
value
? Like can values have spaces which would break config parsing? I'm not super familiar with this areaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how it breaks given how the parsing works. We're not parsing the command line string, we're parsing each arg.
I also don't see a way to escape
-
or/
so that might be the only problem.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An
=
in the key would also mess this up, right? I'm not sure how realistic that is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could always base64 encode/decode. Stripping and adding back the
=
padding characters might be a pain though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we wait until somebody hits this then we fix it by supporting escape sequences in the command line configuration provider, the problem is general.