Improvements to the testing package.
* Detect a ConfigureDefaultBuilder static method on Program.cs of the test site and use it to initialize the IWebHostBuilder instance that we configure for testing. * Make use of the new service configuration extensibility points in Hosting to override services for testing. * Get rid of MvcWebApplicationBuilder and move MVC specific methods into extension methods.
This commit is contained in:
parent
72eb546329
commit
96bd2769d0
|
|
@ -1,33 +0,0 @@
|
|||
// 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 System.Collections.Generic;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Testing.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper class to orchestrate service registrations in <see cref="TestStartup{TStartup}"/>.
|
||||
/// </summary>
|
||||
public class TestServiceRegistrations
|
||||
{
|
||||
public IList<Action<IServiceCollection>> Before { get; set; } = new List<Action<IServiceCollection>>();
|
||||
public IList<Action<IServiceCollection>> After { get; set; } = new List<Action<IServiceCollection>>();
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, Action startupConfigureServices)
|
||||
{
|
||||
foreach (var config in Before)
|
||||
{
|
||||
config(services);
|
||||
}
|
||||
|
||||
startupConfigureServices();
|
||||
|
||||
foreach (var config in After)
|
||||
{
|
||||
config(services);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
// 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 System.Linq;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Testing.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Fake startup class used in functional tests to decorate the registration of
|
||||
/// ConfigureServices.
|
||||
/// </summary>
|
||||
/// <typeparam name="TStartup">The startup class of your application.</typeparam>
|
||||
public class TestStartup<TStartup> where TStartup : class
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly TestServiceRegistrations _registrations;
|
||||
private readonly TStartup _instance;
|
||||
|
||||
public TestStartup(IServiceProvider serviceProvider, TestServiceRegistrations registrations)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_registrations = registrations;
|
||||
_instance = (TStartup)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TStartup));
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
var configureServices = _instance.GetType().GetMethod(nameof(ConfigureServices));
|
||||
var parameters = Enumerable.Repeat(services, 1)
|
||||
.Concat(configureServices
|
||||
.GetParameters()
|
||||
.Skip(1)
|
||||
.Select(p => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, p.ParameterType)))
|
||||
.ToArray();
|
||||
|
||||
_registrations.ConfigureServices(services, () => configureServices.Invoke(_instance, parameters));
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder applicationBuilder)
|
||||
{
|
||||
var configure = _instance.GetType().GetMethod(nameof(Configure));
|
||||
var parameters = Enumerable.Repeat(applicationBuilder, 1)
|
||||
.Concat(configure
|
||||
.GetParameters()
|
||||
.Skip(1)
|
||||
.Select(p => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, p.ParameterType)))
|
||||
.ToArray();
|
||||
|
||||
configure.Invoke(_instance, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,145 +0,0 @@
|
|||
// 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 System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Internal;
|
||||
using Microsoft.AspNetCore.Mvc.Testing.Internal;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Testing
|
||||
{
|
||||
/// <summary>
|
||||
/// Builder API for bootstraping an MVC application for functional tests.
|
||||
/// </summary>
|
||||
/// <typeparam name="TStartup">The application startup class.</typeparam>
|
||||
public class MvcWebApplicationBuilder<TStartup> where TStartup : class
|
||||
{
|
||||
public string ContentRoot { get; set; }
|
||||
public IList<Action<IServiceCollection>> ConfigureServicesBeforeStartup { get; set; } = new List<Action<IServiceCollection>>();
|
||||
public IList<Action<IServiceCollection>> ConfigureServicesAfterStartup { get; set; } = new List<Action<IServiceCollection>>();
|
||||
public List<Assembly> ApplicationAssemblies { get; set; } = new List<Assembly>();
|
||||
|
||||
/// <summary>
|
||||
/// Configures services before TStartup.ConfigureServices runs.
|
||||
/// </summary>
|
||||
/// <param name="configure">The <see cref="Action{IServiceCollection}"/> to configure the services with.</param>
|
||||
/// <returns>An instance of this <see cref="MvcWebApplicationBuilder{TStartup}"/></returns>
|
||||
public MvcWebApplicationBuilder<TStartup> ConfigureBeforeStartup(Action<IServiceCollection> configure)
|
||||
{
|
||||
ConfigureServicesBeforeStartup.Add(configure);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures services after TStartup.ConfigureServices runs.
|
||||
/// </summary>
|
||||
/// <param name="configure">The <see cref="Action{IServiceCollection}"/> to configure the services with.</param>
|
||||
/// <returns>An instance of this <see cref="MvcWebApplicationBuilder{TStartup}"/></returns>
|
||||
public MvcWebApplicationBuilder<TStartup> ConfigureAfterStartup(Action<IServiceCollection> configure)
|
||||
{
|
||||
ConfigureServicesAfterStartup.Add(configure);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures <see cref="ApplicationPartManager"/> to include the default set
|
||||
/// of <see cref="ApplicationPart"/> provided by <see cref="DefaultAssemblyPartDiscoveryProvider"/>.
|
||||
/// </summary>
|
||||
/// <returns>An instance of this <see cref="MvcWebApplicationBuilder{TStartup}"/></returns>
|
||||
public MvcWebApplicationBuilder<TStartup> UseApplicationAssemblies()
|
||||
{
|
||||
var depsFileName = $"{typeof(TStartup).Assembly.GetName().Name}.deps.json";
|
||||
var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName));
|
||||
if (!depsFile.Exists)
|
||||
{
|
||||
throw new InvalidOperationException($"Can't find'{depsFile.FullName}'. This file is required for functional tests " +
|
||||
"to run properly. There should be a copy of the file on your source project bin folder. If thats not the " +
|
||||
"case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g" +
|
||||
"'<PreserveCompilationContext>true</PreserveCompilationContext>'." +
|
||||
$"For functional tests to work they need to either run from the build output folder or the {Path.GetFileName(depsFile.FullName)} " +
|
||||
$"file from your application's output directory must be copied" +
|
||||
"to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the " +
|
||||
"tests run.");
|
||||
}
|
||||
|
||||
ApplicationAssemblies.AddRange(DefaultAssemblyPartDiscoveryProvider
|
||||
.DiscoverAssemblyParts(typeof(TStartup).Assembly.GetName().Name)
|
||||
.Select(s => ((AssemblyPart)s).Assembly)
|
||||
.ToList());
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the application content root.
|
||||
/// </summary>
|
||||
/// <param name="solutionName">The glob pattern to use for finding the solution.</param>
|
||||
/// <param name="solutionRelativePath">The relative path to the content root from the solution file.</param>
|
||||
/// <returns>An instance of this <see cref="MvcWebApplicationBuilder{TStartup}"/></returns>
|
||||
public MvcWebApplicationBuilder<TStartup> UseSolutionRelativeContentRoot(
|
||||
string solutionRelativePath,
|
||||
string solutionName = "*.sln")
|
||||
{
|
||||
if (solutionRelativePath == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(solutionRelativePath));
|
||||
}
|
||||
|
||||
var applicationBasePath = AppContext.BaseDirectory;
|
||||
|
||||
var directoryInfo = new DirectoryInfo(applicationBasePath);
|
||||
do
|
||||
{
|
||||
var solutionPath = Directory.EnumerateFiles(directoryInfo.FullName, solutionName).FirstOrDefault();
|
||||
if (solutionPath != null)
|
||||
{
|
||||
ContentRoot = Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath));
|
||||
return this;
|
||||
}
|
||||
|
||||
directoryInfo = directoryInfo.Parent;
|
||||
}
|
||||
while (directoryInfo.Parent != null);
|
||||
|
||||
throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
|
||||
}
|
||||
|
||||
public TestServer Build()
|
||||
{
|
||||
var builder = new WebHostBuilder()
|
||||
.UseStartup<TestStartup<TStartup>>()
|
||||
// This is necessary so that IHostingEnvironment.ApplicationName has the right
|
||||
// value and libraries depending on it (to load the dependency context, for example)
|
||||
// work properly.
|
||||
.UseSetting(WebHostDefaults.ApplicationKey, typeof(TStartup).Assembly.GetName().Name)
|
||||
.UseContentRoot(ContentRoot)
|
||||
.ConfigureServices(InitializeServices);
|
||||
|
||||
return new TestServer(builder);
|
||||
}
|
||||
|
||||
protected virtual void InitializeServices(IServiceCollection services)
|
||||
{
|
||||
// Inject a custom application part manager. Overrides AddMvcCore() because that uses TryAdd().
|
||||
var manager = new ApplicationPartManager();
|
||||
foreach (var assembly in ApplicationAssemblies)
|
||||
{
|
||||
manager.ApplicationParts.Add(new AssemblyPart(assembly));
|
||||
}
|
||||
|
||||
services.AddSingleton(manager);
|
||||
services.AddSingleton(new TestServiceRegistrations
|
||||
{
|
||||
Before = ConfigureServicesBeforeStartup,
|
||||
After = ConfigureServicesAfterStartup
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
// <auto-generated />
|
||||
namespace Microsoft.AspNetCore.Mvc.Testing
|
||||
{
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
|
||||
internal static class Resources
|
||||
{
|
||||
private static readonly ResourceManager _resourceManager
|
||||
= new ResourceManager("Microsoft.AspNetCore.Mvc.Testing.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
||||
|
||||
/// <summary>
|
||||
/// Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.
|
||||
/// </summary>
|
||||
internal static string MissingDepsFile
|
||||
{
|
||||
get => GetString("MissingDepsFile");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.
|
||||
/// </summary>
|
||||
internal static string FormatMissingDepsFile(object p0, object p1)
|
||||
=> string.Format(CultureInfo.CurrentCulture, GetString("MissingDepsFile"), p0, p1);
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
||||
System.Diagnostics.Debug.Assert(value != null);
|
||||
|
||||
if (formatterNames != null)
|
||||
{
|
||||
for (var i = 0; i < formatterNames.Length; i++)
|
||||
{
|
||||
value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}");
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="MissingDepsFile" xml:space="preserve">
|
||||
<value>Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
// 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.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.Testing
|
||||
|
|
@ -23,7 +24,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// This constructor will infer the application root directive by searching for a solution file (*.sln) and then
|
||||
/// appending the path<c> src/{AssemblyName}</c> to the solution directory.The application root directory will be
|
||||
/// appending the path<c>{AssemblyName}</c> to the solution directory.The application root directory will be
|
||||
/// used to discover views and content files.
|
||||
/// </para>
|
||||
/// <para>
|
||||
|
|
@ -33,7 +34,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// </para>
|
||||
/// </summary>
|
||||
public WebApplicationTestFixture()
|
||||
: this(Path.Combine("src", typeof(TStartup).Assembly.GetName().Name))
|
||||
: this(typeof(TStartup).Assembly.GetName().Name)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -56,7 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// <param name="solutionRelativePath">The path to the project folder relative to the solution file of your
|
||||
/// application. The folder of the first .sln file found traversing up the folder hierarchy from the test execution
|
||||
/// folder is considered as the base path.</param>
|
||||
protected WebApplicationTestFixture(string solutionRelativePath)
|
||||
public WebApplicationTestFixture(string solutionRelativePath)
|
||||
: this("*.sln", solutionRelativePath)
|
||||
{
|
||||
}
|
||||
|
|
@ -82,35 +83,59 @@ namespace Microsoft.AspNetCore.Mvc.Testing
|
|||
/// <param name="solutionRelativePath">The path to the project folder relative to the solution file of your
|
||||
/// application. The folder of the first sln file that matches the <paramref name="solutionSearchPattern"/>
|
||||
/// found traversing up the folder hierarchy from the test execution folder is considered as the base path.</param>
|
||||
protected WebApplicationTestFixture(string solutionSearchPattern, string solutionRelativePath)
|
||||
public WebApplicationTestFixture(string solutionSearchPattern, string solutionRelativePath)
|
||||
{
|
||||
var builder = new MvcWebApplicationBuilder<TStartup>()
|
||||
.UseSolutionRelativeContentRoot(solutionRelativePath)
|
||||
.UseApplicationAssemblies();
|
||||
EnsureDepsFile();
|
||||
|
||||
ConfigureApplication(builder);
|
||||
var builder = CreateWebHostBuilder();
|
||||
builder
|
||||
.UseStartup<TStartup>()
|
||||
.UseSolutionRelativeContentRoot(solutionRelativePath);
|
||||
|
||||
ConfigureWebHost(builder);
|
||||
_server = CreateServer(builder);
|
||||
|
||||
Client = _server.CreateClient();
|
||||
Client.BaseAddress = new Uri("http://localhost");
|
||||
}
|
||||
|
||||
private void EnsureDepsFile()
|
||||
{
|
||||
var depsFileName = $"{typeof(TStartup).Assembly.GetName().Name}.deps.json";
|
||||
var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName));
|
||||
if (!depsFile.Exists)
|
||||
{
|
||||
throw new InvalidOperationException(Resources.FormatMissingDepsFile(
|
||||
depsFile.FullName,
|
||||
Path.GetFileName(depsFile.FullName)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="IWebHostBuilder"/> used to setup <see cref="TestServer"/>.
|
||||
/// <remarks>
|
||||
/// The default implementation of this method looks for a <c>public static IWebHostBuilder CreateDefaultBuilder(string[] args)</c>
|
||||
/// method defined on the entry point of the assembly of <typeparamref name="TStartup" /> and invokes it passing an empty string
|
||||
/// array as arguments. In case this method can't be found,
|
||||
/// </remarks>
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="IWebHostBuilder"/> instance.</returns>
|
||||
protected virtual IWebHostBuilder CreateWebHostBuilder() =>
|
||||
WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint<TStartup>(Array.Empty<string>()) ?? new WebHostBuilder();
|
||||
|
||||
/// <summary>
|
||||
/// Creates the <see cref="TestServer"/> with the bootstrapped application in <paramref name="builder"/>.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="MvcWebApplicationBuilder{TStartup}"/> used to
|
||||
/// <param name="builder">The <see cref="IWebHostBuilder"/> used to
|
||||
/// create the server.</param>
|
||||
/// <returns>The <see cref="TestServer"/> with the bootstrapped application.</returns>
|
||||
protected virtual TestServer CreateServer(MvcWebApplicationBuilder<TStartup> builder)
|
||||
{
|
||||
return builder.Build();
|
||||
}
|
||||
protected virtual TestServer CreateServer(IWebHostBuilder builder) => new TestServer(builder);
|
||||
|
||||
/// <summary>
|
||||
/// Gives a fixture an opportunity to configure the application before it gets built.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="MvcWebApplicationBuilder{TStartup}"/> for the application.</param>
|
||||
protected virtual void ConfigureApplication(MvcWebApplicationBuilder<TStartup> builder)
|
||||
/// <param name="builder">The <see cref="IWebHostBuilder"/> for the application.</param>
|
||||
protected virtual void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -460,5 +460,15 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
var result = JsonConvert.DeserializeObject<Product[]>(response);
|
||||
Assert.Equal(2, result.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TestingInfrastructure_InvokesCreateDefaultBuilder()
|
||||
{
|
||||
// Act
|
||||
var response = await Client.GetStringAsync("Testing/Builder");
|
||||
|
||||
// Assert
|
||||
Assert.Equal("true", response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.WebEncoders.Testing;
|
||||
|
||||
|
|
@ -11,10 +11,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
public class MvcEncodedTestFixture<TStartup> : MvcTestFixture<TStartup>
|
||||
where TStartup : class
|
||||
{
|
||||
protected override void ConfigureApplication(MvcWebApplicationBuilder<TStartup> builder)
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureApplication(builder);
|
||||
builder.ConfigureBeforeStartup(services =>
|
||||
base.ConfigureWebHost(builder);
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.TryAddTransient<HtmlEncoder, HtmlTestEncoder>();
|
||||
services.TryAddTransient<JavaScriptEncoder, JavaScriptTestEncoder>();
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
|
||||
|
|
@ -22,14 +22,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
{
|
||||
}
|
||||
|
||||
protected override void ConfigureApplication(MvcWebApplicationBuilder<TStartup> builder)
|
||||
{
|
||||
builder.UseRequestCulture("en-GB", "en-US");
|
||||
builder.ApplicationAssemblies.Clear();
|
||||
builder.ApplicationAssemblies.Add(typeof(TStartup).GetTypeInfo().Assembly);
|
||||
}
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder) =>
|
||||
builder.UseRequestCulture<TStartup>("en-GB", "en-US");
|
||||
|
||||
protected override TestServer CreateServer(MvcWebApplicationBuilder<TStartup> builder)
|
||||
protected override TestServer CreateServer(IWebHostBuilder builder)
|
||||
{
|
||||
var originalCulture = CultureInfo.CurrentCulture;
|
||||
var originalUICulture = CultureInfo.CurrentUICulture;
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
/// </summary>
|
||||
/// <param name="culture">The culture to use when processing <see cref="HttpRequest"/>.</param>
|
||||
/// <param name="uiCulture">The UI culture to use when processing <see cref="HttpRequest"/>.</param>
|
||||
/// <returns>An instance of this <see cref="MvcWebApplicationBuilder{TStartup}"/></returns>
|
||||
public static MvcWebApplicationBuilder<TStartup> UseRequestCulture<TStartup>(this MvcWebApplicationBuilder<TStartup> builder, string culture, string uiCulture)
|
||||
/// <returns>An instance of this <see cref="IWebHostBuilder"/></returns>
|
||||
public static IWebHostBuilder UseRequestCulture<TStartup>(this IWebHostBuilder builder, string culture, string uiCulture)
|
||||
where TStartup : class
|
||||
{
|
||||
if (culture == null)
|
||||
|
|
@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
throw new ArgumentNullException(nameof(uiCulture));
|
||||
}
|
||||
|
||||
builder.ConfigureBeforeStartup(services =>
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.TryAddSingleton(new TestCulture
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BasicWebSite.Controllers
|
||||
{
|
||||
public class TestingController
|
||||
{
|
||||
public TestingController(TestService service)
|
||||
{
|
||||
Service = service;
|
||||
}
|
||||
|
||||
public TestService Service { get; }
|
||||
|
||||
[HttpGet("Testing/Builder")]
|
||||
public string Get() => Service.Message;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,21 +3,27 @@
|
|||
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BasicWebSite
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var host = new WebHostBuilder()
|
||||
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
|
||||
|
||||
// Do not change. This is the pattern our test infrastructure uses to initialize a IWebHostBuilder from
|
||||
// a users app.
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
new WebHostBuilder()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseStartup<Startup>()
|
||||
.ConfigureServices(s => s.AddSingleton(new TestService { Message = "true" }))
|
||||
.UseKestrel()
|
||||
.UseIISIntegration()
|
||||
.Build();
|
||||
.UseIISIntegration();
|
||||
}
|
||||
|
||||
host.Run();
|
||||
}
|
||||
public class TestService
|
||||
{
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue