mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
fix: makes the set language endpoint work without a return url
This commit is contained in:
1 parent
d10233f4e3
commit
c4135dbec9
3 files changed
+27
-29
No files matched your search
+10
-21
@@ -2,7 +2,6 @@
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Requests.Tools;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Common.Services;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -20,17 +19,6 @@ namespace Lumina.Presentation.Web.Core.Endpoints.Tools.Language.SetLanguage;
|
||||
/// </summary>
|
||||
public class SetLanguageEndpoint : BaseEndpoint<SetLanguageRequest, IResult>
|
||||
{
|
||||
private readonly IUrlService _urlService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SetLanguageEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="urlService">Injected service for generating URLs from action and controller names, with localization.</param>
|
||||
public SetLanguageEndpoint(IUrlService urlService)
|
||||
{
|
||||
_urlService = urlService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the endpoint.
|
||||
/// </summary>
|
||||
@@ -62,24 +50,25 @@ public class SetLanguageEndpoint : BaseEndpoint<SetLanguageRequest, IResult>
|
||||
Path = "/" // ensure the cookie works across all paths
|
||||
}
|
||||
);
|
||||
// ensure the return URL includes the correct culture
|
||||
string returnUrl = string.IsNullOrEmpty(request.ReturnUrl) ? _urlService.GetAbsoluteUrl(WebRoutes.Home.INDEX_CULTURED)! : request.ReturnUrl;
|
||||
// handle the culture replacement in the return URL
|
||||
// determine the URL to return to, defaulting to the localized home page when no return URL is supplied
|
||||
string newCulture = request.NewCulture!;
|
||||
string returnUrl = string.IsNullOrEmpty(request.ReturnUrl) ? $"/{newCulture.ToLower()}" : request.ReturnUrl;
|
||||
string currentCulture = Culture;
|
||||
string culturePath = $"/{currentCulture.ToLower()}/";
|
||||
string newCulturePath = $"/{request.NewCulture!.ToLower()}/";
|
||||
|
||||
// handle the base path if present
|
||||
// handle the base path, so that the culture replacement matches the URL
|
||||
if (!string.IsNullOrEmpty(HttpContext.Request.PathBase))
|
||||
returnUrl = returnUrl.Replace(HttpContext.Request.PathBase.Value!, string.Empty, StringComparison.OrdinalIgnoreCase);
|
||||
// replace the culture in the URL
|
||||
returnUrl = returnUrl.Replace(culturePath, newCulturePath, StringComparison.OrdinalIgnoreCase);
|
||||
// replace the current culture segment of the URL with the new one, whether or not a trailing slash is present
|
||||
string currentCultureSegment = $"/{currentCulture.ToLower()}";
|
||||
string newCultureSegment = $"/{newCulture.ToLower()}";
|
||||
if (returnUrl.StartsWith(currentCultureSegment, StringComparison.OrdinalIgnoreCase))
|
||||
returnUrl = newCultureSegment + returnUrl[currentCultureSegment.Length..];
|
||||
|
||||
// re-apply the base path if present
|
||||
if (!string.IsNullOrEmpty(HttpContext.Request.PathBase))
|
||||
returnUrl = HttpContext.Request.PathBase.Value + returnUrl;
|
||||
|
||||
// redirect to the original page
|
||||
// redirect to the target page
|
||||
return Results.LocalRedirect(returnUrl);
|
||||
}
|
||||
}
|
||||
+15
@@ -45,6 +45,21 @@ public class SetLanguageEndpointTests : IClassFixture<LuminaWebFactory>
|
||||
Assert.Contains("de-DE", response.Headers.GetValues("Set-Cookie").First());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetLanguage_WhenCalledByAuthenticatedUserWithoutReturnUrl_ShouldRedirectToHomePageWithNewCulture()
|
||||
{
|
||||
// Arrange
|
||||
AuthenticatedWebClient webClient = await WebTestHelpers.CreateAuthenticatedClientAsync(_apiFactory);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await webClient.Client.GetAsync("/en-us/tools/language/set-language?newCulture=de-DE");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
|
||||
Assert.Equal("/de-de", response.Headers.Location!.ToString());
|
||||
Assert.Contains("de-DE", response.Headers.GetValues("Set-Cookie").First());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetLanguage_WhenCalledWithoutAuthentication_ShouldRedirectToLogin()
|
||||
{
|
||||
|
||||
+2
-8
@@ -1,14 +1,11 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FastEndpoints;
|
||||
using Lumina.Presentation.Web.Common.Requests.Tools;
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Common.Services;
|
||||
using Lumina.Presentation.Web.Core.Endpoints.Tools.Language.SetLanguage;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using NSubstitute;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -22,7 +19,6 @@ namespace Lumina.Presentation.Web.UnitTests.Core.Endpoints.Tools.Language.SetLan
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class SetLanguageEndpointTests
|
||||
{
|
||||
private readonly IUrlService _mockUrlService;
|
||||
private readonly SetLanguageEndpoint _sut;
|
||||
|
||||
/// <summary>
|
||||
@@ -30,8 +26,7 @@ public class SetLanguageEndpointTests
|
||||
/// </summary>
|
||||
public SetLanguageEndpointTests()
|
||||
{
|
||||
_mockUrlService = Substitute.For<IUrlService>();
|
||||
_sut = Factory.Create<SetLanguageEndpoint>(_mockUrlService);
|
||||
_sut = Factory.Create<SetLanguageEndpoint>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -39,14 +34,13 @@ public class SetLanguageEndpointTests
|
||||
{
|
||||
// Arrange
|
||||
TestHttpContextFactory.ConfigureCulture(_sut.HttpContext, "en-us");
|
||||
_mockUrlService.GetAbsoluteUrl(WebRoutes.Home.INDEX_CULTURED).Returns("http://localhost/en-us/");
|
||||
|
||||
// Act
|
||||
IResult result = await _sut.ExecuteAsync(new SetLanguageRequest("de-DE", null), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
RedirectHttpResult redirectResult = Assert.IsType<RedirectHttpResult>(result);
|
||||
Assert.Equal("http://localhost/de-de/", redirectResult.Url);
|
||||
Assert.Equal("/de-de", redirectResult.Url);
|
||||
Assert.True(redirectResult.AcceptLocalUrlOnly);
|
||||
string setCookieHeader = _sut.HttpContext.Response.Headers.SetCookie.ToString();
|
||||
Assert.Contains(CookieRequestCultureProvider.DefaultCookieName, setCookieHeader);
|
||||
|
||||
Reference in new issue
Block a user