More unit tests

pull/3895/head
Jamie Rees 5 years ago
parent ad7b5af997
commit 781cc31380

@ -0,0 +1,28 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
namespace Ombi.Helpers.Tests
{
[TestFixture]
public class EmbyHelperTests
{
[TestCaseSource(nameof(UrlData))]
public string TestUrl(string mediaId, string url)
{
return EmbyHelper.GetEmbyMediaUrl(mediaId, url);
}
public static IEnumerable<TestCaseData> UrlData
{
get
{
var mediaId = 1;
yield return new TestCaseData(mediaId.ToString(), "http://google.com").Returns($"http://google.com/#!/itemdetails.html?id={mediaId}").SetName("EmbyHelper_GetMediaUrl_WithCustomDomain_WithoutTrailingSlash");
yield return new TestCaseData(mediaId.ToString(), "http://google.com/").Returns($"http://google.com/#!/itemdetails.html?id={mediaId}").SetName("EmbyHelper_GetMediaUrl_WithCustomDomain");
yield return new TestCaseData(mediaId.ToString(), "https://google.com/").Returns($"https://google.com/#!/itemdetails.html?id={mediaId}").SetName("EmbyHelper_GetMediaUrl_WithCustomDomain_Https");
}
}
}
}

@ -0,0 +1,115 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ombi.Helpers.Tests
{
[TestFixture]
public class UriHelperTests
{
[TestCaseSource(nameof(UrlData))]
public string ReturnUri(string uri)
{
return UriHelper.ReturnUri(uri).ToString();
}
public static IEnumerable<TestCaseData> UrlData
{
get
{
yield return new TestCaseData("https://google.com/").Returns($"https://google.com/").SetName("ReturnUri_With_HttpScheme");
yield return new TestCaseData("google.com/").Returns($"http://google.com/").SetName("ReturnUri_HttpScheme_Not_Provided");
yield return new TestCaseData("http://google.com:9090/").Returns($"http://google.com:9090/").SetName("ReturnUri_WithPort");
yield return new TestCaseData("https://google.com/").Returns($"https://google.com/").SetName("ReturnUri_With_HttpsScheme");
yield return new TestCaseData("https://hi.google.com/").Returns($"https://hi.google.com/").SetName("ReturnUri_With_SubDomain");
yield return new TestCaseData("https://google.com/hi").Returns($"https://google.com/hi").SetName("ReturnUri_With_Path");
yield return new TestCaseData("https://hi.google.com/hi").Returns($"https://hi.google.com/hi").SetName("ReturnUri_With_Path_And_SubDomain");
}
}
[TestCaseSource(nameof(UrlWithPortData))]
public string ReturnUriWithPort(string uri, int port)
{
return UriHelper.ReturnUri(uri, port).ToString();
}
public static IEnumerable<TestCaseData> UrlWithPortData
{
get
{
yield return new TestCaseData("https://google.com", 443).Returns($"https://google.com/").SetName("ReturnUri_With_HttpsPort");
yield return new TestCaseData("https://google.com/", 123).Returns($"https://google.com:123/").SetName("ReturnUri_With_HttpScheme_With_Port");
yield return new TestCaseData("google.com/", 80).Returns($"http://google.com/").SetName("ReturnUri_HttpScheme_Not_Provided_With_Port");
yield return new TestCaseData("https://google.com/", 7000).Returns($"https://google.com:7000/").SetName("ReturnUri_With_HttpsScheme_With_Port");
yield return new TestCaseData("https://hi.google.com/", 1).Returns($"https://hi.google.com:1/").SetName("ReturnUri_With_SubDomain_With_Port");
yield return new TestCaseData("https://google.com/hi", 443).Returns($"https://google.com/hi").SetName("ReturnUri_With_Path_With_Port");
yield return new TestCaseData("https://hi.google.com/hi", 443).Returns($"https://hi.google.com/hi").SetName("ReturnUri_With_Path_And_SubDomain_With_Port");
}
}
[TestCaseSource(nameof(UrlWithPortWithSSLData))]
public string ReturnUriWithPortAndSSL(string uri, int port, bool ssl)
{
return UriHelper.ReturnUri(uri, port, ssl).ToString();
}
public static IEnumerable<TestCaseData> UrlWithPortWithSSLData
{
get
{
foreach (var d in UrlWithPortData)
{
var expected = (string)d.ExpectedResult;
var args = d.Arguments.ToList();
args.Add(true);
var newExpected = expected.Replace("http://", "https://");
if (args.Contains(80))
{
newExpected = expected;
}
d.TestName += "_With_SSL";
yield return new TestCaseData(args.ToArray()).Returns(newExpected).SetName(d.TestName);
}
}
}
[TestCaseSource(nameof(UrlWithPortWithSSLDataCasing))]
public string ReturnUriWithPortAndSSLCasing(string uri, int port, bool ssl)
{
return UriHelper.ReturnUri(uri, port, ssl).ToString();
}
public static IEnumerable<TestCaseData> UrlWithPortWithSSLDataCasing
{
get
{
foreach (var d in UrlWithPortData)
{
if (d.TestName.Contains("_Path_"))
{
continue;
}
var expected = (string)d.ExpectedResult;
var args = d.Arguments.ToList();
for (int i = 0; i < args.Count; i++)
{
if(args[i] is string)
{
args[i] = ((string)args[i]).ToUpper();
}
}
args.Add(true);
var newExpected = expected.Replace("http://", "https://");
if (args.Contains(80))
{
newExpected = expected;
}
d.TestName += "_With_SSL_ToUpper";
yield return new TestCaseData(args.ToArray()).Returns(newExpected).SetName(d.TestName);
}
}
}
}
}

@ -1,46 +0,0 @@
using System;
using System.Security.Claims;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Ombi.Helpers
{
public class ClaimConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(System.Security.Claims.Claim));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var claim = (System.Security.Claims.Claim)value;
JObject jo = new JObject();
jo.Add("Type", claim.Type);
jo.Add("Value", IsJson(claim.Value) ? new JRaw(claim.Value) : new JValue(claim.Value));
jo.Add("ValueType", claim.ValueType);
jo.Add("Issuer", claim.Issuer);
jo.Add("OriginalIssuer", claim.OriginalIssuer);
jo.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
string type = (string)jo["Type"];
JToken token = jo["Value"];
string value = token.Type == JTokenType.String ? (string)token : token.ToString(Formatting.None);
string valueType = (string)jo["ValueType"];
string issuer = (string)jo["Issuer"];
string originalIssuer = (string)jo["OriginalIssuer"];
return new Claim(type, value, valueType, issuer, originalIssuer);
}
private bool IsJson(string val)
{
return (val != null &&
(val.StartsWith("[") && val.EndsWith("]")) ||
(val.StartsWith("{") && val.EndsWith("}")));
}
}
}

@ -1,9 +1,4 @@
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
namespace Ombi.Helpers
namespace Ombi.Helpers
{
public class EmbyHelper
{
@ -11,6 +6,10 @@ namespace Ombi.Helpers
{
if (customerServerUrl.HasValue())
{
if(!customerServerUrl.EndsWith("/"))
{
return $"{customerServerUrl}/#!/itemdetails.html?id={mediaId}";
}
return $"{customerServerUrl}#!/itemdetails.html?id={mediaId}";
}
else

@ -35,6 +35,11 @@ namespace Ombi.Helpers
}
else
{
if(val.EndsWith("/"))
{
// Remove a trailing slash, since the URIBuilder adds one
val = val.Remove(val.Length - 1, 1);
}
uri = new UriBuilder(Http, val);
}
@ -59,24 +64,34 @@ namespace Ombi.Helpers
}
var uri = new UriBuilder();
if (val.StartsWith("http://", StringComparison.Ordinal))
if (val.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))
{
var split = val.Split('/');
uri = split.Length >= 4 ? new UriBuilder(Http, split[2], port, "/" + split[3]) : new UriBuilder(new Uri($"{val}:{port}"));
}
else if (val.StartsWith("https://", StringComparison.Ordinal))
else if (val.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
{
var split = val.Split('/');
uri = split.Length >= 4
? new UriBuilder(Https, split[2], port, "/" + split[3])
: new UriBuilder(Https, split[2], port);
}
else if (ssl)
else if ((ssl || port == 443) && port != 80)
{
if (val.EndsWith("/"))
{
// Remove a trailing slash, since the URIBuilder adds one
val = val.Remove(val.Length - 1, 1);
}
uri = new UriBuilder(Https, val, port);
}
else
{
if (val.EndsWith("/"))
{
// Remove a trailing slash, since the URIBuilder adds one
val = val.Remove(val.Length - 1, 1);
}
uri = new UriBuilder(Http, val, port);
}

Loading…
Cancel
Save