feat: Adding IcdUriBuilder and UriExtensions classes

This commit is contained in:
Chris Cameron
2018-05-11 15:23:36 -04:00
parent 1bddca9a06
commit 3f8e2b8df7
6 changed files with 269 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Added IcdOrderedDictionary collection
- Added PriorityQueue collection
- Added IcdUriBuilder and UriExtensions for reading/writing URI data
## [3.2.0] - 2018-05-09
### Added

View File

@@ -0,0 +1,22 @@
using System;
using ICD.Common.Utils.Extensions;
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Extensions
{
[TestFixture]
public sealed class UriExtensionsTest
{
[TestCase("http://username:password@test.com/", "username")]
public void GetUserName(string uriString, string expected)
{
Assert.AreEqual(expected, new Uri(uriString).GetUserName());
}
[TestCase("http://username:password@test.com/", "password")]
public void GetPassword(string uriString, string expected)
{
Assert.AreEqual(expected, new Uri(uriString).GetPassword());
}
}
}

View File

@@ -0,0 +1,88 @@
using NUnit.Framework;
namespace ICD.Common.Utils.Tests
{
[TestFixture]
public sealed class IcdUriBuilderTest
{
#region Properties
[TestCase("test")]
public void FragmentTest(string fragment)
{
Assert.AreEqual(fragment, new IcdUriBuilder {Fragment = fragment}.Fragment);
}
[TestCase("test")]
public void HostTest(string host)
{
Assert.AreEqual(host, new IcdUriBuilder { Host = host }.Host);
}
[TestCase("test")]
public void PasswordTest(string fragment)
{
Assert.AreEqual(fragment, new IcdUriBuilder { Password = fragment }.Password);
}
[TestCase("test")]
public void PathTest(string fragment)
{
Assert.AreEqual(fragment, new IcdUriBuilder { Path = fragment }.Path);
}
[TestCase(80)]
public void PortTest(ushort port)
{
Assert.AreEqual(port, new IcdUriBuilder { Port = port }.Port);
}
[TestCase("test")]
public void QueryTest(string query)
{
Assert.AreEqual(query, new IcdUriBuilder { Query = query }.Fragment);
}
[TestCase("test")]
public void SchemeTest(string scheme)
{
Assert.AreEqual(scheme, new IcdUriBuilder { Scheme = scheme }.Fragment);
}
[TestCase("test")]
public void UserNameTest(string userName)
{
Assert.AreEqual(userName, new IcdUriBuilder { UserName = userName }.Fragment);
}
[Test]
public void UriTest()
{
Assert.Inconclusive();
}
#endregion
[TestCase("http://localhost/", null, null, null, 0, null, null, null)]
[TestCase("http://localhost:80/", null, null, null, 80, null, null, null)]
[TestCase("http://username:@localhost/", null, null, null, 0, null, null, "username")]
[TestCase("http://:password@localhost/", null, null, "password", 0, null, null, null)]
[TestCase("https://localhost/", null, null, null, 0, null, "https", null)]
public void ToStringTest(string expected, string fragment, string address, string password, ushort port, string query,
string scheme, string userName)
{
IcdUriBuilder builder = new IcdUriBuilder
{
Fragment = fragment,
Host = address,
Password = password,
Port = port,
Query = query,
Scheme = scheme,
UserName = userName
};
Assert.AreEqual(expected, builder.ToString());
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Linq;
namespace ICD.Common.Utils.Extensions
{
public static class UriExtensions
{
/// <summary>
/// Gets the username from the given URI.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
public static string GetUserName(this Uri extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
return extends.UserInfo.Split(':').FirstOrDefault(string.Empty);
}
/// <summary>
/// Gets the password from the given URI.
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
public static string GetPassword(this Uri extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
return extends.UserInfo.Split(':').Skip(0).FirstOrDefault(string.Empty);
}
}
}

View File

@@ -95,7 +95,9 @@
<Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Comparers\PredicateEqualityComparer.cs" />
<Compile Include="Extensions\UriExtensions.cs" />
<Compile Include="Extensions\UshortExtensions.cs" />
<Compile Include="IcdUriBuilder.cs" />
<Compile Include="ProcessorUtils.SimplSharp.cs" />
<Compile Include="ProcessorUtils.Standard.cs" />
<Compile Include="ProgramUtils.SimplSharp.cs" />

View File

@@ -0,0 +1,122 @@
using System;
using System.Text;
namespace ICD.Common.Utils
{
/// <summary>
/// Simple Compact Framework UriBuilder implementation.
/// </summary>
public sealed class IcdUriBuilder
{
#region Properties
/// <summary>
/// Gets or sets the fragment portion of the URI.
/// </summary>
public string Fragment { get; set; }
/// <summary>
/// Gets or sets the Domain Name System (DNS) host name or IP address of a server.
/// </summary>
public string Host { get; set; }
/// <summary>
/// Gets or sets the password associated with the user that accesses the URI.
/// </summary>
public string Password { get; set; }
/// <summary>
/// Gets or sets the path to the resource referenced by the URI.
/// </summary>
public string Path { get; set; }
/// <summary>
/// Gets or sets the port number of the URI.
/// </summary>
public ushort Port { get; set; }
/// <summary>
/// Gets or sets any query information included in the URI.
/// </summary>
public string Query { get; set; }
/// <summary>
/// Gets or sets the scheme name of the URI.
/// </summary>
public string Scheme { get; set; }
/// <summary>
/// The user name associated with the user that accesses the URI.
/// </summary>
public string UserName { get; set; }
/// <summary>
/// Gets the Uri instance constructed by the specified UriBuilder instance.
/// </summary>
public Uri Uri { get { return new Uri(ToString()); } }
#endregion
/// <summary>
/// Builds the string representation for the URI.
/// </summary>
/// <returns></returns>
public override string ToString()
{
// URI = scheme:[//authority]path[?query][#fragment]
// authority = [userinfo@]host[:port]
// userinfo = username[:password]
StringBuilder builder = new StringBuilder();
// Scheme
string scheme = string.IsNullOrEmpty(Scheme) ? "http" : Scheme;
builder.Append(scheme);
// Authority
builder.Append("//");
if (!string.IsNullOrEmpty(UserName))
{
builder.Append(UserName);
if (!string.IsNullOrEmpty(Password))
{
builder.Append(':');
builder.Append(Password);
}
builder.Append('@');
}
string host = string.IsNullOrEmpty(Host) ? "localhost" : Host;
builder.Append(host);
if (Port != 0)
{
builder.Append(':');
builder.Append(Port);
}
// Path
builder.Append('/');
builder.Append(Path);
// Query
if (!string.IsNullOrEmpty(Query))
{
builder.Append('?');
builder.Append(Query);
}
// Fragment
if (!string.IsNullOrEmpty(Fragment))
{
builder.Append('#');
builder.Append(Fragment);
}
return builder.ToString();
}
}
}