Skip to content

Commit 89b7584

Browse files
authored
Work in HttpUtility class (#370)
***NO_CI***
1 parent 54d722f commit 89b7584

File tree

6 files changed

+277
-128
lines changed

6 files changed

+277
-128
lines changed

.runsettings

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RunSettings>
3-
<!-- Configurations that affect the Test Framework -->
4-
<RunConfiguration>
5-
<MaxCpuCount>1</MaxCpuCount>
6-
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
7-
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
8-
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
9-
<TargetPlatform>x64</TargetPlatform>
10-
</RunConfiguration>
11-
<nanoFrameworkAdapter>
12-
<Logging>None</Logging>
13-
<IsRealHardware>False</IsRealHardware>
14-
</nanoFrameworkAdapter>
15-
</RunSettings>
3+
<!-- Configurations that affect the Test Framework -->
4+
<RunConfiguration>
5+
<MaxCpuCount>1</MaxCpuCount>
6+
<ResultsDirectory>.\TestResults</ResultsDirectory>
7+
<!-- Path relative to solution directory -->
8+
<TestSessionTimeout>60000</TestSessionTimeout>
9+
<!-- Milliseconds -->
10+
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
11+
<TargetPlatform>x64</TargetPlatform>
12+
</RunConfiguration>
13+
<nanoFrameworkAdapter>
14+
<Logging>Detailed</Logging>
15+
<IsRealHardware>False</IsRealHardware>
16+
</nanoFrameworkAdapter>
17+
</RunSettings>

Tests/HttpUnitTests/HttpUnitTests.nfproj

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
</PropertyGroup>
2727
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2828
<ItemGroup>
29+
<Compile Include="HttpUtilityTest.cs" />
2930
<Compile Include="StreamContentTest.cs" />
3031
<Compile Include="ByteArrayContentTest.cs" />
3132
<Compile Include="StringContentTest.cs" />
@@ -39,41 +40,28 @@
3940
<Content Include="packages.lock.json" />
4041
</ItemGroup>
4142
<ItemGroup>
42-
<Reference Include="mscorlib, Version=1.12.0.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
43+
<ProjectReference Include="..\..\nanoFramework.System.Net.Http\System.Net.Http.nfproj" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Reference Include="mscorlib">
4347
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
44-
<Private>True</Private>
45-
</Reference>
46-
<Reference Include="nanoFramework.System.Collections, Version=1.4.0.3, Culture=neutral, PublicKeyToken=c07d481e9758c731">
47-
<HintPath>..\..\packages\nanoFramework.System.Collections.1.4.0\lib\nanoFramework.System.Collections.dll</HintPath>
48-
<Private>True</Private>
4948
</Reference>
50-
<Reference Include="nanoFramework.System.Text, Version=1.2.22.3995, Culture=neutral, PublicKeyToken=c07d481e9758c731">
49+
<Reference Include="nanoFramework.System.Text">
5150
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.22\lib\nanoFramework.System.Text.dll</HintPath>
52-
<Private>True</Private>
5351
</Reference>
54-
<Reference Include="nanoFramework.TestFramework, Version=2.0.60.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
52+
<Reference Include="nanoFramework.TestFramework">
5553
<HintPath>..\..\packages\nanoFramework.TestFramework.2.0.60\lib\nanoFramework.TestFramework.dll</HintPath>
56-
<Private>True</Private>
5754
</Reference>
58-
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
55+
<Reference Include="nanoFramework.UnitTestLauncher">
5956
<HintPath>..\..\packages\nanoFramework.TestFramework.2.0.60\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
60-
<Private>True</Private>
6157
</Reference>
62-
<Reference Include="System.IO.Streams, Version=1.1.27.27650, Culture=neutral, PublicKeyToken=c07d481e9758c731">
58+
<Reference Include="System.IO.Streams">
6359
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.27\lib\System.IO.Streams.dll</HintPath>
64-
<Private>True</Private>
65-
</Reference>
66-
<Reference Include="System.Threading, Version=1.1.8.6695, Culture=neutral, PublicKeyToken=c07d481e9758c731">
67-
<HintPath>..\..\packages\nanoFramework.System.Threading.1.1.8\lib\System.Threading.dll</HintPath>
68-
<Private>True</Private>
6960
</Reference>
7061
</ItemGroup>
7162
<ItemGroup>
7263
<None Include="packages.config" />
7364
</ItemGroup>
74-
<ItemGroup>
75-
<ProjectReference Include="..\..\nanoFramework.System.Net.Http\System.Net.Http.nfproj" />
76-
</ItemGroup>
7765
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
7866
<!-- MANUAL UPDATE HERE -->
7967
<ProjectExtensions>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using nanoFramework.TestFramework;
8+
using System;
9+
using System.IO;
10+
using System.Net.Http;
11+
using System.Reflection;
12+
using System.Text;
13+
using System.Web;
14+
15+
namespace HttpUnitTests
16+
{
17+
[TestClass]
18+
public class HttpUtilityTest
19+
{
20+
21+
[TestMethod]
22+
public void UrlDecodeNoThrow()
23+
{
24+
string str = "../../&amp;param2=%CURRREV%";
25+
26+
Assert.Equal(str, HttpUtility.UrlDecode(str));
27+
}
28+
29+
[TestMethod]
30+
public void UrlEncodeTest()
31+
{
32+
for (char c = char.MinValue; c < char.MaxValue; c++)
33+
{
34+
byte[] bIn;
35+
bIn = Encoding.UTF8.GetBytes(c.ToString());
36+
MemoryStream expected = new MemoryStream();
37+
MemoryStream expUnicode = new MemoryStream();
38+
39+
// build expected result for UrlEncode
40+
for (int i = 0; i < bIn.Length; i++)
41+
{
42+
UrlEncodeChar((char)bIn[i], expected, false);
43+
}
44+
45+
// build expected result for UrlEncodeUnicode
46+
UrlEncodeChar(c, expUnicode, true);
47+
48+
byte[] bOut = expected.ToArray();
49+
50+
Assert.Equal(
51+
Encoding.UTF8.GetString(bOut, 0, bOut.Length),
52+
HttpUtility.UrlEncode(c.ToString()),
53+
$"Expecting UrlEncode of '{c}' ({(int)c}) as [{Encoding.UTF8.GetString(bOut, 0, bOut.Length)}] got {HttpUtility.UrlEncode(c.ToString())}");
54+
}
55+
}
56+
57+
static void UrlEncodeChar(char c, Stream result, bool isUnicode)
58+
{
59+
if (c > 255)
60+
{
61+
int idx;
62+
int i = (int)c;
63+
64+
result.WriteByte((byte)'%');
65+
result.WriteByte((byte)'u');
66+
idx = i >> 12;
67+
result.WriteByte((byte)hexChars[idx]);
68+
idx = (i >> 8) & 0x0F;
69+
result.WriteByte((byte)hexChars[idx]);
70+
idx = (i >> 4) & 0x0F;
71+
result.WriteByte((byte)hexChars[idx]);
72+
idx = i & 0x0F;
73+
result.WriteByte((byte)hexChars[idx]);
74+
return;
75+
}
76+
77+
if (c > ' ' && notEncoded.IndexOf(c) != -1)
78+
{
79+
result.WriteByte((byte)c);
80+
return;
81+
}
82+
if (c == ' ')
83+
{
84+
result.WriteByte((byte)'+');
85+
return;
86+
}
87+
if ((c < '0') ||
88+
(c < 'A' && c > '9') ||
89+
(c > 'Z' && c < 'a') ||
90+
(c > 'z'))
91+
{
92+
if (isUnicode && c > 127)
93+
{
94+
result.WriteByte((byte)'%');
95+
result.WriteByte((byte)'u');
96+
result.WriteByte((byte)'0');
97+
result.WriteByte((byte)'0');
98+
}
99+
else
100+
{
101+
result.WriteByte((byte)'%');
102+
}
103+
104+
int idx = ((int)c) >> 4;
105+
result.WriteByte((byte)hexChars[idx]);
106+
idx = c & 0x0F;
107+
result.WriteByte((byte)hexChars[idx]);
108+
}
109+
else
110+
{
111+
result.WriteByte((byte)c);
112+
}
113+
}
114+
115+
static char[] hexChars = "0123456789ABCDEF".ToCharArray();
116+
const string notEncoded = "!()*-._";
117+
}
118+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
4-
<package id="nanoFramework.System.Collections" version="1.4.0" targetFramework="netnano1.0" />
54
<package id="nanoFramework.System.IO.Streams" version="1.1.27" targetFramework="netnano1.0" />
65
<package id="nanoFramework.System.Text" version="1.2.22" targetFramework="netnano1.0" />
7-
<package id="nanoFramework.System.Threading" version="1.1.8" targetFramework="netnano1.0" />
86
<package id="nanoFramework.TestFramework" version="2.0.60" targetFramework="netnano1.0" developmentDependency="true" />
97
</packages>

Tests/HttpUnitTests/packages.lock.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
"resolved": "1.12.0",
99
"contentHash": "qQrFNXmJiStMC4VXk5cVMOJp23/qlT9FW5i9i+igwQVwraQTtvpkam8yK1hj992jqrbjoCIFZP4Hw9E8H0pB7w=="
1010
},
11-
"nanoFramework.System.Collections": {
12-
"type": "Direct",
13-
"requested": "[1.4.0, 1.4.0]",
14-
"resolved": "1.4.0",
15-
"contentHash": "/yFwxtCFzi+24NuyxcwlH1YyBGOxRX4oHGLwVmFbgbvOyx3ny/Mwyk2YjHTzmTSgUg9C2XxPF+EkXWwCOAkytw=="
16-
},
1711
"nanoFramework.System.IO.Streams": {
1812
"type": "Direct",
1913
"requested": "[1.1.27, 1.1.27]",
@@ -26,12 +20,6 @@
2620
"resolved": "1.2.22",
2721
"contentHash": "vLvU0II3oJfajQ8MgNm8aCkaQ2JhjznzruwksOorbMJf86zLRbA5NUeg9X/KjbAE5pIalitUOqtNLKorYTbYGg=="
2822
},
29-
"nanoFramework.System.Threading": {
30-
"type": "Direct",
31-
"requested": "[1.1.8, 1.1.8]",
32-
"resolved": "1.1.8",
33-
"contentHash": "oES5GN3KHoDzifRNr06WM7P9NaQf+kDmIYkr1ETR2awmERHz4sRpECduGEatwyo1vMhMvZY/KoBcEpAyKNbDgQ=="
34-
},
3523
"nanoFramework.TestFramework": {
3624
"type": "Direct",
3725
"requested": "[2.0.60, 2.0.60]",

0 commit comments

Comments
 (0)