Skip to content

Commit

Permalink
Work in HttpUtility class (#370)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Nov 23, 2022
1 parent 54d722f commit 89b7584
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 128 deletions.
28 changes: 15 additions & 13 deletions .runsettings
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
<TargetPlatform>x64</TargetPlatform>
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>None</Logging>
<IsRealHardware>False</IsRealHardware>
</nanoFrameworkAdapter>
</RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<MaxCpuCount>1</MaxCpuCount>
<ResultsDirectory>.\TestResults</ResultsDirectory>
<!-- Path relative to solution directory -->
<TestSessionTimeout>60000</TestSessionTimeout>
<!-- Milliseconds -->
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
<TargetPlatform>x64</TargetPlatform>
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>Detailed</Logging>
<IsRealHardware>False</IsRealHardware>
</nanoFrameworkAdapter>
</RunSettings>
30 changes: 9 additions & 21 deletions Tests/HttpUnitTests/HttpUnitTests.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="HttpUtilityTest.cs" />
<Compile Include="StreamContentTest.cs" />
<Compile Include="ByteArrayContentTest.cs" />
<Compile Include="StringContentTest.cs" />
Expand All @@ -39,41 +40,28 @@
<Content Include="packages.lock.json" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib, Version=1.12.0.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<ProjectReference Include="..\..\nanoFramework.System.Net.Http\System.Net.Http.nfproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib">
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.System.Collections, Version=1.4.0.3, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.Collections.1.4.0\lib\nanoFramework.System.Collections.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.2.22.3995, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.System.Text">
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.22\lib\nanoFramework.System.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.TestFramework, Version=2.0.60.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.TestFramework">
<HintPath>..\..\packages\nanoFramework.TestFramework.2.0.60\lib\nanoFramework.TestFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.UnitTestLauncher">
<HintPath>..\..\packages\nanoFramework.TestFramework.2.0.60\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Streams, Version=1.1.27.27650, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="System.IO.Streams">
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.27\lib\System.IO.Streams.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading, Version=1.1.8.6695, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.Threading.1.1.8\lib\System.Threading.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\nanoFramework.System.Net.Http\System.Net.Http.nfproj" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<!-- MANUAL UPDATE HERE -->
<ProjectExtensions>
Expand Down
118 changes: 118 additions & 0 deletions Tests/HttpUnitTests/HttpUtilityTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

using nanoFramework.TestFramework;
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Web;

namespace HttpUnitTests
{
[TestClass]
public class HttpUtilityTest
{

[TestMethod]
public void UrlDecodeNoThrow()
{
string str = "../../&amp;param2=%CURRREV%";

Assert.Equal(str, HttpUtility.UrlDecode(str));
}

[TestMethod]
public void UrlEncodeTest()
{
for (char c = char.MinValue; c < char.MaxValue; c++)
{
byte[] bIn;
bIn = Encoding.UTF8.GetBytes(c.ToString());
MemoryStream expected = new MemoryStream();
MemoryStream expUnicode = new MemoryStream();

// build expected result for UrlEncode
for (int i = 0; i < bIn.Length; i++)
{
UrlEncodeChar((char)bIn[i], expected, false);
}

// build expected result for UrlEncodeUnicode
UrlEncodeChar(c, expUnicode, true);

byte[] bOut = expected.ToArray();

Assert.Equal(
Encoding.UTF8.GetString(bOut, 0, bOut.Length),
HttpUtility.UrlEncode(c.ToString()),
$"Expecting UrlEncode of '{c}' ({(int)c}) as [{Encoding.UTF8.GetString(bOut, 0, bOut.Length)}] got {HttpUtility.UrlEncode(c.ToString())}");
}
}

static void UrlEncodeChar(char c, Stream result, bool isUnicode)
{
if (c > 255)
{
int idx;
int i = (int)c;

result.WriteByte((byte)'%');
result.WriteByte((byte)'u');
idx = i >> 12;
result.WriteByte((byte)hexChars[idx]);
idx = (i >> 8) & 0x0F;
result.WriteByte((byte)hexChars[idx]);
idx = (i >> 4) & 0x0F;
result.WriteByte((byte)hexChars[idx]);
idx = i & 0x0F;
result.WriteByte((byte)hexChars[idx]);
return;
}

if (c > ' ' && notEncoded.IndexOf(c) != -1)
{
result.WriteByte((byte)c);
return;
}
if (c == ' ')
{
result.WriteByte((byte)'+');
return;
}
if ((c < '0') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a') ||
(c > 'z'))
{
if (isUnicode && c > 127)
{
result.WriteByte((byte)'%');
result.WriteByte((byte)'u');
result.WriteByte((byte)'0');
result.WriteByte((byte)'0');
}
else
{
result.WriteByte((byte)'%');
}

int idx = ((int)c) >> 4;
result.WriteByte((byte)hexChars[idx]);
idx = c & 0x0F;
result.WriteByte((byte)hexChars[idx]);
}
else
{
result.WriteByte((byte)c);
}
}

static char[] hexChars = "0123456789ABCDEF".ToCharArray();
const string notEncoded = "!()*-._";
}
}
2 changes: 0 additions & 2 deletions Tests/HttpUnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Collections" version="1.4.0" targetFramework="netnano1.0" />
<package id="nanoFramework.System.IO.Streams" version="1.1.27" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Text" version="1.2.22" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Threading" version="1.1.8" targetFramework="netnano1.0" />
<package id="nanoFramework.TestFramework" version="2.0.60" targetFramework="netnano1.0" developmentDependency="true" />
</packages>
12 changes: 0 additions & 12 deletions Tests/HttpUnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
"resolved": "1.12.0",
"contentHash": "qQrFNXmJiStMC4VXk5cVMOJp23/qlT9FW5i9i+igwQVwraQTtvpkam8yK1hj992jqrbjoCIFZP4Hw9E8H0pB7w=="
},
"nanoFramework.System.Collections": {
"type": "Direct",
"requested": "[1.4.0, 1.4.0]",
"resolved": "1.4.0",
"contentHash": "/yFwxtCFzi+24NuyxcwlH1YyBGOxRX4oHGLwVmFbgbvOyx3ny/Mwyk2YjHTzmTSgUg9C2XxPF+EkXWwCOAkytw=="
},
"nanoFramework.System.IO.Streams": {
"type": "Direct",
"requested": "[1.1.27, 1.1.27]",
Expand All @@ -26,12 +20,6 @@
"resolved": "1.2.22",
"contentHash": "vLvU0II3oJfajQ8MgNm8aCkaQ2JhjznzruwksOorbMJf86zLRbA5NUeg9X/KjbAE5pIalitUOqtNLKorYTbYGg=="
},
"nanoFramework.System.Threading": {
"type": "Direct",
"requested": "[1.1.8, 1.1.8]",
"resolved": "1.1.8",
"contentHash": "oES5GN3KHoDzifRNr06WM7P9NaQf+kDmIYkr1ETR2awmERHz4sRpECduGEatwyo1vMhMvZY/KoBcEpAyKNbDgQ=="
},
"nanoFramework.TestFramework": {
"type": "Direct",
"requested": "[2.0.60, 2.0.60]",
Expand Down
Loading

0 comments on commit 89b7584

Please sign in to comment.