diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Microsoft.AspNetCore.Mvc.Testing.csproj b/src/Microsoft.AspNetCore.Mvc.Testing/Microsoft.AspNetCore.Mvc.Testing.csproj index eebf6d1fac..33173f515d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Microsoft.AspNetCore.Mvc.Testing.csproj +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Microsoft.AspNetCore.Mvc.Testing.csproj @@ -17,7 +17,7 @@ - + diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs b/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs index 56e4b45dce..c5a4d94bdc 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs @@ -3,18 +3,15 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.Testing.Internal; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.AspNetCore.Mvc.Testing { @@ -24,8 +21,6 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// The application startup class. public class MvcWebApplicationBuilder where TStartup : class { - private TestCulture _systemCulture; - public string ContentRoot { get; set; } public IList> ConfigureServicesBeforeStartup { get; set; } = new List>(); public IList> ConfigureServicesAfterStartup { get; set; } = new List>(); @@ -82,64 +77,6 @@ namespace Microsoft.AspNetCore.Mvc.Testing return this; } - /// - /// Sets up an that configures the at the - /// beginning of the pipeline to change the and - /// of the thread so that they match the cultures in and for the rest of the - /// . - /// - /// The culture to use when processing . - /// The UI culture to use when processing . - /// An instance of this - public MvcWebApplicationBuilder UseRequestCulture(string culture, string uiCulture) - { - if (culture == null) - { - throw new ArgumentNullException(nameof(culture)); - } - - if (uiCulture == null) - { - throw new ArgumentNullException(nameof(uiCulture)); - } - - ConfigureBeforeStartup(services => - { - services.TryAddSingleton(new TestCulture - { - Culture = culture, - UICulture = uiCulture - }); - services.TryAddSingleton(); - }); - - return this; - } - - /// - /// Overrides the and the - /// of the system during the initial configuration of the in case there is any middleware - /// that captures the current culture of the system when the pipeline is being constructed. - /// - /// The culture to use when processing . - /// The UI culture to use when processing . - /// An instance of this - public MvcWebApplicationBuilder UseStartupCulture(string culture, string uiCulture) - { - if (culture == null) - { - throw new ArgumentNullException(nameof(culture)); - } - - if (uiCulture == null) - { - throw new ArgumentNullException(nameof(uiCulture)); - } - - _systemCulture = new TestCulture { Culture = culture, UICulture = uiCulture }; - return this; - } - /// /// Configures the application content root. /// @@ -185,24 +122,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing .UseContentRoot(ContentRoot) .ConfigureServices(InitializeServices); - if (_systemCulture == null) - { - return new TestServer(builder); - } - - var originalCulture = CultureInfo.CurrentCulture; - var originalUICulture = CultureInfo.CurrentUICulture; - try - { - CultureInfo.CurrentCulture = new CultureInfo(_systemCulture.Culture); - CultureInfo.CurrentUICulture = new CultureInfo(_systemCulture.UICulture); - return new TestServer(builder); - } - finally - { - CultureInfo.CurrentCulture = originalCulture; - CultureInfo.CurrentUICulture = originalUICulture; - } + return new TestServer(builder); } protected virtual void InitializeServices(IServiceCollection services) diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs index 2495a7824a..00ede535b4 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs @@ -4,12 +4,7 @@ using System; using System.IO; using System.Net.Http; -using System.Reflection; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing.Internal; using Microsoft.AspNetCore.TestHost; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.AspNetCore.Mvc.Testing { @@ -21,43 +16,102 @@ namespace Microsoft.AspNetCore.Mvc.Testing { private readonly TestServer _server; + /// + /// + /// Creates a TestServer instance using the MVC application defined by. + /// The startup code defined in will be executed to configure the application. + /// + /// + /// This constructor will infer the application root directive by searching for a solution file (*.sln) and then + /// appending the path src/{AssemblyName} to the solution directory.The application root directory will be + /// used to discover views and content files. + /// + /// + /// The application assemblies will be loaded from the dependency context of the assembly containing + /// .This means that project dependencies of the assembly containing + /// will be loaded as application assemblies. + /// + /// public WebApplicationTestFixture() - : this("src") + : this(Path.Combine("src", typeof(TStartup).Assembly.GetName().Name)) { } + /// + /// + /// Creates a TestServer instance using the MVC application defined by. + /// The startup code defined in will be executed to configure the application. + /// + /// + /// This constructor will infer the application root directive by searching for a solution file (*.sln) and then + /// appending the path to the solution directory.The application root + /// directory will be used to discover views and content files. + /// + /// + /// The application assemblies will be loaded from the dependency context of the assembly containing + /// .This means that project dependencies of the assembly containing + /// will be loaded as application assemblies. + /// + /// + /// 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. protected WebApplicationTestFixture(string solutionRelativePath) : this("*.sln", solutionRelativePath) { } + /// + /// + /// Creates a TestServer instance using the MVC application defined by. + /// The startup code defined in will be executed to configure the application. + /// + /// + /// This constructor will infer the application root directive by searching for a solution file tht matches the pattern + /// and then appending the path + /// to the solution directory.The application root directory will be used to discover views and content files. + /// + /// + /// The application assemblies will be loaded from the dependency context of the assembly containing + /// .This means that project dependencies of the assembly containing + /// will be loaded as application assemblies. + /// + /// + /// The glob pattern to use when searching for a solution file by + /// traversing up the folder hierarchy from the test execution folder. + /// The path to the project folder relative to the solution file of your + /// application. The folder of the first sln file that matches the + /// found traversing up the folder hierarchy from the test execution folder is considered as the base path. protected WebApplicationTestFixture(string solutionSearchPattern, string solutionRelativePath) { - var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; - - // This step assumes project name = assembly name. - var projectName = startupAssembly.GetName().Name; - var projectPath = Path.Combine(solutionRelativePath, projectName); var builder = new MvcWebApplicationBuilder() - .UseSolutionRelativeContentRoot(projectPath) - .UseApplicationAssemblies() - .UseRequestCulture("en-GB", "en-US") - .UseStartupCulture("en-GB", "en-US"); + .UseSolutionRelativeContentRoot(solutionRelativePath) + .UseApplicationAssemblies(); ConfigureApplication(builder); - _server = builder.Build(); + _server = CreateServer(builder); Client = _server.CreateClient(); Client.BaseAddress = new Uri("http://localhost"); } + /// + /// Creates the with the bootstrapped application in . + /// + /// The used to + /// create the server. + /// The with the bootstrapped application. + protected virtual TestServer CreateServer(MvcWebApplicationBuilder builder) + { + return builder.Build(); + } + /// /// Gives a fixture an opportunity to configure the application before it gets built. /// /// The for the application. protected virtual void ConfigureApplication(MvcWebApplicationBuilder builder) { - builder.ConfigureAfterStartup(s => s.TryAddEnumerable(ServiceDescriptor.Transient())); } /// @@ -98,7 +152,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing else { - for (var i = handlers.Length - 1; i > 1; i++) + for (var i = handlers.Length - 1; i > 1; i--) { handlers[i - 1].InnerHandler = handlers[i]; } diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/CultureReplacerMiddleware.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/CultureReplacerMiddleware.cs similarity index 97% rename from src/Microsoft.AspNetCore.Mvc.Testing/Internal/CultureReplacerMiddleware.cs rename to test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/CultureReplacerMiddleware.cs index 8e8cf8d19c..ae40cee982 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/CultureReplacerMiddleware.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/CultureReplacerMiddleware.cs @@ -5,7 +5,7 @@ using System.Globalization; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -namespace Microsoft.AspNetCore.Mvc.Testing.Internal +namespace Microsoft.AspNetCore.Mvc.FunctionalTests { /// /// A middleware that ensures web sites run in a consistent culture. Currently useful for tests that format dates, diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/CultureReplacerStartupFilter.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/CultureReplacerStartupFilter.cs similarity index 94% rename from src/Microsoft.AspNetCore.Mvc.Testing/Internal/CultureReplacerStartupFilter.cs rename to test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/CultureReplacerStartupFilter.cs index bb63e6adf1..dff810ddaf 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/CultureReplacerStartupFilter.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/CultureReplacerStartupFilter.cs @@ -5,7 +5,7 @@ using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -namespace Microsoft.AspNetCore.Mvc.Testing.Internal +namespace Microsoft.AspNetCore.Mvc.FunctionalTests { /// /// Inserts the at the beginning of the pipeline. diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcEncodedTestFixtureOfT.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcEncodedTestFixtureOfT.cs similarity index 100% rename from test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcEncodedTestFixtureOfT.cs rename to test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcEncodedTestFixtureOfT.cs diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs similarity index 68% rename from test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs rename to test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs index c17a3f504b..17fce4b085 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcSampleFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs @@ -1,11 +1,14 @@ // 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.IO; +using System.Reflection; + namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public class MvcSampleFixture : MvcTestFixture where TStartup : class { - public MvcSampleFixture() : base("samples") { } + public MvcSampleFixture() : base(Path.Combine("samples", typeof(TStartup).Assembly.GetName().Name)) { } } } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs new file mode 100644 index 0000000000..36470f9d2f --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs @@ -0,0 +1,49 @@ +// 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.Globalization; +using System.IO; +using System.Reflection; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; + +namespace Microsoft.AspNetCore.Mvc.FunctionalTests +{ + public class MvcTestFixture : WebApplicationTestFixture + where TStartup : class + { + public MvcTestFixture() + : base(Path.Combine("test", "WebSites", typeof(TStartup).Assembly.GetName().Name)) + { + } + + protected MvcTestFixture(string solutionRelativePath) + : base(solutionRelativePath) + { + } + + protected override void ConfigureApplication(MvcWebApplicationBuilder builder) + { + builder.UseRequestCulture("en-GB", "en-US"); + builder.ApplicationAssemblies.Clear(); + builder.ApplicationAssemblies.Add(typeof(TStartup).GetTypeInfo().Assembly); + } + + protected override TestServer CreateServer(MvcWebApplicationBuilder builder) + { + var originalCulture = CultureInfo.CurrentCulture; + var originalUICulture = CultureInfo.CurrentUICulture; + try + { + CultureInfo.CurrentCulture = new CultureInfo("en-GB"); + CultureInfo.CurrentUICulture = new CultureInfo("en-US"); + return base.CreateServer(builder); + } + finally + { + CultureInfo.CurrentCulture = originalCulture; + CultureInfo.CurrentUICulture = originalUICulture; + } + } + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..6a39eb5785 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs @@ -0,0 +1,49 @@ +// 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.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.AspNetCore.Mvc.FunctionalTests +{ + public static class MvcWebApplicationBuilderExtensions + { + /// + /// Sets up an that configures the at the + /// beginning of the pipeline to change the and + /// of the thread so that they match the cultures in and for the rest of the + /// . + /// + /// The culture to use when processing . + /// The UI culture to use when processing . + /// An instance of this + public static MvcWebApplicationBuilder UseRequestCulture(this MvcWebApplicationBuilder builder, string culture, string uiCulture) + where TStartup : class + { + if (culture == null) + { + throw new ArgumentNullException(nameof(culture)); + } + + if (uiCulture == null) + { + throw new ArgumentNullException(nameof(uiCulture)); + } + + builder.ConfigureBeforeStartup(services => + { + services.TryAddSingleton(new TestCulture + { + Culture = culture, + UICulture = uiCulture + }); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); + }); + + return builder; + } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestCulture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/TestCulture.cs similarity index 85% rename from src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestCulture.cs rename to test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/TestCulture.cs index 7c0be08210..180fa7490b 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestCulture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/TestCulture.cs @@ -1,7 +1,7 @@ // 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. -namespace Microsoft.AspNetCore.Mvc.Testing.Internal +namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public class TestCulture { diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs deleted file mode 100644 index 6fb5c1661e..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/MvcTestFixture.cs +++ /dev/null @@ -1,30 +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.IO; -using System.Reflection; -using Microsoft.AspNetCore.Mvc.Testing; - -namespace Microsoft.AspNetCore.Mvc.FunctionalTests -{ - public class MvcTestFixture : WebApplicationTestFixture - where TStartup : class - { - public MvcTestFixture() - : base(Path.Combine("test", "WebSites")) - { - } - - protected MvcTestFixture(string solutionRelativePath) - : base(solutionRelativePath) - { - } - - protected override void ConfigureApplication(MvcWebApplicationBuilder builder) - { - base.ConfigureApplication(builder); - builder.ApplicationAssemblies.Clear(); - builder.ApplicationAssemblies.Add(typeof(TStartup).GetTypeInfo().Assembly); - } - } -}