From 7be9cefc4c7671caba7d8fe538ba6a0ad308fdad Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Thu, 24 Jul 2014 16:24:44 -0700 Subject: [PATCH] QueryBuilder Add overload for IEnumerable. --- .../QueryBuilder.cs | 14 ++++++++++--- .../QueryBuilderTests.cs | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNet.WebUtilities/QueryBuilder.cs b/src/Microsoft.AspNet.WebUtilities/QueryBuilder.cs index a719db53..28cdfa85 100644 --- a/src/Microsoft.AspNet.WebUtilities/QueryBuilder.cs +++ b/src/Microsoft.AspNet.WebUtilities/QueryBuilder.cs @@ -14,16 +14,24 @@ public class QueryBuilder : IEnumerable> { private IList> _params; - public QueryBuilder() - { + public QueryBuilder() + { _params = new List>(); - } + } public QueryBuilder(IEnumerable> parameters) { _params = new List>(parameters); } + public void Add(string key, IEnumerable values) + { + foreach (var value in values) + { + _params.Add(new KeyValuePair(key, value)); + } + } + public void Add(string key, string value) { _params.Add(new KeyValuePair(key, value)); diff --git a/test/Microsoft.AspNet.WebUtilities.Tests/QueryBuilderTests.cs b/test/Microsoft.AspNet.WebUtilities.Tests/QueryBuilderTests.cs index 2de60d8a..76d3ac97 100644 --- a/test/Microsoft.AspNet.WebUtilities.Tests/QueryBuilderTests.cs +++ b/test/Microsoft.AspNet.WebUtilities.Tests/QueryBuilderTests.cs @@ -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() { @@ -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()); + } } } \ No newline at end of file