-
Notifications
You must be signed in to change notification settings - Fork 68
Integrate with IIISEnvironmentFeature #419
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 2 commits
Commits
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
60 changes: 60 additions & 0 deletions
60
src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/Features/IIISEnvironmentFeature.cs
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #if !NET8_0_OR_GREATER | ||
| using System; | ||
|
|
||
| namespace Microsoft.AspNetCore.Server.IIS; | ||
|
|
||
| /// <summary> | ||
| /// This feature provides access to IIS application information. This is <see href="https://github.com/dotnet/aspnetcore/blob/4218bd758012820a955b0185e5b1824168d00c6a/src/Servers/IIS/IIS/src/IIISEnvironmentFeature.cs">available in-box</see> | ||
| /// on .NET 8, but we have an internal copy so we can utilize its features on downlevel versions. | ||
| /// </summary> | ||
| internal interface IIISEnvironmentFeature | ||
| { | ||
| /// <summary> | ||
| /// Gets the version of IIS that is being used. | ||
| /// </summary> | ||
| Version IISVersion { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the AppPool name that is currently running | ||
| /// </summary> | ||
| string AppPoolId { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the path to the AppPool config | ||
| /// </summary> | ||
| string AppPoolConfigFile { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets path to the application configuration that is currently running | ||
| /// </summary> | ||
| string AppConfigPath { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the physical path of the application. | ||
| /// </summary> | ||
| string ApplicationPhysicalPath { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the virtual path of the application. | ||
| /// </summary> | ||
| string ApplicationVirtualPath { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets ID of the current application. | ||
| /// </summary> | ||
| string ApplicationId { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the current site. | ||
| /// </summary> | ||
| string SiteName { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the id of the current site. | ||
| /// </summary> | ||
| uint SiteId { get; } | ||
| } | ||
| #endif |
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
92 changes: 92 additions & 0 deletions
92
.../Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/HttpRuntimeIntegrationTests.cs
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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // 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.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Server.IIS; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Options; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| public class HttpRuntimeIntegrationTests : SelfHostedTestBase | ||
| { | ||
| private const string IIS_VERSION = "IIS_Version"; | ||
| private const string IIS_SITE_ID = "IIS_SITE_ID"; | ||
| private const string IIS_SITE_NAME = "IIS_SITE_NAME"; | ||
| private const string IIS_APPLICATION_VIRTUAL_PATH = "IIS_APPLICATION_VIRTUAL_PATH"; | ||
| private const string IIS_PHYSICAL_PATH = "IIS_PHYSICAL_PATH"; | ||
| private const string IIS_APPLICATION_ID = "IIS_APPLICATION_ID"; | ||
| private const string IIS_APP_CONFIG_PATH = "IIS_APP_CONFIG_PATH"; | ||
| private const string IIS_APP_POOL_CONFIG_FILE = "IIS_APP_POOL_CONFIG_FILE"; | ||
| private const string IIS_APP_POOL_ID = "IIS_APP_POOL_ID"; | ||
|
|
||
| [Fact] | ||
| public async Task ConfigureRuntimeViaConfig() | ||
| { | ||
| // Arrange | ||
| using var host = await GetTestHost() | ||
| .ConfigureAppConfiguration(config => | ||
| { | ||
| config.AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| [IIS_VERSION] = "10.0", | ||
| [IIS_SITE_ID] = "1", | ||
| [IIS_APP_POOL_ID] = IIS_APP_POOL_ID, | ||
| [IIS_APP_POOL_CONFIG_FILE] = IIS_APP_POOL_CONFIG_FILE, | ||
| [IIS_APP_CONFIG_PATH] = IIS_APP_CONFIG_PATH, | ||
| [IIS_PHYSICAL_PATH] = IIS_PHYSICAL_PATH, | ||
| [IIS_APPLICATION_VIRTUAL_PATH] = IIS_APPLICATION_VIRTUAL_PATH, | ||
| [IIS_APPLICATION_ID] = IIS_APPLICATION_ID, | ||
| [IIS_SITE_NAME] = IIS_SITE_NAME, | ||
| }); | ||
| }) | ||
| .StartAsync(); | ||
|
|
||
| // Act | ||
| var options = host.Services.GetRequiredService<IOptions<SystemWebAdaptersOptions>>().Value; | ||
|
|
||
| // Assert | ||
| Assert.Equal(IIS_SITE_NAME, options.SiteName); | ||
| Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.AppDomainAppVirtualPath); | ||
| Assert.Equal(IIS_PHYSICAL_PATH, options.AppDomainAppPath); | ||
| Assert.Equal(IIS_APPLICATION_ID, options.ApplicationID); | ||
| Assert.True(options.IsHosted); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ConfigureRuntimeViaFeature() | ||
| { | ||
| // Arrange | ||
| var feature = new Mock<IIISEnvironmentFeature>(); | ||
|
|
||
| feature.Setup(f => f.SiteName).Returns(IIS_SITE_NAME); | ||
| feature.Setup(f => f.ApplicationVirtualPath).Returns(IIS_APPLICATION_VIRTUAL_PATH); | ||
| feature.Setup(f => f.ApplicationPhysicalPath).Returns(IIS_PHYSICAL_PATH); | ||
| feature.Setup(f => f.ApplicationId).Returns(IIS_APPLICATION_ID); | ||
| feature.Setup(f => f.AppConfigPath).Returns(IIS_APP_CONFIG_PATH); | ||
| feature.Setup(f => f.AppPoolConfigFile).Returns(IIS_APP_POOL_CONFIG_FILE); | ||
| feature.Setup(f => f.AppPoolId).Returns(IIS_APP_POOL_ID); | ||
|
|
||
| using var host = await GetTestHost() | ||
| .StartAsync(); | ||
|
|
||
| host.GetTestServer().Features.Set<IIISEnvironmentFeature>(feature.Object); | ||
|
|
||
| // Act | ||
| var options = host.Services.GetRequiredService<IOptions<SystemWebAdaptersOptions>>().Value; | ||
|
|
||
| // Assert | ||
| Assert.Equal(IIS_SITE_NAME, options.SiteName); | ||
| Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.AppDomainAppVirtualPath); | ||
| Assert.Equal(IIS_PHYSICAL_PATH, options.AppDomainAppPath); | ||
| Assert.Equal(IIS_APPLICATION_ID, options.ApplicationID); | ||
| Assert.True(options.IsHosted); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/SelfHostedTestBase.cs
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // 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.Hosting; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| [Collection(nameof(SelfHostedTests))] | ||
| public class SelfHostedTestBase | ||
| { | ||
| /// <summary> | ||
| /// This method starts up a host in the background that | ||
| /// makes it possible to initialize <see cref="HttpRuntime"/> | ||
| /// and <see cref="HostingEnvironment"/> with values needed | ||
| /// for testing with the <paramref name="configure"/> option. | ||
| /// </summary> | ||
| /// <param name="configure"> | ||
| /// Configuration for the hosting and runtime options. | ||
| /// </param> | ||
| protected static IHostBuilder GetTestHost() | ||
| => new HostBuilder() | ||
| .ConfigureWebHost(webBuilder => | ||
| { | ||
| webBuilder | ||
| .UseTestServer() | ||
| .ConfigureServices(services => | ||
| { | ||
| services.AddSystemWebAdapters(); | ||
| }) | ||
| .Configure(app => | ||
| { | ||
| // No need to configure pipeline for tests | ||
| }); | ||
| }); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.