Skip to content

Commit

Permalink
Fix application root absolute path
Browse files Browse the repository at this point in the history
* test: add functional test for LocalizationSample

* fixed path for JsonStringSerializer

* test: add more functional tests, LocalizatinSample.Mvc

* Remove unnecessary functional test project

* Fix culture querystring

* Add cookie to HttpClient header

* Revert JsonStringLocalizer changes

* Revert LocalizationSample/Program changes

Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
  • Loading branch information
shahabganji and hishamco committed Apr 3, 2020
1 parent 17fe21a commit b14339e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 11 deletions.
9 changes: 8 additions & 1 deletion My.Extensions.Localization.Json.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LocalizationSample", "sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LocalizationSample.Resources", "samples\LocalizationSample.Resources\LocalizationSample.Resources.csproj", "{9789DDC4-6CC1-4A77-9883-D745BCAB524F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalizationSample.Mvc", "samples\LocalizationSample.Mvc\LocalizationSample.Mvc.csproj", "{92B0128E-57BA-4D6B-A2D9-26B54686F7D9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LocalizationSample.Mvc", "samples\LocalizationSample.Mvc\LocalizationSample.Mvc.csproj", "{92B0128E-57BA-4D6B-A2D9-26B54686F7D9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "My.Extensions.Localization.Json.FunctionalTests", "test\My.Extensions.Localization.Json.FunctionalTests\My.Extensions.Localization.Json.FunctionalTests.csproj", "{DC858972-8F2F-4E79-A455-154D58EC47AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -45,6 +47,10 @@ Global
{92B0128E-57BA-4D6B-A2D9-26B54686F7D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92B0128E-57BA-4D6B-A2D9-26B54686F7D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92B0128E-57BA-4D6B-A2D9-26B54686F7D9}.Release|Any CPU.Build.0 = Release|Any CPU
{DC858972-8F2F-4E79-A455-154D58EC47AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC858972-8F2F-4E79-A455-154D58EC47AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC858972-8F2F-4E79-A455-154D58EC47AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC858972-8F2F-4E79-A455-154D58EC47AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -55,6 +61,7 @@ Global
{4732D5B3-AEB5-4AF4-8679-7EBB4B2FBF95} = {5D3D85C6-7B3B-4D7E-925F-33B19AE15D76}
{9789DDC4-6CC1-4A77-9883-D745BCAB524F} = {5D3D85C6-7B3B-4D7E-925F-33B19AE15D76}
{92B0128E-57BA-4D6B-A2D9-26B54686F7D9} = {5D3D85C6-7B3B-4D7E-925F-33B19AE15D76}
{DC858972-8F2F-4E79-A455-154D58EC47AE} = {9FF90A72-EC14-4C15-921F-DB7AE3D1D34C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D173946A-BDF7-44A8-9740-2156FA7EC074}
Expand Down
10 changes: 1 addition & 9 deletions src/My.Extensions.Localization.Json/Internal/PathHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace My.Extensions.Localization.Json.Internal
{
public static class PathHelpers
{
// http://codebuckets.com/2017/10/19/getting-the-root-directory-path-for-net-core-applications/
public static string GetApplicationRoot()
{
var exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var appPathMatcher = new Regex(@"(?<!fil)[A-Za-z]:\\+[\S\s]*?(?=\\+bin)");
var appRoot = appPathMatcher.Match(exePath).Value;

return appRoot;
}
=> Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
}
1 change: 1 addition & 0 deletions src/My.Extensions.Localization.Json/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ private void BuildResourcesCache(string culture)
: $"{_resourceName}.{culture}.json";
_searchedLocation = Path.Combine(_resourcesPath, resourceFile);
IEnumerable<KeyValuePair<string, string>> value = null;
if (File.Exists(_searchedLocation))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;

namespace LocalizationSample.Mvc.FunctionalTest
{
public class LocalizationMvcSampleTests : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly HttpClient _client;

public LocalizationMvcSampleTests(WebApplicationFactory<Startup> factory)
{
_client = factory.CreateClient();
}

[Theory]
[InlineData("en-US", "Privacy Policy")]
[InlineData("fr-FR", "Politique de confidentialité")]
public async Task LocalizePrivacyView(string culture, string expected)
{
// Arrange
var url = "/Home/Privacy";
var request = new HttpRequestMessage(HttpMethod.Get, url);
var cookieValue = $"c={culture}|uic={culture}";
request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}");

// Act
var response = await _client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains(expected, content);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\samples\LocalizationSample.Mvc\LocalizationSample.Mvc.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -18,4 +17,19 @@
<ProjectReference Include="..\..\src\My.Extensions.Localization.Json\My.Extensions.Localization.Json.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\fr-FR.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Test.ar.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Test.fr-FR.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Test.fr.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit b14339e

Please sign in to comment.