Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Routing/UriUtilityTests.cs
Nikolaj Geisle 7aeb400fce V10: fix build warnings in test projects (#12509)
* Run code cleanup

* Dotnet format benchmarks project

* Fix up Test.Common

* Run dotnet format + manual cleanup

* Run code cleanup for unit tests

* Run dotnet format

* Fix up errors

* Manual cleanup of Unit test project

* Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix according to review

* Fix after merge

* Fix errors

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
Co-authored-by: Zeegaan <nge@umbraco.dk>
2022-06-21 08:09:38 +02:00

114 lines
4.0 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Routing;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Routing;
// FIXME: not testing virtual directory!
[TestFixture]
public class UriUtilityTests
{
// test normal urls
[TestCase("http://LocalHost/", "http://localhost/")]
[TestCase("http://LocalHost/?x=y", "http://localhost/?x=y")]
[TestCase("http://LocalHost/Home", "http://localhost/home")]
[TestCase("http://LocalHost/Home?x=y", "http://localhost/home?x=y")]
[TestCase("http://LocalHost/Home/Sub1", "http://localhost/home/sub1")]
[TestCase("http://LocalHost/Home/Sub1?x=y", "http://localhost/home/sub1?x=y")]
// test that the trailing slash goes but not on hostname
[TestCase("http://LocalHost/", "http://localhost/")]
[TestCase("http://LocalHost/Home/", "http://localhost/home")]
[TestCase("http://LocalHost/Home/?x=y", "http://localhost/home?x=y")]
[TestCase("http://LocalHost/Home/Sub1/", "http://localhost/home/sub1")]
[TestCase("http://LocalHost/Home/Sub1/?x=y", "http://localhost/home/sub1?x=y")]
public void Uri_To_Umbraco(string sourceUrl, string expectedUrl)
{
// Arrange
var sourceUri = new Uri(sourceUrl);
var uriUtility = BuildUriUtility("/");
// Act
var resultUri = uriUtility.UriToUmbraco(sourceUri);
// Assert
var expectedUri = new Uri(expectedUrl);
Assert.AreEqual(expectedUri.ToString(), resultUri.ToString());
}
// test directoryUrl true, trailingSlash false
[TestCase("/", "/", false)]
[TestCase("/home", "/home", false)]
[TestCase("/home/sub1", "/home/sub1", false)]
// test directoryUrl true, trailingSlash true
[TestCase("/", "/", true)]
[TestCase("/home", "/home/", true)]
[TestCase("/home/sub1", "/home/sub1/", true)]
public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool trailingSlash)
{
// Arrange
var sourceUri = new Uri(sourceUrl, UriKind.Relative);
var requestHandlerSettings = new RequestHandlerSettings { AddTrailingSlash = trailingSlash };
var uriUtility = BuildUriUtility("/");
// Act
var resultUri = uriUtility.UriFromUmbraco(sourceUri, requestHandlerSettings);
// Assert
var expectedUri = new Uri(expectedUrl, UriKind.Relative);
Assert.AreEqual(expectedUri.ToString(), resultUri.ToString());
}
[TestCase("/", "/", "/")]
[TestCase("/", "/foo", "/foo")]
[TestCase("/", "~/foo", "/foo")]
[TestCase("/vdir", "/", "/vdir/")]
[TestCase("/vdir", "/foo", "/vdir/foo")]
[TestCase("/vdir", "/foo/", "/vdir/foo/")]
[TestCase("/vdir", "~/foo", "/vdir/foo")]
public void Uri_To_Absolute(string virtualPath, string sourceUrl, string expectedUrl)
{
// Arrange
var uriUtility = BuildUriUtility(virtualPath);
// Act
var resultUrl = uriUtility.ToAbsolute(sourceUrl);
// Assert
Assert.AreEqual(expectedUrl, resultUrl);
}
[TestCase("/", "/", "/")]
[TestCase("/", "/foo", "/foo")]
[TestCase("/", "/foo/", "/foo/")]
[TestCase("/vdir", "/vdir", "/")]
[TestCase("/vdir", "/vdir/", "/")]
[TestCase("/vdir", "/vdir/foo", "/foo")]
[TestCase("/vdir", "/vdir/foo/", "/foo/")]
public void Url_To_App_Relative(string virtualPath, string sourceUrl, string expectedUrl)
{
// Arrange
var uriUtility = BuildUriUtility(virtualPath);
// Act
var resultUrl = uriUtility.ToAppRelative(sourceUrl);
// Assert
Assert.AreEqual(expectedUrl, resultUrl);
}
private UriUtility BuildUriUtility(string virtualPath)
{
var mockHostingEnvironment = new Mock<IHostingEnvironment>();
mockHostingEnvironment.Setup(x => x.ApplicationVirtualPath).Returns(virtualPath);
return new UriUtility(mockHostingEnvironment.Object);
}
}