Repository navigation
Expand file tree
/
Copy pathUriQueryUtilityTest.cs
More file actions
125 lines (107 loc) · 3.9 KB
/
Copy pathUriQueryUtilityTest.cs
File metadata and controls
125 lines (107 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net.Http.Formatting;
using System.Text;
using System.Web.Http;
using Microsoft.TestCommon;
namespace System.Net.Http
{
public class UriQueryUtilityTest
{
public static TheoryDataSet<string, string, string> UriQueryData
{
get
{
return UriQueryTestData.UriQueryData;
}
}
[Fact]
public void TypeIsCorrect()
{
Assert.Type.HasProperties(typeof(UriQueryUtility), TypeAssert.TypeProperties.IsClass | TypeAssert.TypeProperties.IsStatic);
}
[Fact]
public void UrlEncode_ReturnsNull()
{
Assert.Null(UriQueryUtility.UrlEncode(null));
}
[Fact]
public void UrlDecode_ReturnsNull()
{
Assert.Null(UriQueryUtility.UrlDecode(null));
}
[Fact]
public void UrlDecode_ParsesEmptySegmentsCorrectly()
{
int iterations = 16;
List<string> segments = new List<string>();
for (int index = 1; index < iterations; index++)
{
segments.Add("&");
string query = string.Join("", segments);
NameValueCollection result = ParseQueryString(query);
Assert.NotNull(result);
// Because this is a NameValueCollection, the same name appears only once
Assert.Single(result);
// Values should be a comma separated list of empty strings
string[] values = result[""].Split(new char[] { ',' });
// We expect length+1 segment as the final '&' counts as a segment
Assert.Equal(index + 1, values.Length);
foreach (var value in values)
{
Assert.Equal("", value);
}
}
}
[Theory]
[InlineData("N", "N", "")]
[InlineData("%26", "&", "")]
[InlineData("foo=%u0026", "foo", "%u0026")]
[PropertyData("UriQueryData")]
public void UrlDecode_ParsesCorrectly(string segment, string resultName, string resultValue)
{
int iterations = 16;
List<string> segments = new List<string>();
for (int index = 1; index < iterations; index++)
{
segments.Add(segment);
string query = CreateQuery(segments.ToArray());
NameValueCollection result = ParseQueryString(query);
Assert.NotNull(result);
// Because this is a NameValueCollection, the same name appears only once
Assert.Single(result);
// Values should be a comma separated list of resultValue
string[] values = result[resultName].Split(new char[] { ',' });
Assert.Equal(index, values.Length);
foreach (var value in values)
{
Assert.Equal(resultValue, value);
}
}
}
private static string CreateQuery(params string[] segments)
{
StringBuilder buffer = new StringBuilder();
bool first = true;
foreach (string segment in segments)
{
if (first)
{
first = false;
}
else
{
buffer.Append('&');
}
buffer.Append(segment);
}
return buffer.ToString();
}
private static NameValueCollection ParseQueryString(string query)
{
return new FormDataCollection(query).ReadAsNameValueCollection();
}
}
}