|
| 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 = "../../&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 | +} |
0 commit comments