2020-03-03 16:28:26 +00:00
using System.Text ;
2017-11-05 09:42:03 +00:00
using System.Web ;
2018-03-10 08:05:56 +00:00
using Jackett.Common.Helpers ;
2020-10-31 21:11:02 +00:00
using NUnit.Framework ;
using Assert = Microsoft . VisualStudio . TestTools . UnitTesting . Assert ;
2017-11-05 09:42:03 +00:00
2020-10-31 00:12:35 +00:00
namespace Jackett.Test.TestHelpers
2017-11-05 09:42:03 +00:00
{
2020-10-31 21:11:02 +00:00
[TestFixture]
2017-11-05 09:42:03 +00:00
public class WebUtilityHelpersTests
{
private readonly Encoding [ ] _codePagesToTest ;
private readonly string [ ] _stringsToTest ;
public WebUtilityHelpersTests ( )
{
2020-01-22 17:42:23 +00:00
//https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0
2020-02-06 02:21:48 +00:00
Encoding . RegisterProvider ( CodePagesEncodingProvider . Instance ) ;
2020-01-22 17:42:23 +00:00
2020-10-31 21:11:02 +00:00
_codePagesToTest = new [ ] {
2017-11-05 09:42:03 +00:00
Encoding . UTF8 ,
Encoding . ASCII ,
Encoding . GetEncoding ( "iso-8859-1" ) ,
Encoding . GetEncoding ( "windows-1255" ) ,
Encoding . GetEncoding ( "windows-1252" ) ,
Encoding . GetEncoding ( "windows-1251" ) }
;
2020-10-31 21:11:02 +00:00
_stringsToTest = new [ ]
2017-11-05 09:42:03 +00:00
{
"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]
2017-11-05 09:42:03 +00:00
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 ) ;
2017-11-05 09:42:03 +00:00
//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 ) ;
2017-11-05 09:42:03 +00:00
2020-10-31 21:11:02 +00:00
Assert . AreEqual ( netDecode , webUtilityDecode , $"{testString} did not match the expected decoded string with {encoding.EncodingName})" ) ;
2017-11-05 09:42:03 +00:00
}
}
}
2020-10-31 21:11:02 +00:00
[Test]
2017-11-05 09:42:03 +00:00
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})" ) ;
2017-11-05 09:42:03 +00:00
}
}
}
}
}