diff --git a/samples/LocalizationSample/LocalizationSample.csproj b/samples/LocalizationSample/LocalizationSample.csproj index 81c4203bcb..aed1b732b6 100644 --- a/samples/LocalizationSample/LocalizationSample.csproj +++ b/samples/LocalizationSample/LocalizationSample.csproj @@ -1,6 +1,6 @@  - + netcoreapp2.0;net461 diff --git a/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationSampleTest.cs b/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationSampleTest.cs index c97859f21b..0b5e64f48a 100644 --- a/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationSampleTest.cs +++ b/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationSampleTest.cs @@ -1,27 +1,38 @@ // 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; +using System.Net.Http; using System.Threading.Tasks; -using Microsoft.AspNetCore.Server.IntegrationTesting; +using LocalizationSample; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; using Xunit; namespace Microsoft.AspNetCore.Localization.FunctionalTests { public class LocalizationSampleTest { - private static readonly string _applicationPath = Path.Combine("samples", "LocalizationSample"); - [Fact] - public Task RunSite() + public async Task LocalizationSampleSmokeTest() { - var testRunner = new TestRunner(_applicationPath); + // Arrange + var webHostBuilder = new WebHostBuilder().UseStartup(typeof(Startup)); + var testHost = new TestServer(webHostBuilder); + var locale = "fr-FR"; + var client = testHost.CreateClient(); + var request = new HttpRequestMessage(HttpMethod.Get, "My/Resources"); + var cookieValue = $"c={locale}|uic={locale}"; + request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}"); - return testRunner.RunTestAndVerifyResponseHeading( - RuntimeArchitecture.x64, - "My/Resources", - "fr-FR", - "

Bonjour

"); + // Act + var response = await client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Contains("

Bonjour

", await response.Content.ReadAsStringAsync()); } } } diff --git a/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationTest.cs b/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationTest.cs index 8c0015da4d..5181e87f42 100644 --- a/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationTest.cs +++ b/test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationTest.cs @@ -1,25 +1,24 @@ // 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; +using System.Net; +using System.Net.Http; using System.Threading.Tasks; -using Microsoft.AspNetCore.Server.IntegrationTesting; +using LocalizationWebsite; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; using Xunit; namespace Microsoft.AspNetCore.Localization.FunctionalTests { public class LocalizationTest { - private static readonly string _applicationPath = Path.Combine("test", "LocalizationWebsite"); - [Fact] public Task Localization_CustomCulture() { - var testRunner = new TestRunner(_applicationPath); - - return testRunner.RunTestAndVerifyResponse( - RuntimeArchitecture.x64, - "CustomCulturePreserved", + return RunTest( + typeof(StartupCustomCulturePreserved), "en-US", "kr10.00"); } @@ -27,11 +26,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests [Fact] public Task Localization_ResourcesInClassLibrary_ReturnLocalizedValue() { - var testRunner = new TestRunner(_applicationPath); - - return testRunner.RunTestAndVerifyResponse( - RuntimeArchitecture.x64, - "ResourcesInClassLibrary", + return RunTest( + typeof(StartupResourcesInClassLibrary), "fr-FR", "Bonjour from ResourcesClassLibraryNoAttribute Bonjour from ResourcesClassLibraryNoAttribute Bonjour from ResourcesClassLibraryWithAttribute Bonjour from ResourcesClassLibraryWithAttribute"); } @@ -39,11 +35,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests [Fact] public Task Localization_ResourcesInFolder_ReturnLocalizedValue() { - var testRunner = new TestRunner(_applicationPath); - - return testRunner.RunTestAndVerifyResponse( - RuntimeArchitecture.x64, - "ResourcesInFolder", + return RunTest( + typeof(StartupResourcesInFolder), "fr-FR", "Bonjour from StartupResourcesInFolder Bonjour from Test in resources folder Bonjour from Customer in resources folder Hello"); } @@ -51,11 +44,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests [Fact] public Task Localization_ResourcesInFolder_ReturnLocalizedValue_WithCultureFallback() { - var testRunner = new TestRunner(_applicationPath); - - return testRunner.RunTestAndVerifyResponse( - RuntimeArchitecture.x64, - "ResourcesInFolder", + return RunTest( + typeof(StartupResourcesInFolder), "fr-FR-test", "Bonjour from StartupResourcesInFolder Bonjour from Test in resources folder Bonjour from Customer in resources folder Hello"); } @@ -63,11 +53,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests [Fact] public Task Localization_ResourcesInFolder_ReturnNonLocalizedValue_CultureHierarchyTooDeep() { - var testRunner = new TestRunner(_applicationPath); - - return testRunner.RunTestAndVerifyResponse( - RuntimeArchitecture.x64, - "ResourcesInFolder", + return RunTest( + typeof(StartupResourcesInFolder), "fr-FR-test-again-too-deep-to-work", "Hello Hello Hello Hello"); } @@ -75,13 +62,26 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests [Fact] public Task Localization_ResourcesAtRootFolder_ReturnLocalizedValue() { - var testRunner = new TestRunner(_applicationPath); - - return testRunner.RunTestAndVerifyResponse( - RuntimeArchitecture.x64, - "ResourcesAtRootFolder", + return RunTest( + typeof(StartupResourcesAtRootFolder), "fr-FR", "Bonjour from StartupResourcesAtRootFolder Bonjour from Test in root folder Bonjour from Customer in Models folder"); } + + private async Task RunTest(Type startupType, string culture, string expected) + { + var webHostBuilder = new WebHostBuilder().UseStartup(startupType); + var testHost = new TestServer(webHostBuilder); + + var client = testHost.CreateClient(); + var request = new HttpRequestMessage(); + var cookieValue = $"c={culture}|uic={culture}"; + request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}"); + + var response = await client.SendAsync(request); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Contains(expected, await response.Content.ReadAsStringAsync()); + } } } diff --git a/test/Microsoft.AspNetCore.Localization.FunctionalTests/Microsoft.AspNetCore.Localization.FunctionalTests.csproj b/test/Microsoft.AspNetCore.Localization.FunctionalTests/Microsoft.AspNetCore.Localization.FunctionalTests.csproj index 58eda69570..c5be3d0ee5 100644 --- a/test/Microsoft.AspNetCore.Localization.FunctionalTests/Microsoft.AspNetCore.Localization.FunctionalTests.csproj +++ b/test/Microsoft.AspNetCore.Localization.FunctionalTests/Microsoft.AspNetCore.Localization.FunctionalTests.csproj @@ -8,11 +8,13 @@
+ + - + diff --git a/test/Microsoft.AspNetCore.Localization.FunctionalTests/Properties/AssemblyInfo.cs b/test/Microsoft.AspNetCore.Localization.FunctionalTests/Properties/AssemblyInfo.cs deleted file mode 100644 index b1fa884228..0000000000 --- a/test/Microsoft.AspNetCore.Localization.FunctionalTests/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,3 +0,0 @@ -using Xunit; - -[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)] \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Localization.FunctionalTests/TestRunner.cs b/test/Microsoft.AspNetCore.Localization.FunctionalTests/TestRunner.cs deleted file mode 100644 index 0c1dee8c7d..0000000000 --- a/test/Microsoft.AspNetCore.Localization.FunctionalTests/TestRunner.cs +++ /dev/null @@ -1,109 +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.IO; -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Server.IntegrationTesting; -using Microsoft.Extensions.Logging; -using Xunit; - -namespace Microsoft.AspNetCore.Localization.FunctionalTests -{ - public class TestRunner - { - private const string ApplicationBasePath = "http://localhost:0"; - - private string _applicationPath; - - public TestRunner(string applicationPath) - { - _applicationPath = Path.Combine(ResolveRootFolder(AppContext.BaseDirectory), applicationPath); - } - - private static string ResolveRootFolder(string projectFolder) - { - var di = new DirectoryInfo(projectFolder); - - while (di.Parent != null) - { - var globalJsonPath = Path.Combine(di.FullName, "version.xml"); - - if (File.Exists(globalJsonPath)) - { - return di.FullName; - } - - di = di.Parent; - } - - // If we don't find any files then make the project folder the root - return projectFolder; - } - - private async Task RunTestAndGetResponse( - RuntimeArchitecture runtimeArchitecture, - string environmentName, - string locale) - { - var loggerFactory = new LoggerFactory(); - - var deploymentParameters = new DeploymentParameters(_applicationPath, ServerType.Kestrel, RuntimeFlavor.CoreClr, runtimeArchitecture) - { - ApplicationBaseUriHint = ApplicationBasePath, - EnvironmentName = environmentName, - TargetFramework = "netcoreapp2.0" - }; - - using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory)) - { - var deploymentResult = await deployer.DeployAsync(); - - var cookie = new Cookie(CookieRequestCultureProvider.DefaultCookieName, "c=" + locale + "|uic=" + locale); - var cookieContainer = new CookieContainer(); - cookieContainer.Add(new Uri(deploymentResult.ApplicationBaseUri), cookie); - - var httpClientHandler = new HttpClientHandler(); - httpClientHandler.CookieContainer = cookieContainer; - - using (var httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) }) - { - var logger = loggerFactory.CreateLogger(string.Format("Localization Test Site:{0}:{1}", ServerType.Kestrel, runtimeArchitecture)); - - // Request to base address and check if various parts of the body are rendered & measure the cold startup time. - var response = await RetryHelper.RetryRequest(() => - { - return httpClient.GetAsync(string.Empty); - }, logger, deploymentResult.HostShutdownToken); - - return await response.Content.ReadAsStringAsync(); - } - } - } - - public async Task RunTestAndVerifyResponse( - RuntimeArchitecture runtimeArchitecture, - string environmentName, - string locale, - string expectedText) - { - var responseText = await RunTestAndGetResponse(runtimeArchitecture, environmentName, locale); - Console.WriteLine("Response Text " + responseText); - Assert.Equal(expectedText, responseText); - } - - public async Task RunTestAndVerifyResponseHeading( - RuntimeArchitecture runtimeArchitecture, - string environmentName, - string locale, - string expectedHeadingText) - { - var responseText = await RunTestAndGetResponse(runtimeArchitecture, environmentName, locale); - var headingIndex = responseText.IndexOf(expectedHeadingText); - Console.WriteLine("Response Header " + responseText); - Assert.True(headingIndex >= 0); - } - } -} \ No newline at end of file