mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +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 FastEndpoints;
|
||||||
using Lumina.Presentation.Web.Common.Requests.Tools;
|
using Lumina.Presentation.Web.Common.Requests.Tools;
|
||||||
using Lumina.Presentation.Web.Common.Routes;
|
using Lumina.Presentation.Web.Common.Routes;
|
||||||
using Lumina.Presentation.Web.Common.Services;
|
|
||||||
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
using Lumina.Presentation.Web.Core.Endpoints.Common;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@@ -20,17 +19,6 @@ namespace Lumina.Presentation.Web.Core.Endpoints.Tools.Language.SetLanguage;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SetLanguageEndpoint : BaseEndpoint<SetLanguageRequest, IResult>
|
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>
|
/// <summary>
|
||||||
/// Configures the endpoint.
|
/// Configures the endpoint.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -62,24 +50,25 @@ public class SetLanguageEndpoint : BaseEndpoint<SetLanguageRequest, IResult>
|
|||||||
Path = "/" // ensure the cookie works across all paths
|
Path = "/" // ensure the cookie works across all paths
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
// ensure the return URL includes the correct culture
|
// determine the URL to return to, defaulting to the localized home page when no return URL is supplied
|
||||||
string returnUrl = string.IsNullOrEmpty(request.ReturnUrl) ? _urlService.GetAbsoluteUrl(WebRoutes.Home.INDEX_CULTURED)! : request.ReturnUrl;
|
string newCulture = request.NewCulture!;
|
||||||
// handle the culture replacement in the return URL
|
string returnUrl = string.IsNullOrEmpty(request.ReturnUrl) ? $"/{newCulture.ToLower()}" : request.ReturnUrl;
|
||||||
string currentCulture = Culture;
|
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))
|
if (!string.IsNullOrEmpty(HttpContext.Request.PathBase))
|
||||||
returnUrl = returnUrl.Replace(HttpContext.Request.PathBase.Value!, string.Empty, StringComparison.OrdinalIgnoreCase);
|
returnUrl = returnUrl.Replace(HttpContext.Request.PathBase.Value!, string.Empty, StringComparison.OrdinalIgnoreCase);
|
||||||
// replace the culture in the URL
|
// replace the current culture segment of the URL with the new one, whether or not a trailing slash is present
|
||||||
returnUrl = returnUrl.Replace(culturePath, newCulturePath, StringComparison.OrdinalIgnoreCase);
|
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
|
// re-apply the base path if present
|
||||||
if (!string.IsNullOrEmpty(HttpContext.Request.PathBase))
|
if (!string.IsNullOrEmpty(HttpContext.Request.PathBase))
|
||||||
returnUrl = HttpContext.Request.PathBase.Value + returnUrl;
|
returnUrl = HttpContext.Request.PathBase.Value + returnUrl;
|
||||||
|
|
||||||
// redirect to the original page
|
// redirect to the target page
|
||||||
return Results.LocalRedirect(returnUrl);
|
return Results.LocalRedirect(returnUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+15
@@ -45,6 +45,21 @@ public class SetLanguageEndpointTests : IClassFixture<LuminaWebFactory>
|
|||||||
Assert.Contains("de-DE", response.Headers.GetValues("Set-Cookie").First());
|
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]
|
[Fact]
|
||||||
public async Task SetLanguage_WhenCalledWithoutAuthentication_ShouldRedirectToLogin()
|
public async Task SetLanguage_WhenCalledWithoutAuthentication_ShouldRedirectToLogin()
|
||||||
{
|
{
|
||||||
|
|||||||
+2
-8
@@ -1,14 +1,11 @@
|
|||||||
#region ========================================================================= USING =====================================================================================
|
#region ========================================================================= USING =====================================================================================
|
||||||
using FastEndpoints;
|
using FastEndpoints;
|
||||||
using Lumina.Presentation.Web.Common.Requests.Tools;
|
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.Core.Endpoints.Tools.Language.SetLanguage;
|
||||||
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
using Microsoft.AspNetCore.Localization;
|
using Microsoft.AspNetCore.Localization;
|
||||||
using NSubstitute;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -22,7 +19,6 @@ namespace Lumina.Presentation.Web.UnitTests.Core.Endpoints.Tools.Language.SetLan
|
|||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public class SetLanguageEndpointTests
|
public class SetLanguageEndpointTests
|
||||||
{
|
{
|
||||||
private readonly IUrlService _mockUrlService;
|
|
||||||
private readonly SetLanguageEndpoint _sut;
|
private readonly SetLanguageEndpoint _sut;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -30,8 +26,7 @@ public class SetLanguageEndpointTests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public SetLanguageEndpointTests()
|
public SetLanguageEndpointTests()
|
||||||
{
|
{
|
||||||
_mockUrlService = Substitute.For<IUrlService>();
|
_sut = Factory.Create<SetLanguageEndpoint>();
|
||||||
_sut = Factory.Create<SetLanguageEndpoint>(_mockUrlService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -39,14 +34,13 @@ public class SetLanguageEndpointTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
TestHttpContextFactory.ConfigureCulture(_sut.HttpContext, "en-us");
|
TestHttpContextFactory.ConfigureCulture(_sut.HttpContext, "en-us");
|
||||||
_mockUrlService.GetAbsoluteUrl(WebRoutes.Home.INDEX_CULTURED).Returns("http://localhost/en-us/");
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
IResult result = await _sut.ExecuteAsync(new SetLanguageRequest("de-DE", null), CancellationToken.None);
|
IResult result = await _sut.ExecuteAsync(new SetLanguageRequest("de-DE", null), CancellationToken.None);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
RedirectHttpResult redirectResult = Assert.IsType<RedirectHttpResult>(result);
|
RedirectHttpResult redirectResult = Assert.IsType<RedirectHttpResult>(result);
|
||||||
Assert.Equal("http://localhost/de-de/", redirectResult.Url);
|
Assert.Equal("/de-de", redirectResult.Url);
|
||||||
Assert.True(redirectResult.AcceptLocalUrlOnly);
|
Assert.True(redirectResult.AcceptLocalUrlOnly);
|
||||||
string setCookieHeader = _sut.HttpContext.Response.Headers.SetCookie.ToString();
|
string setCookieHeader = _sut.HttpContext.Response.Headers.SetCookie.ToString();
|
||||||
Assert.Contains(CookieRequestCultureProvider.DefaultCookieName, setCookieHeader);
|
Assert.Contains(CookieRequestCultureProvider.DefaultCookieName, setCookieHeader);
|
||||||
|
|||||||
Reference in new issue
Block a user