From 16f96b9b27b9f64fc6f992995932087972cc9ed5 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Sat, 12 Nov 2016 16:38:10 -0500 Subject: [PATCH 1/2] add dictionary of params --- Readme.md | 13 +++++++++++++ .../ExtensionTests.cs | 14 ++++++++++++++ .../TerribleDevUriExtensions.cs | 16 ++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/Readme.md b/Readme.md index 9bbbe91..c7d2d4d 100644 --- a/Readme.md +++ b/Readme.md @@ -32,6 +32,19 @@ new UriBuilder("https://awesome.com/yo) ``` result: `https://awesome.com/yo?id=5` +you can even pass a dictionary of parameters + +```csharp + var dictionary = new Dictionary() + { + ["yo"] = "dawg" + }; + new UriBuilder("http://awesome.com") + .WithParameter(dictionary); + http://awesome.com/?yo=dawg + +``` + ## Getting started Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you! \ No newline at end of file diff --git a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs index 308880a..548a78c 100644 --- a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs @@ -116,5 +116,19 @@ namespace FluentUriBuilder.Tests .WithParameter("supgf", "no22"); Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString()); } + + [Fact] + public void AddDictOfParams() + { + var dictionary = new Dictionary() + { + ["yo"] = "dawg", + ["troll"] = "toll", + ["hammer"] = string.Empty + }; + var url = new UriBuilder("http://awesome.com") + .WithParameter(dictionary); + Assert.Equal("http://awesome.com/?yo=dawg&troll=toll&hammer", url.Uri.ToString()); + } } } \ No newline at end of file diff --git a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs index 32d88dd..466c374 100644 --- a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs +++ b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs @@ -17,6 +17,22 @@ namespace System /// public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values); + /// + /// Appends query strings from dictionary + /// + /// + /// + /// + public static UriBuilder WithParameter(this UriBuilder bld, IDictionary parameterDictionary) + { + if(parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary)); + foreach(var item in parameterDictionary) + { + bld.WithParameter(item.Key, item.Value); + } + return bld; + } + /// /// Appends a query string parameter with a key, and many values. Multiple values will be comma seperated. If only 1 value is passed and its null or value, the key will be added to the QS. /// From 3ff3165a42d9afc9a2d45d51e7fac7c7070a89a4 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Sat, 12 Nov 2016 16:40:22 -0500 Subject: [PATCH 2/2] add exception throw test --- src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs b/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs index 012070c..e055915 100644 --- a/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs @@ -16,6 +16,7 @@ namespace FluentUriBuilder.Tests Assert.Throws(() => tstObj.WithPathSegment(null)); Assert.Throws(() => tstObj.WithScheme(null)); Assert.Throws(() => tstObj.WithHost(null)); + Assert.Throws(() => tstObj.WithParameter(parameterDictionary: null)); Assert.Throws(() => tstObj.WithPort(-1)); } }