[Blazor] Move all test projects to Generic host (#13891)
* Moves all test assets to use generic host. * Cleans up unnecessary code.
This commit is contained in:
parent
04f37e59d5
commit
db1aca12a5
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Microsoft.AspNetCore.Blazor.Server" />
|
<Reference Include="Microsoft.AspNetCore.Blazor.Server" />
|
||||||
<Reference Include="Microsoft.AspNetCore" />
|
<Reference Include="Microsoft.AspNetCore" />
|
||||||
|
<Reference Include="Microsoft.Extensions.Hosting" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using Microsoft.AspNetCore;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace HostedInAspNet.Server
|
namespace HostedInAspNet.Server
|
||||||
{
|
{
|
||||||
|
|
@ -14,12 +13,12 @@ namespace HostedInAspNet.Server
|
||||||
BuildWebHost(args).Run();
|
BuildWebHost(args).Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IWebHost BuildWebHost(string[] args) =>
|
public static IHost BuildWebHost(string[] args) =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.UseConfiguration(new ConfigurationBuilder()
|
.ConfigureWebHostDefaults(webHostBuilder =>
|
||||||
.AddCommandLine(args)
|
{
|
||||||
.Build())
|
webHostBuilder.UseStartup<Startup>();
|
||||||
.UseStartup<Startup>()
|
})
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Microsoft.AspNetCore" />
|
<Reference Include="Microsoft.AspNetCore" />
|
||||||
<Reference Include="Microsoft.AspNetCore.Blazor.Server" />
|
<Reference Include="Microsoft.AspNetCore.Blazor.Server" />
|
||||||
|
<Reference Include="Microsoft.Extensions.Hosting" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using Microsoft.AspNetCore;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace MonoSanity
|
namespace MonoSanity
|
||||||
{
|
{
|
||||||
|
|
@ -14,12 +13,12 @@ namespace MonoSanity
|
||||||
BuildWebHost(args).Run();
|
BuildWebHost(args).Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IWebHost BuildWebHost(string[] args) =>
|
public static IHost BuildWebHost(string[] args) =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.UseConfiguration(new ConfigurationBuilder()
|
.ConfigureWebHostDefaults(webHostBuilder =>
|
||||||
.AddCommandLine(args)
|
{
|
||||||
.Build())
|
webHostBuilder.UseStartup<Startup>();
|
||||||
.UseStartup<Startup>()
|
})
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -7,12 +7,13 @@ using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
public class AspNetSiteServerFixture : WebHostServerFixture
|
public class AspNetSiteServerFixture : WebHostServerFixture
|
||||||
{
|
{
|
||||||
public delegate IWebHost BuildWebHost(string[] args);
|
public delegate IHost BuildWebHost(string[] args);
|
||||||
|
|
||||||
public Assembly ApplicationAssembly { get; set; }
|
public Assembly ApplicationAssembly { get; set; }
|
||||||
|
|
||||||
|
|
@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
|
|
||||||
public List<string> AdditionalArguments { get; set; } = new List<string> { "--test-execution-mode", "server" };
|
public List<string> AdditionalArguments { get; set; } = new List<string> { "--test-execution-mode", "server" };
|
||||||
|
|
||||||
protected override IWebHost CreateWebHost()
|
protected override IHost CreateWebHost()
|
||||||
{
|
{
|
||||||
if (BuildWebHostMethod == null)
|
if (BuildWebHostMethod == null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
public string PathBase { get; set; }
|
public string PathBase { get; set; }
|
||||||
public string ContentRoot { get; private set; }
|
public string ContentRoot { get; private set; }
|
||||||
|
|
||||||
protected override IWebHost CreateWebHost()
|
protected override IHost CreateWebHost()
|
||||||
{
|
{
|
||||||
ContentRoot = FindSampleOrTestSitePath(
|
ContentRoot = FindSampleOrTestSitePath(
|
||||||
typeof(TProgram).Assembly.FullName);
|
typeof(TProgram).Assembly.FullName);
|
||||||
|
|
@ -38,29 +38,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
args.Add(Environment);
|
args.Add(Environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new FakeWebHost(DevHostServerProgram.BuildWebHost(args.ToArray()));
|
return DevHostServerProgram.BuildWebHost(args.ToArray());
|
||||||
}
|
|
||||||
|
|
||||||
private class FakeWebHost : IWebHost
|
|
||||||
{
|
|
||||||
private readonly IHost _realHost;
|
|
||||||
|
|
||||||
public FakeWebHost(IHost realHost)
|
|
||||||
{
|
|
||||||
_realHost = realHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IFeatureCollection ServerFeatures => ((IServer)_realHost.Services.GetService(typeof(IServer))).Features;
|
|
||||||
|
|
||||||
public IServiceProvider Services => _realHost.Services;
|
|
||||||
|
|
||||||
public void Dispose() => _realHost.Dispose();
|
|
||||||
|
|
||||||
public void Start() => _realHost.Start();
|
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken = default) => _realHost.StartAsync();
|
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken = default) => _realHost.StopAsync();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
|
|
@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
public string SampleSiteName { get; set; }
|
public string SampleSiteName { get; set; }
|
||||||
|
|
||||||
protected override IWebHost CreateWebHost()
|
protected override IHost CreateWebHost()
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(SampleSiteName))
|
if (string.IsNullOrEmpty(SampleSiteName))
|
||||||
{
|
{
|
||||||
|
|
@ -25,12 +26,13 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
|
|
||||||
var sampleSitePath = FindSampleOrTestSitePath(SampleSiteName);
|
var sampleSitePath = FindSampleOrTestSitePath(SampleSiteName);
|
||||||
|
|
||||||
return new WebHostBuilder()
|
return new HostBuilder()
|
||||||
.UseKestrel()
|
.ConfigureWebHost(webHostBuilder => webHostBuilder
|
||||||
.UseContentRoot(sampleSitePath)
|
.UseKestrel()
|
||||||
.UseWebRoot(string.Empty)
|
.UseContentRoot(sampleSitePath)
|
||||||
.UseStartup<StaticSiteStartup>()
|
.UseWebRoot(string.Empty)
|
||||||
.UseUrls("http://127.0.0.1:0")
|
.UseStartup<StaticSiteStartup>()
|
||||||
|
.UseUrls("http://127.0.0.1:0"))
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
|
|
@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
public string PathBase { get; set; }
|
public string PathBase { get; set; }
|
||||||
|
|
||||||
public IWebHost Host { get; set; }
|
public IHost Host { get; set; }
|
||||||
|
|
||||||
public ExecutionMode ExecutionMode { get; set; } = ExecutionMode.Client;
|
public ExecutionMode ExecutionMode { get; set; } = ExecutionMode.Client;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,10 @@
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.AspNetCore.Hosting.Server;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
|
|
@ -13,12 +16,12 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
{
|
{
|
||||||
Host = CreateWebHost();
|
Host = CreateWebHost();
|
||||||
RunInBackgroundThread(Host.Start);
|
RunInBackgroundThread(Host.Start);
|
||||||
return Host.ServerFeatures
|
return Host.Services.GetRequiredService<IServer>().Features
|
||||||
.Get<IServerAddressesFeature>()
|
.Get<IServerAddressesFeature>()
|
||||||
.Addresses.Single();
|
.Addresses.Single();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IWebHost Host { get; set; }
|
public IHost Host { get; set; }
|
||||||
|
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
|
|
@ -28,6 +31,6 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
|
||||||
Host?.StopAsync();
|
Host?.StopAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract IWebHost CreateWebHost();
|
protected abstract IHost CreateWebHost();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ namespace TestServer
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var createIndividualHosts = new Dictionary<string, (IWebHost host, string basePath)>
|
var createIndividualHosts = new Dictionary<string, (IHost host, string basePath)>
|
||||||
{
|
{
|
||||||
["Client authentication"] = (BuildWebHost<AuthenticationStartup>(CreateAdditionalArgs(args)), "/subdir"),
|
["Client authentication"] = (BuildWebHost<AuthenticationStartup>(CreateAdditionalArgs(args)), "/subdir"),
|
||||||
["Server authentication"] = (BuildWebHost<ServerAuthenticationStartup>(CreateAdditionalArgs(args)), "/subdir"),
|
["Server authentication"] = (BuildWebHost<ServerAuthenticationStartup>(CreateAdditionalArgs(args)), "/subdir"),
|
||||||
|
|
@ -41,13 +41,13 @@ namespace TestServer
|
||||||
var testAppInfo = mainHost.Services.GetRequiredService<TestAppInfo>();
|
var testAppInfo = mainHost.Services.GetRequiredService<TestAppInfo>();
|
||||||
testAppInfo.Scenarios = createIndividualHosts
|
testAppInfo.Scenarios = createIndividualHosts
|
||||||
.ToDictionary(kvp => kvp.Key,
|
.ToDictionary(kvp => kvp.Key,
|
||||||
kvp => kvp.Value.host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.FirstOrDefault()
|
kvp => kvp.Value.host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses.FirstOrDefault()
|
||||||
.Replace("127.0.0.1", "localhost") + kvp.Value.basePath);
|
.Replace("127.0.0.1", "localhost") + kvp.Value.basePath);
|
||||||
|
|
||||||
mainHost.Run();
|
mainHost.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (IWebHost host, string basePath) CreateDevServerHost(string[] args)
|
private static (IHost host, string basePath) CreateDevServerHost(string[] args)
|
||||||
{
|
{
|
||||||
var contentRoot = typeof(Program).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
|
var contentRoot = typeof(Program).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
|
||||||
.Single(a => a.Key == "Microsoft.AspNetCore.Testing.BasicTestApp.ContentRoot")
|
.Single(a => a.Key == "Microsoft.AspNetCore.Testing.BasicTestApp.ContentRoot")
|
||||||
|
|
@ -60,46 +60,30 @@ namespace TestServer
|
||||||
"--applicationpath", typeof(BasicTestApp.Program).Assembly.Location,
|
"--applicationpath", typeof(BasicTestApp.Program).Assembly.Location,
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
var host = DevServerProgram.BuildWebHost(finalArgs);
|
var host = DevServerProgram.BuildWebHost(finalArgs);
|
||||||
return (new WebHostShim(host), "/subdir");
|
return (host, "/subdir");
|
||||||
}
|
|
||||||
|
|
||||||
private class WebHostShim : IWebHost
|
|
||||||
{
|
|
||||||
private readonly IHost _host;
|
|
||||||
|
|
||||||
public WebHostShim(IHost host) => _host = host;
|
|
||||||
|
|
||||||
public IFeatureCollection ServerFeatures => _host.Services.GetRequiredService<IServer>().Features;
|
|
||||||
|
|
||||||
public IServiceProvider Services => _host.Services;
|
|
||||||
|
|
||||||
public void Dispose() => _host.Dispose();
|
|
||||||
|
|
||||||
public void Start() => _host.Start();
|
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken = default) => _host.StartAsync(cancellationToken);
|
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken = default) => _host.StopAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string[] CreateAdditionalArgs(string[] args) =>
|
private static string[] CreateAdditionalArgs(string[] args) =>
|
||||||
args.Concat(new[] { "--urls", "http://127.0.0.1:0" }).ToArray();
|
args.Concat(new[] { "--urls", "http://127.0.0.1:0" }).ToArray();
|
||||||
|
|
||||||
public static IWebHost BuildWebHost(string[] args) => BuildWebHost<Startup>(args);
|
public static IHost BuildWebHost(string[] args) => BuildWebHost<Startup>(args);
|
||||||
|
|
||||||
public static IWebHost BuildWebHost<TStartup>(string[] args) where TStartup : class =>
|
public static IHost BuildWebHost<TStartup>(string[] args) where TStartup : class =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureLogging((ctx, lb) =>
|
.ConfigureLogging((ctx, lb) =>
|
||||||
{
|
{
|
||||||
TestSink sink = new TestSink();
|
TestSink sink = new TestSink();
|
||||||
lb.AddProvider(new TestLoggerProvider(sink));
|
lb.AddProvider(new TestLoggerProvider(sink));
|
||||||
lb.Services.Add(ServiceDescriptor.Singleton(sink));
|
lb.Services.Add(ServiceDescriptor.Singleton(sink));
|
||||||
})
|
})
|
||||||
.UseConfiguration(new ConfigurationBuilder()
|
.ConfigureWebHostDefaults(webHostBuilder =>
|
||||||
.AddCommandLine(args)
|
{
|
||||||
.Build())
|
webHostBuilder.UseStartup<TStartup>();
|
||||||
.UseStartup<TStartup>()
|
|
||||||
.UseStaticWebAssets()
|
// We require this line because we run in Production environment
|
||||||
|
// and static web assets are only on by default during development.
|
||||||
|
webHostBuilder.UseStaticWebAssets();
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue