Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
QueryBuilder Add overload for IEnumerable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jul 24, 2014
1 parent af279f6 commit 7be9cef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Microsoft.AspNet.WebUtilities/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ public class QueryBuilder : IEnumerable<KeyValuePair<string, string>>
{
private IList<KeyValuePair<string, string>> _params;

public QueryBuilder()
{
public QueryBuilder()
{
_params = new List<KeyValuePair<string, string>>();
}
}

public QueryBuilder(IEnumerable<KeyValuePair<string, string>> parameters)
{
_params = new List<KeyValuePair<string, string>>(parameters);
}

public void Add(string key, IEnumerable<string> values)
{
foreach (var value in values)
{
_params.Add(new KeyValuePair<string, string>(key, value));
}
}

public void Add(string key, string value)
{
_params.Add(new KeyValuePair<string, string>(key, value));
Expand Down
21 changes: 21 additions & 0 deletions test/Microsoft.AspNet.WebUtilities.Tests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public void AddMultipleValues_AddedInOrder()
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder.ToString());
}

[Fact]
public void AddIEnumerableValues_AddedInOrder()
{
var builder = new QueryBuilder();
builder.Add("key", new[] { "value1", "value2", "value3" });
Assert.Equal("?key=value1&key=value2&key=value3", builder.ToString());
}

[Fact]
public void AddMultipleValuesViaConstructor_AddedInOrder()
{
Expand All @@ -73,5 +81,18 @@ public void AddMultipleValuesViaInitializer_AddedInOrder()
};
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder.ToString());
}

[Fact]
public void CopyViaConstructor_AddedInOrder()
{
var builder = new QueryBuilder()
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "key3", "value3" },
};
var builder1 = new QueryBuilder(builder);
Assert.Equal("?key1=value1&key2=value2&key3=value3", builder1.ToString());
}
}
}

0 comments on commit 7be9cef

Please sign in to comment.