Files
Umbraco-CMS/src/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/UriExtensionsTests.cs

225 lines
12 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using Moq;
2016-11-07 19:12:09 +01:00
using NUnit.Framework;
using Umbraco.Core;
2020-08-24 10:52:48 +02:00
using Umbraco.Core.Configuration.Models;
using Umbraco.Web.Common.AspNetCore;
2016-11-07 19:12:09 +01:00
namespace Umbraco.Tests.UnitTests.Umbraco.Core.Extensions
2016-11-07 19:12:09 +01:00
{
[TestFixture]
public class UriExtensionsTests
{
[OneTimeSetUp]
public void Setup()
{
_hostEnvironment = Mock.Of<IWebHostEnvironment>();
2020-09-21 21:06:24 +02:00
_globalSettings = new GlobalSettings();
}
private IWebHostEnvironment _hostEnvironment;
2020-08-24 10:52:48 +02:00
private GlobalSettings _globalSettings;
2016-11-07 19:12:09 +01:00
[TestCase("http://www.domain.com/umbraco/preview/frame?id=1234", "", true)]
2016-11-07 19:12:09 +01:00
[TestCase("http://www.domain.com/umbraco", "", true)]
[TestCase("http://www.domain.com/Umbraco/", "", true)]
[TestCase("http://www.domain.com/umbraco/default.aspx", "", true)]
[TestCase("http://www.domain.com/umbraco/test/test", "", false)]
[TestCase("http://www.domain.com/umbraco/test/test/test", "", false)]
[TestCase("http://www.domain.com/Umbraco/test/test.aspx", "", true)]
[TestCase("http://www.domain.com/umbraco/test/test.js", "", true)]
[TestCase("http://www.domain.com/umbrac", "", false)]
[TestCase("http://www.domain.com/test", "", false)]
[TestCase("http://www.domain.com/test/umbraco", "", false)]
2016-11-07 19:12:09 +01:00
[TestCase("http://www.domain.com/Umbraco/Backoffice/blah", "", true)]
[TestCase("http://www.domain.com/Umbraco/anything", "", true)]
[TestCase("http://www.domain.com/Umbraco/anything/", "", true)]
[TestCase("http://www.domain.com/Umbraco/surface/blah", "", false)]
[TestCase("http://www.domain.com/umbraco/api/blah", "", false)]
[TestCase("http://www.domain.com/myvdir/umbraco/api/blah", "myvdir", false)]
[TestCase("http://www.domain.com/MyVdir/umbraco/api/blah", "/myvdir", false)]
[TestCase("http://www.domain.com/MyVdir/Umbraco/", "myvdir", true)]
2017-07-20 11:21:28 +02:00
[TestCase("http://www.domain.com/umbraco/test/legacyAjaxCalls.ashx?some=query&blah=js", "", true)]
2016-11-07 19:12:09 +01:00
public void Is_Back_Office_Request(string input, string virtualPath, bool expected)
{
var source = new Uri(input);
var hostingEnvironment = CreateHostingEnvironment(virtualPath);
Assert.AreEqual(expected, source.IsBackOfficeRequest(_globalSettings, hostingEnvironment));
2016-11-07 19:12:09 +01:00
}
[TestCase("http://www.domain.com/install", true)]
[TestCase("http://www.domain.com/Install/", true)]
[TestCase("http://www.domain.com/install/default.aspx", true)]
[TestCase("http://www.domain.com/install/test/test", true)]
[TestCase("http://www.domain.com/Install/test/test.aspx", true)]
[TestCase("http://www.domain.com/install/test/test.js", true)]
[TestCase("http://www.domain.com/instal", false)]
[TestCase("http://www.domain.com/umbraco", false)]
[TestCase("http://www.domain.com/umbraco/umbraco", false)]
2016-11-07 19:12:09 +01:00
public void Is_Installer_Request(string input, bool expected)
{
var source = new Uri(input);
var hostingEnvironment = CreateHostingEnvironment();
Assert.AreEqual(expected, source.IsInstallerRequest(hostingEnvironment));
2016-11-07 19:12:09 +01:00
}
private AspNetCoreHostingEnvironment CreateHostingEnvironment(string virtualPath = "")
{
2020-09-21 21:06:24 +02:00
var hostingSettings = new HostingSettings { ApplicationVirtualPath = virtualPath };
var mockedOptionsMonitorOfHostingSettings = Mock.Of<IOptionsMonitor<HostingSettings>>(x => x.CurrentValue == hostingSettings);
return new AspNetCoreHostingEnvironment(mockedOptionsMonitorOfHostingSettings, _hostEnvironment);
}
2016-11-07 19:12:09 +01:00
[TestCase("http://www.domain.com/foo/bar", "/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/foo/bar#hop", "/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/foo/bar?q=2#hop", "/", "http://www.domain.com/?q=2")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/foo?q=3", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")]
[TestCase("http://www.domain.com/foo#bang", "/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/foo?q=3#bang", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")]
public void RewritePath(string input, string path, string expected)
{
var source = new Uri(input);
var output = source.Rewrite(path);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/", "path/to/page/", typeof(ArgumentException))]
[TestCase("http://www.domain.com", "path/to/page/", typeof(ArgumentException))]
public void RewritePath_Exceptions(string input, string path, Type exception)
{
var source = new Uri(input);
Assert.Throws(exception, () =>
{
var output = source.Rewrite(path);
});
}
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/foo/bar", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
[TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")]
public void RewritePathAndQuery(string input, string path, string query, string expected)
{
var source = new Uri(input);
var output = source.Rewrite(path, query);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/", "path/to/page/", "", typeof(ArgumentException))]
[TestCase("http://www.domain.com/", "/path/to/page/", "x=27", typeof(ArgumentException))]
public void RewritePathAndQuery_Exceptions(string input, string path, string query, Type exception)
{
var source = new Uri(input);
Assert.Throws(exception, () =>
{
var output = source.Rewrite(path, query);
});
}
[TestCase("http://www.domain.com", "/")]
[TestCase("http://www.domain.com/", "/")]
[TestCase("http://www.domain.com/foo", "/foo")]
[TestCase("http://www.domain.com/foo/", "/foo/")]
[TestCase("http://www.domain.com/foo/bar", "/foo/bar")]
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar%20nix")]
[TestCase("http://www.domain.com/foo/bar?q=7#hop", "/foo/bar")]
[TestCase("/", "/")]
[TestCase("/foo", "/foo")]
[TestCase("/foo/", "/foo/")]
[TestCase("/foo/bar", "/foo/bar")]
[TestCase("/foo/bar?q=7#hop", "/foo/bar")]
[TestCase("/foo%20bar/pof", "/foo%20bar/pof")]
public void GetSafeAbsolutePath(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetSafeAbsolutePath();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")]
public void GetAbsolutePathDecoded(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetAbsolutePathDecoded();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")]
[TestCase("/foo%20bar/pof", "/foo bar/pof")]
public void GetSafeAbsolutePathDecoded(string input, string expected)
{
var source = new Uri(input, UriKind.RelativeOrAbsolute);
var output = source.GetSafeAbsolutePathDecoded();
Assert.AreEqual(expected, output);
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to/")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to/?q=3")]
public void EndPathWithSlash(string input, string expected)
{
var source = new Uri(input);
var output = source.EndPathWithSlash();
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to?q=3")]
public void TrimPathEndSlash(string input, string expected)
{
var source = new Uri(input);
var output = source.TrimPathEndSlash();
Assert.AreEqual(expected, output.ToString());
}
[TestCase("/foo/bar", "http://www.domain.com", "http://www.domain.com/foo/bar")]
[TestCase("/foo/bar", "http://www.domain.com/dang/dang", "http://www.domain.com/foo/bar")]
[TestCase("/", "http://www.domain.com/dang/dang", "http://www.domain.com/")]
[TestCase("/foo/bar", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar")]
[TestCase("/foo/bar?k=6#yop", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar?k=6")]
public void MakeAbsolute(string input, string reference, string expected)
{
var source = new Uri(input, UriKind.Relative);
var absolute = new Uri(reference);
var output = source.MakeAbsolute(absolute);
Assert.AreEqual(expected, output.ToString());
}
[TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com", "http://www.domain.com/")]
[TestCase("http://www.domain.com/", "http://www.domain.com/")]
[TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")]
[TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")]
[TestCase("http://www.domain.com:666/path/to/page", "http://www.domain.com/path/to/page")]
[TestCase("http://www.domain.com:666/path/to/page/", "http://www.domain.com/path/to/page/")]
[TestCase("http://www.domain.com:666", "http://www.domain.com/")]
[TestCase("http://www.domain.com:666/", "http://www.domain.com/")]
[TestCase("http://www.domain.com:666/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")]
[TestCase("http://www.domain.com:666/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")]
public void WithoutPort(string input, string expected)
{
var source = new Uri(input);
var output = source.WithoutPort();
Assert.AreEqual(expected, output.ToString());
}
}
}