From d5caf139e9fa6fca8ac9ec186a3cc5bace573450 Mon Sep 17 00:00:00 2001 From: Adrian Godong Date: Thu, 19 Oct 2017 20:17:08 -0700 Subject: [PATCH 1/2] Added extension methods to add fragments. --- .../ExtensionTests.cs | 16 +++++ .../ThrowsTests.cs | 2 + .../TerribleDevUriExtensions.cs | 59 +++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs index 6aef3cb..48319ce 100644 --- a/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ExtensionTests.cs @@ -71,6 +71,22 @@ namespace FluentUriBuilder.Tests Assert.Equal("http://awesome.com/?awesome", url.Uri.ToString()); } + [Fact] + public void TestAddFragmentArrayint() + { + var url = new UriBuilder("http://awesome.com") + .WithFragment("awesome", new List() { 1, 2 }.Cast()); + Assert.Equal("http://awesome.com/#awesome=1,2", url.Uri.ToString()); + } + + [Fact] + public void TestAddFragmentNoValue() + { + var url = new UriBuilder("http://awesome.com") + .WithFragment("awesome"); + Assert.Equal("http://awesome.com/#awesome", url.Uri.ToString()); + } + [Fact] public void WithPort() { diff --git a/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs b/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs index e055915..2448e81 100644 --- a/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs +++ b/src/UriBuilder.Fluent.UnitTests/ThrowsTests.cs @@ -13,10 +13,12 @@ namespace FluentUriBuilder.Tests { var tstObj = new UriBuilder(); Assert.Throws(() => tstObj.WithParameter(string.Empty, string.Empty)); + Assert.Throws(() => tstObj.WithFragment(string.Empty, string.Empty)); Assert.Throws(() => tstObj.WithPathSegment(null)); Assert.Throws(() => tstObj.WithScheme(null)); Assert.Throws(() => tstObj.WithHost(null)); Assert.Throws(() => tstObj.WithParameter(parameterDictionary: null)); + Assert.Throws(() => tstObj.WithFragment(fragmentDictionary: null)); Assert.Throws(() => tstObj.WithPort(-1)); } } diff --git a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs index 06641aa..ea882f8 100644 --- a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs +++ b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs @@ -67,6 +67,65 @@ namespace System return bld; } + /// + /// Appends a fragments 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 fragment. + /// + /// + /// + /// + /// + public static UriBuilder WithFragment(this UriBuilder bld, string key, params string[] values) => bld.WithFragment(key, valuesEnum: values); + + /// + /// Appends fragments from dictionary + /// + /// + /// + /// + /// + public static UriBuilder WithFragment(this UriBuilder bld, IDictionary fragmentDictionary) + { + if(fragmentDictionary == null) throw new ArgumentNullException(nameof(fragmentDictionary)); + foreach(var item in fragmentDictionary) + { + bld.WithFragment(item.Key, item.Value); + } + return bld; + } + + /// + /// Appends a fragments 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 fragment. + /// + /// + /// + /// + /// + public static UriBuilder WithFragment(this UriBuilder bld, string key, IEnumerable valuesEnum) + { + if(string.IsNullOrWhiteSpace(key)) + { + throw new ArgumentNullException(nameof(key)); + } + if(valuesEnum == null) + { + valuesEnum = new string[0]; + } + var intitialValue = string.IsNullOrWhiteSpace(bld.Fragment) ? "" : $"{bld.Fragment.TrimStart('?')}&"; + var sb = new StringBuilder($"{intitialValue}{key}"); + var validValueHit = false; + foreach(var value in valuesEnum) + { + var toSValue = value?.ToString(); + if(string.IsNullOrWhiteSpace(toSValue)) continue; + // we can't just have an = sign since its valid to have query string paramters with no value; + if(!validValueHit) toSValue = "=" + value; + validValueHit = true; + sb.Append($"{toSValue},"); + } + bld.Fragment = sb.ToString().TrimEnd(','); + return bld; + } + /// /// Sets the port to be the port number /// From 8c036b0986a2a669285a258f6573bde1a5cd0abf Mon Sep 17 00:00:00 2001 From: Adrian Godong Date: Thu, 19 Oct 2017 20:40:21 -0700 Subject: [PATCH 2/2] Refactored key value encoding into a separate method. --- .../TerribleDevUriExtensions.cs | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs index ea882f8..cec579f 100644 --- a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs +++ b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs @@ -52,18 +52,7 @@ namespace System valuesEnum = new string[0]; } var intitialValue = string.IsNullOrWhiteSpace(bld.Query) ? "" : $"{bld.Query.TrimStart('?')}&"; - var sb = new StringBuilder($"{intitialValue}{key}"); - var validValueHit = false; - foreach(var value in valuesEnum) - { - var toSValue = value?.ToString(); - if(string.IsNullOrWhiteSpace(toSValue)) continue; - // we can't just have an = sign since its valid to have query string paramters with no value; - if(!validValueHit) toSValue = "=" + value; - validValueHit = true; - sb.Append($"{toSValue},"); - } - bld.Query = sb.ToString().TrimEnd(','); + bld.Query = intitialValue.AppendKeyValue(key, valuesEnum); return bld; } @@ -111,18 +100,7 @@ namespace System valuesEnum = new string[0]; } var intitialValue = string.IsNullOrWhiteSpace(bld.Fragment) ? "" : $"{bld.Fragment.TrimStart('?')}&"; - var sb = new StringBuilder($"{intitialValue}{key}"); - var validValueHit = false; - foreach(var value in valuesEnum) - { - var toSValue = value?.ToString(); - if(string.IsNullOrWhiteSpace(toSValue)) continue; - // we can't just have an = sign since its valid to have query string paramters with no value; - if(!validValueHit) toSValue = "=" + value; - validValueHit = true; - sb.Append($"{toSValue},"); - } - bld.Fragment = sb.ToString().TrimEnd(','); + bld.Fragment = intitialValue.AppendKeyValue(key, valuesEnum); return bld; } @@ -225,5 +203,23 @@ namespace System /// public static string ToEscapeDataString(this UriBuilder bld) => Uri.EscapeDataString(bld.Uri.ToString()); + /// + /// Appends x-www-form-urlencoded key and valuesEnum into initialValue. + /// + private static string AppendKeyValue(this string intitialValue, string key, IEnumerable valuesEnum) + { + var sb = new StringBuilder($"{intitialValue}{key}"); + var validValueHit = false; + foreach(var value in valuesEnum) + { + var toSValue = value?.ToString(); + if(string.IsNullOrWhiteSpace(toSValue)) continue; + // we can't just have an = sign since its valid to have query string paramters with no value; + if(!validValueHit) toSValue = "=" + value; + validValueHit = true; + sb.Append($"{toSValue},"); + } + return sb.ToString().TrimEnd(','); + } } } \ No newline at end of file