Jackett/src/Jackett.Test/Common/Helpers/WebUtilityHelpersTests.cs

87 lines
3.9 KiB
C#
Raw Normal View History

using System.Text;
using System.Web;
using Jackett.Common.Helpers;
2020-10-31 21:11:02 +00:00
using NUnit.Framework;
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
2020-11-04 20:56:54 +00:00
namespace Jackett.Test.Common.Helpers
{
2020-10-31 21:11:02 +00:00
[TestFixture]
public class WebUtilityHelpersTests
{
private readonly Encoding[] _codePagesToTest;
private readonly string[] _stringsToTest;
public WebUtilityHelpersTests()
{
//https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
2021-05-16 18:13:54 +00:00
_codePagesToTest = new[]{
Encoding.UTF8,
Encoding.ASCII,
Encoding.GetEncoding("iso-8859-1"),
Encoding.GetEncoding("windows-1255"),
Encoding.GetEncoding("windows-1252"),
Encoding.GetEncoding("windows-1251") }
;
2021-05-16 18:13:54 +00:00
_stringsToTest = new[]
{
"Test! אני לא יודע עברית, אבל אני מאמין שזה טקסט חוקי! $ # 2 אני תוהה אם אמוג'י יהיה נתמך 🐀.",
"Å[ÉfÉBÉìÉOÇÕìÔǵÇ≠ǻǢ",
"J͖ͥͨ̑͂̄̈́ḁ̹ͧͦ͡ͅc̲̗̮͍̻͓ͤk̳̥̖͗ͭ̾͌e̖̲̟̽ț̠͕͈͓͎̱t͕͕͓̹̹ͫͧ̆͑ͤ͝ ̼͓̟̣͔̇̒T̻̺̙̣̘͔̤̅͒̈̈͛̅e̥̗̍͟s̖̬̭͈̠t̫̩͙̯̩ͣ̏̕ ̸̰̬̄̀ͧ̀S̨̻̼̜̹̼͓̺ͨ̍ͦt͇̻̺̂́̄͌͗̕r̥͈̙͙̰͈̙͗̆̽̀i͉͔̖̻̹̗̣̍ͭ̒͗n̴̻͔̹̘̱̳͈͐ͦ̃̽͐̓̂g̴͚͙̲ͩ͌̆̉̀̾"
};
}
2020-10-31 21:11:02 +00:00
[Test]
public void WebUtilityHelpers_UrlEncode_CorrectlyEncodes()
{
foreach (var encoding in _codePagesToTest)
{
foreach (var testString in _stringsToTest)
{
//Check our implementation of Decode in .NET Standard Matches the .NET Framework Version
2020-10-31 21:11:02 +00:00
var netString = HttpUtility.UrlEncode(testString, encoding);
var webUtilityString = WebUtilityHelpers.UrlEncode(testString, encoding);
//Of note is that percent encoding gives lowercase values, where NET Native uses upper case this should be okay according to RFC3986 (https://tools.ietf.org/html/rfc3986#section-2.1)
2020-10-31 21:11:02 +00:00
var netDecode = HttpUtility.UrlDecode(netString);
var webUtilityDecode = HttpUtility.UrlDecode(webUtilityString);
2020-10-31 21:11:02 +00:00
Assert.AreEqual(netDecode, webUtilityDecode, $"{testString} did not match the expected decoded string with {encoding.EncodingName})");
}
}
}
2020-11-04 20:56:54 +00:00
[Test]
public void WebUtilityHelpers_UrlEncode_BadEncodes()
{
var encoded = WebUtilityHelpers.UrlEncode(null, Encoding.UTF8);
Assert.AreEqual("", encoded);
}
2020-10-31 21:11:02 +00:00
[Test]
public void WebUtilityHelpers_UrlDecode_CorrectlyDecodes()
{
foreach (var encoding in _codePagesToTest)
{
foreach (var testString in _stringsToTest)
{
//Check our implementation of Decode in .NET Standard Matches the .NET Framework Version
var encodedString = HttpUtility.UrlEncode(testString, encoding);
2020-10-31 21:11:02 +00:00
var netString = HttpUtility.UrlDecode(encodedString, encoding);
var webUtilityString = WebUtilityHelpers.UrlDecode(encodedString, encoding);
Assert.AreEqual(netString, webUtilityString, $"{testString} did not match the expected decoded value after encoding with {encoding.EncodingName})");
}
}
}
2020-11-04 20:56:54 +00:00
[Test]
public void WebUtilityHelpers_UrlDecode_BadDecodes()
{
var decoded = WebUtilityHelpers.UrlDecode(null, Encoding.UTF8);
Assert.AreEqual("", decoded);
}
}
}