mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Implements tests for commit e6fa85f7c6
Covers the root relative URL generation of UrlService with unit tests, which had no coverage since the method was added, and keeps the absolute URL coverage over the shared test HTTP context factory.
This commit is contained in:
1 parent
c0737dbfe8
commit
cf3855ec12
1 file changed
+129
-12
+129
-12
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Routes;
|
||||
using Lumina.Presentation.Web.Common.Services;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
@@ -34,7 +35,8 @@ public class UrlServiceTests
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => _sut.GetAbsoluteUrl("/not-a-real-route"));
|
||||
|
||||
// Assert
|
||||
Assert.Contains("not defined in the WebRoutes constants", exception.Message);
|
||||
Assert.Equal("routeTemplate", exception.ParamName);
|
||||
Assert.Contains("The route template '/not-a-real-route' is not defined in the WebRoutes constants.", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -54,7 +56,9 @@ public class UrlServiceTests
|
||||
public void GetAbsoluteUrl_WhenCalled_ShouldReplaceCulturePlaceholderAndBuildAbsoluteUrl()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = CreateHttpContext(culture: "en-us", pathBase: "", host: "localhost:5012", scheme: "http");
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "en-us");
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "http";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
|
||||
// Act
|
||||
@@ -68,7 +72,9 @@ public class UrlServiceTests
|
||||
public void GetAbsoluteUrl_WhenCalledWithAdditionalRouteValues_ShouldReplacePlaceholders()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = CreateHttpContext(culture: "en-us", pathBase: "", host: "localhost:5012", scheme: "http");
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "en-us");
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "http";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
@@ -83,7 +89,10 @@ public class UrlServiceTests
|
||||
public void GetAbsoluteUrl_WhenPathBaseIsPresent_ShouldIncludePathBase()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = CreateHttpContext(culture: "de-de", pathBase: "/lumina", host: "localhost:5012", scheme: "https");
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "de-de");
|
||||
httpContext.Request.PathBase = "/lumina";
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "https";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
|
||||
// Act
|
||||
@@ -97,7 +106,9 @@ public class UrlServiceTests
|
||||
public void GetAbsoluteUrl_WhenRouteTemplateDoesNotStartWithSlash_ShouldPrependSlash()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = CreateHttpContext(culture: "en-us", pathBase: "", host: "localhost:5012", scheme: "http");
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "en-us");
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "http";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
|
||||
// Act
|
||||
@@ -107,13 +118,119 @@ public class UrlServiceTests
|
||||
Assert.Equal("http://localhost:5012/file-system/api-get-type", url);
|
||||
}
|
||||
|
||||
private static DefaultHttpContext CreateHttpContext(string culture, string pathBase, string host, string scheme)
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenRouteTemplateIsNotDefined_ShouldThrowArgumentException()
|
||||
{
|
||||
DefaultHttpContext httpContext = new();
|
||||
httpContext.Request.RouteValues["culture"] = culture;
|
||||
httpContext.Request.PathBase = pathBase;
|
||||
httpContext.Request.Host = new HostString(host);
|
||||
httpContext.Request.Scheme = scheme;
|
||||
return httpContext;
|
||||
// Act
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => _sut.GetRelativeUrl("/not-a-real-route"));
|
||||
|
||||
// Assert
|
||||
Assert.Equal("routeTemplate", exception.ParamName);
|
||||
Assert.Contains("The route template '/not-a-real-route' is not defined in the WebRoutes constants.", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenHttpContextIsNull_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns((HttpContext?)null);
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN);
|
||||
|
||||
// Assert
|
||||
Assert.Null(url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenCalled_ShouldReturnCultureLocalizedPathWithoutSchemeOrHost()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "en-us");
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/en-us/auth/api-login", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenCultureRouteValueIsAbsent_ShouldFallBackToDefaultCulture()
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: null));
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.LOGIN);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/en-US/auth/api-login", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenCalledWithAdditionalRouteValues_ShouldReplacePlaceholders()
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us"));
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.LibraryManagement.DELETE_LIBRARY, new { id });
|
||||
|
||||
// Assert
|
||||
Assert.Equal($"/en-us/libraries/manage/api-item/{id}", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenRouteTemplateDoesNotStartWithSlash_ShouldPrependSlash()
|
||||
{
|
||||
// Arrange
|
||||
_mockHttpContextAccessor.HttpContext.Returns(TestHttpContextFactory.Create(culture: "en-us"));
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.FileSystem.GET_TYPE);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/file-system/api-get-type", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRelativeUrl_WhenPathBaseIsPresent_ShouldIncludePathBaseWithoutSchemeOrHost()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "de-de");
|
||||
httpContext.Request.PathBase = "/lumina";
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "https";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
|
||||
// Act
|
||||
string? url = _sut.GetRelativeUrl(WebRoutes.Authentication.REGISTER_VIEW);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("/lumina/de-de/auth/register", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAbsoluteUrl_WhenCalled_ShouldComposeSchemeAndHostWithRelativeUrl()
|
||||
{
|
||||
// Arrange
|
||||
DefaultHttpContext httpContext = TestHttpContextFactory.Create(culture: "de-de");
|
||||
httpContext.Request.PathBase = "/lumina";
|
||||
httpContext.Request.Host = new HostString("localhost:5012");
|
||||
httpContext.Request.Scheme = "https";
|
||||
_mockHttpContextAccessor.HttpContext.Returns(httpContext);
|
||||
Guid id = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
string? relativeUrl = _sut.GetRelativeUrl(WebRoutes.LibraryManagement.DELETE_LIBRARY, new { id });
|
||||
string? absoluteUrl = _sut.GetAbsoluteUrl(WebRoutes.LibraryManagement.DELETE_LIBRARY, new { id });
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(relativeUrl);
|
||||
Assert.Equal($"https://localhost:5012{relativeUrl}", absoluteUrl);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user