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..cec579f 100644 --- a/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs +++ b/src/UriBuilder.Fluent/TerribleDevUriExtensions.cs @@ -52,18 +52,55 @@ 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) + bld.Query = intitialValue.AppendKeyValue(key, valuesEnum); + 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) { - 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.WithFragment(item.Key, item.Value); } - bld.Query = sb.ToString().TrimEnd(','); + 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('?')}&"; + bld.Fragment = intitialValue.AppendKeyValue(key, valuesEnum); return bld; } @@ -166,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