Skip to content
This repository has been archived by the owner on Nov 21, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
John Luo committed Oct 31, 2018
2 parents 3e0ee5a + 27efce1 commit 62d9794
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 137 deletions.
48 changes: 12 additions & 36 deletions test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs
Expand Up @@ -4,31 +4,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Tests
{
public class WebHostFunctionalTests : LoggedTest
{
private readonly string _testSitesPath;

public WebHostFunctionalTests(ITestOutputHelper output) : base(output)
{
_testSitesPath = GetTestSitesPath();
}

[Fact]
public async Task Start_RequestDelegate_Url()
{
Expand Down Expand Up @@ -60,8 +49,6 @@ public async Task CreateDefaultBuilder_InitializeWithDefaults()
{
// Assert server is Kestrel
Assert.Equal("Kestrel", response.Headers.Server.ToString());
// Set from default config
Assert.Equal("http://localhost:5002/", deploymentResult.ApplicationBaseUri);
// The application name will be sent in response when all asserts succeed in the test app.
Assert.Equal(applicationName, responseText);
}
Expand All @@ -87,8 +74,6 @@ public async Task CreateDefaultBuilderOfT_InitializeWithDefaults()
{
// Assert server is Kestrel
Assert.Equal("Kestrel", response.Headers.Server.ToString());
// Set from default config
Assert.Equal("http://localhost:5002/", deploymentResult.ApplicationBaseUri);
// The application name will be sent in response when all asserts succeed in the test app.
Assert.Equal(applicationName, responseText);
}
Expand Down Expand Up @@ -139,7 +124,7 @@ public void LoggingConfigurationSectionPassedToLoggerByDefault()
}
}
");
using (var webHost = WebHost.Start(context => context.Response.WriteAsync("Hello, World!")))
using (var webHost = WebHost.Start("http://127.0.0.1:0", context => context.Response.WriteAsync("Hello, World!")))
{
var factory = (ILoggerFactory)webHost.Services.GetService(typeof(ILoggerFactory));
var logger = factory.CreateLogger("Test");
Expand Down Expand Up @@ -171,13 +156,13 @@ public void LoggingConfigurationSectionPassedToLoggerByDefault()
public async Task RunsInIISExpressInProcess()
{
var applicationName = "CreateDefaultBuilderApp";
var deploymentParameters = new DeploymentParameters(Path.Combine(_testSitesPath, applicationName), ServerType.IISExpress, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
var deploymentParameters = new DeploymentParameters(Path.Combine(GetTestSitesPath(), applicationName), ServerType.IISExpress, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64)
{
TargetFramework = "netcoreapp2.2",
HostingModel = HostingModel.InProcess,
AncmVersion = AncmVersion.AspNetCoreModuleV2
};

SetEnvironmentVariables(deploymentParameters, "Development");

using (var deployer = IISApplicationDeployerFactory.Create(deploymentParameters, LoggerFactory))
Expand Down Expand Up @@ -227,22 +212,18 @@ private async Task ExecuteStartOrStartWithTest(Func<DeploymentResult, Task<HttpR
bool setTestEnvVars = false,
string environment = "Development")
{
using (StartLog(out var loggerFactory, applicationName))
{
var logger = loggerFactory.CreateLogger(nameof(WebHost.Start));
var deploymentParameters = new DeploymentParameters(Path.Combine(_testSitesPath, applicationName), ServerType.Kestrel, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64);
var deploymentParameters = new DeploymentParameters(Path.Combine(GetTestSitesPath(), applicationName), ServerType.Kestrel, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64);

if (setTestEnvVars)
{
SetEnvironmentVariables(deploymentParameters, environment);
}
if (setTestEnvVars)
{
SetEnvironmentVariables(deploymentParameters, environment);
}

using (var deployer = IISApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
using (var deployer = IISApplicationDeployerFactory.Create(deploymentParameters, LoggerFactory))
{
var deploymentResult = await deployer.DeployAsync();

await assertAction(deploymentResult, logger);
}
await assertAction(deploymentResult, Logger);
}
}

Expand Down Expand Up @@ -271,10 +252,5 @@ private static string GetTestSitesPath()

throw new Exception($"Solution root could not be found using {applicationBasePath}");
}

private static int GetWebHostPort(IWebHost webHost)
=> webHost.ServerFeatures.Get<IServerAddressesFeature>().Addresses
.Select(serverAddress => new Uri(serverAddress).Port)
.FirstOrDefault();
}
}
32 changes: 18 additions & 14 deletions test/TestSites/CreateDefaultBuilderApp/Program.cs
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -15,24 +14,29 @@ public class Program
{
static void Main(string[] args)
{
string responseMessage = string.Empty;
string responseMessage = null;

WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
.ConfigureServices((context, services) =>
{
responseMessage = GetResponseMessage(context, services);
})
.Configure(app =>
{
app.Run(context =>
.ConfigureServices((context, services) => responseMessage = responseMessage ?? GetResponseMessage(context))
.ConfigureKestrel(options => options
.Configure(options.ConfigurationLoader.Configuration)
.Endpoint("HTTP", endpointOptions =>
{
return context.Response.WriteAsync(responseMessage);
});
})
if (responseMessage == null
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
{
responseMessage = "Default Kestrel configuration not read.";
}
}))
.Configure(app => app.Run(context =>
{
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
}))
.Build().Run();
}

private static string GetResponseMessage(WebHostBuilderContext context, IServiceCollection services)
private static string GetResponseMessage(WebHostBuilderContext context)
{
// Verify ContentRootPath set
var contentRoot = Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT");
Expand Down Expand Up @@ -71,7 +75,7 @@ private static string GetResponseMessage(WebHostBuilderContext context, IService
// TODO: Verify AddDebug called
// TODO: Verify UseIISIntegration called

return context.HostingEnvironment.ApplicationName;
return null;
}
}
}
3 changes: 2 additions & 1 deletion test/TestSites/CreateDefaultBuilderApp/appsettings.json
Expand Up @@ -3,7 +3,8 @@
"Kestrel": {
"Endpoints": {
"HTTP": {
"Url": "http://localhost:5002"
"Url": "http://127.0.0.1:0",
"KestrelEndPointSettingName": "KestrelEndPointSettingValue"
}
}
}
Expand Down
80 changes: 79 additions & 1 deletion test/TestSites/CreateDefaultBuilderOfTApp/Program.cs
@@ -1,14 +1,92 @@
// 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;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HostFiltering;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace CreateDefaultBuilderOfTApp
{
public class Program
{
static void Main(string[] args) => WebHost.CreateDefaultBuilder<Startup>(new[] { "--cliKey", "cliValue" }) .Build().Run();
static void Main(string[] args)
{
string responseMessage = null;

WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
.ConfigureServices((context, service) => responseMessage = responseMessage ?? GetResponseMessage(context))
.ConfigureKestrel(options => options
.Configure(options.ConfigurationLoader.Configuration)
.Endpoint("HTTP", endpointOptions =>
{
if (responseMessage == null
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
{
responseMessage = "Default Kestrel configuration not read.";
}
}))
.Configure(app => app.Run(context =>
{
// Verify allowed hosts were loaded
var hostFilteringOptions = app.ApplicationServices.GetRequiredService<IOptions<HostFilteringOptions>>();
var hosts = string.Join(',', hostFilteringOptions.Value.AllowedHosts);
if (responseMessage == null && !string.Equals("example.com,127.0.0.1", hosts, StringComparison.Ordinal))
{
responseMessage = "AllowedHosts not loaded into Options.";
}
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
}))
.Build()
.Run();
}

private static string GetResponseMessage(WebHostBuilderContext context)
{
// Verify ContentRootPath set
var contentRoot = Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT");
if (!string.Equals(contentRoot, context.HostingEnvironment.ContentRootPath, StringComparison.Ordinal))
{
return $"ContentRootPath incorrect. Expected: {contentRoot} Actual: {context.HostingEnvironment.ContentRootPath}";
}

// Verify appsettings.json loaded
if (!string.Equals("settingsValue", context.Configuration["settingsKey"], StringComparison.Ordinal))
{
return $"appsettings.json not loaded into Configuration.";
}

// Verify appsettings.environment.json loaded
if (!string.Equals("devSettingsValue", context.Configuration["devSettingsKey"], StringComparison.Ordinal))
{
return $"appsettings.{context.HostingEnvironment.EnvironmentName}.json not loaded into Configuration.";
}

// TODO: Verify UserSecrets loaded

// Verify environment variables loaded
if (!string.Equals("envValue", context.Configuration["envKey"], StringComparison.Ordinal))
{
return $"Environment variables not loaded into Configuration.";
}

// Verify command line arguments loaded
if (!string.Equals("cliValue", context.Configuration["cliKey"], StringComparison.Ordinal))
{
return $"Command line arguments not loaded into Configuration.";
}

// TODO: Verify AddConsole called
// TODO: Verify AddDebug called
// TODO: Verify UseIISIntegration called

return null;
}
}
}
75 changes: 0 additions & 75 deletions test/TestSites/CreateDefaultBuilderOfTApp/Startup.cs

This file was deleted.

5 changes: 3 additions & 2 deletions test/TestSites/CreateDefaultBuilderOfTApp/appsettings.json
@@ -1,10 +1,11 @@
{
"settingsKey": "settingsValue",
"AllowedHosts": "example.com;localhost",
"AllowedHosts": "example.com;127.0.0.1",
"Kestrel": {
"Endpoints": {
"HTTP": {
"Url": "http://localhost:5002"
"Url": "http://127.0.0.1:0",
"KestrelEndPointSettingName": "KestrelEndPointSettingValue"
}
}
}
Expand Down

0 comments on commit 62d9794

Please sign in to comment.