Skip to content

Commit 5e149d5

Browse files
author
Jimmy Merrild Krag
committed
Added method and tests for GetQueryKeys
1 parent 77fe3db commit 5e149d5

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

UriBuilderExtended.Test/UriBuilderExtensionQueryTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ public void SetQuery()
137137
Assert.IsFalse(uriBuilder.HasQuery("key", "value3"), "Unexpected value found");
138138
}
139139

140+
[TestMethod]
141+
public void GetQueryKeys()
142+
{
143+
string urlString = "http://www.test.com/";
144+
145+
UriBuilder uriBuilder = new UriBuilder(urlString);
146+
147+
HashSet<string> inputKeys = new HashSet<string>
148+
{
149+
"key1",
150+
"key2",
151+
"key3",
152+
"key4",
153+
"key5",
154+
};
155+
156+
foreach (string key in inputKeys)
157+
{
158+
uriBuilder.AddQuery(key, "value");
159+
}
160+
161+
HashSet<string> retrievedKeys = new HashSet<string>(uriBuilder.GetQueryKeys());
162+
163+
Assert.AreEqual<int>(uriBuilder.GetQueryKeys().Count, inputKeys.Count, "Count does not match input count");
164+
165+
Assert.IsTrue(inputKeys.SetEquals(retrievedKeys), "Retrieved result does not match set keys");
166+
}
167+
140168
[TestMethod]
141169
public void GetQueryValues()
142170
{
@@ -160,7 +188,7 @@ public void GetQueryValues()
160188

161189
HashSet<string> retrievedValues = new HashSet<string>(uriBuilder.GetQueryValues("key"));
162190

163-
Assert.AreEqual<int>(uriBuilder.GetQueryValues("key").Count, inputValues.Count, "Count for key does not match input count");
191+
Assert.AreEqual<int>(uriBuilder.GetQueryValues("key").Count, inputValues.Count, "Count does not match input count");
164192

165193
Assert.IsTrue(inputValues.SetEquals(retrievedValues), "Retrieved result does not match set values");
166194

UriBuilderExtended/UriBuilderExtensions.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,29 @@ public static UriBuilder AddQuery(this UriBuilder uri, string key, params string
120120
}
121121

122122
/// <summary>
123-
/// Get all values for the given key.
123+
/// Get all keys in the query
124+
/// </summary>
125+
/// <param name="uri">The <see cref="Uri"/></param>
126+
/// <returns>A collection with all keys in the current query</returns>
127+
public static ICollection<string> GetQueryKeys(this UriBuilder uri)
128+
{
129+
HashSet<string> result = new HashSet<string>();
130+
131+
NameValueCollection queryValues = uri.ParseQuery();
132+
foreach (string key in queryValues.AllKeys)
133+
{
134+
result.Add(key);
135+
}
136+
137+
return result;
138+
}
139+
140+
/// <summary>
141+
/// Get all values for the given key
124142
/// </summary>
125143
/// <param name="uri">The <see cref="Uri"/></param>
126144
/// <param name="key">The key to look up on</param>
127-
/// <returns>A collection of values or an empty list for non-existing keys.</returns>
145+
/// <returns>A collection of values or an empty list if the key does not exist</returns>
128146
public static ICollection<string> GetQueryValues(this UriBuilder uri, string key)
129147
{
130148
HashSet<string> result = new HashSet<string>();
@@ -135,7 +153,6 @@ public static ICollection<string> GetQueryValues(this UriBuilder uri, string key
135153
}
136154

137155
NameValueCollection queryValues = uri.ParseQuery();
138-
139156
foreach (string value in queryValues.GetValues(key))
140157
{
141158
result.Add(value);

0 commit comments

Comments
 (0)