Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
819235eedb | ||
|
|
d3a9225930 | ||
|
|
638d077c03 | ||
|
|
b9e6ad6a8b | ||
|
|
542561d778 | ||
|
|
dc1b82f0ab | ||
|
|
8c036b0986 | ||
|
|
d5caf139e9 | ||
|
|
ccc3f229d0 | ||
|
|
4bf5ed64dc |
@@ -19,6 +19,7 @@ new UriBuilder()
|
||||
.WithHost("awesome.com")
|
||||
.WithPathSegment("seg")
|
||||
.UseHttps()
|
||||
.Uri
|
||||
.ToString()
|
||||
|
||||
```
|
||||
@@ -30,6 +31,7 @@ or
|
||||
|
||||
new UriBuilder("https://awesome.com/yo)
|
||||
.WithParameter("id", "5")
|
||||
.Uri
|
||||
.ToString();
|
||||
|
||||
```
|
||||
@@ -78,4 +80,4 @@ var uri = new UriBuilder("https://awesome.com")
|
||||
|
||||
```
|
||||
|
||||
result: `http%3A%2F%2Fawesome.com%2F%3Fyo%3Ddawg%3C`
|
||||
result: `http%3A%2F%2Fawesome.com%2F%3Fyo%3Ddawg%3C`
|
||||
|
||||
@@ -23,6 +23,6 @@ test_script:
|
||||
deploy:
|
||||
- provider: NuGet
|
||||
api_key:
|
||||
secure: bGn7M6dHOJ3QjwYIv7e34tcY/n9cCUZmL1MnM6jRfmnJOOfwlrS+cdRj2n8Wf31n
|
||||
secure: //tKHlb2yqAtpxnR6p9IAtXwQNaq8UYYyIFSD0QVF3XnEasIxG2gTWdmWuG87fUX
|
||||
on:
|
||||
appveyor_repo_tag: true
|
||||
@@ -71,6 +71,30 @@ namespace FluentUriBuilder.Tests
|
||||
Assert.Equal("http://awesome.com/?awesome", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestAddFragmentArray()
|
||||
{
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithFragment("awesome", "cool", "dawg");
|
||||
Assert.Equal("http://awesome.com/#awesome=cool,dawg", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestAddFragmentArrayint()
|
||||
{
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithFragment("awesome", new List<int>() { 1, 2 }.Cast<object>());
|
||||
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()
|
||||
{
|
||||
@@ -158,6 +182,40 @@ namespace FluentUriBuilder.Tests
|
||||
Assert.Equal("http://awesome.com/?yo=dawg&troll=toll&hammer", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddFragmentWithNoValue()
|
||||
{
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithFragment("awesome", "yodawg")
|
||||
.WithFragment("fun", null)
|
||||
.WithFragment("cool", string.Empty);
|
||||
Assert.Equal("http://awesome.com/#awesome=yodawg&fun&cool", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestAddTwoUrlFragments()
|
||||
{
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithFragment("awesome", "yodawg")
|
||||
.WithFragment("supg", "no2")
|
||||
.WithFragment("supgf", "no22");
|
||||
Assert.Equal("http://awesome.com/#awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDictOfFragments()
|
||||
{
|
||||
var dictionary = new Dictionary<string, string>()
|
||||
{
|
||||
["yo"] = "dawg",
|
||||
["troll"] = "toll",
|
||||
["hammer"] = string.Empty
|
||||
};
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithFragment(dictionary);
|
||||
Assert.Equal("http://awesome.com/#yo=dawg&troll=toll&hammer", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestToEscapedString()
|
||||
{
|
||||
|
||||
@@ -13,11 +13,20 @@ namespace FluentUriBuilder.Tests
|
||||
{
|
||||
var tstObj = new UriBuilder();
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter(string.Empty, string.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithFragment(string.Empty, string.Empty));
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithPathSegment(null));
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithScheme(null));
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithHost(null));
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter(parameterDictionary: null));
|
||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithFragment(fragmentDictionary: null));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => tstObj.WithPort(-1));
|
||||
UriBuilder nullPtr = null;
|
||||
Assert.Throws<ArgumentNullException>(()=>nullPtr.WithFragment("yo", "dawg"));
|
||||
Assert.Throws<ArgumentNullException>(()=>nullPtr.WithParameter("yo", "dawg"));
|
||||
Assert.Throws<ArgumentNullException>(()=>nullPtr.WithPathSegment("yo"));
|
||||
Assert.Throws<ArgumentNullException>(()=>nullPtr.WithScheme("yo"));
|
||||
Assert.Throws<ArgumentNullException>(()=>nullPtr.WithFragment("yo"));
|
||||
Assert.Throws<ArgumentNullException>(()=>nullPtr.WithoutDefaultPort());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks>
|
||||
<DebugType>full</DebugType>
|
||||
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
|
||||
|
||||
@@ -26,6 +26,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithParameter(this UriBuilder bld, IDictionary<string, string> parameterDictionary)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if(parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary));
|
||||
foreach(var item in parameterDictionary)
|
||||
{
|
||||
@@ -43,6 +47,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithParameter(this UriBuilder bld, string key, IEnumerable<object> valuesEnum)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
@@ -52,18 +60,63 @@ 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="bld"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="values"></param>
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithFragment(this UriBuilder bld, string key, params string[] values) => bld.WithFragment(key, valuesEnum: values);
|
||||
|
||||
/// <summary>
|
||||
/// Appends fragments from dictionary
|
||||
/// </summary>
|
||||
/// <param name="bld"></param>
|
||||
/// <param name="fragmentDictionary"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithFragment(this UriBuilder bld, IDictionary<string, string> fragmentDictionary)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
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},");
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
bld.Query = sb.ToString().TrimEnd(',');
|
||||
if(fragmentDictionary == null) throw new ArgumentNullException(nameof(fragmentDictionary));
|
||||
foreach(var item in fragmentDictionary)
|
||||
{
|
||||
bld.WithFragment(item.Key, item.Value);
|
||||
}
|
||||
return bld;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="bld"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="valuesEnum"></param>
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithFragment(this UriBuilder bld, string key, IEnumerable<object> valuesEnum)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -76,6 +129,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithPort(this UriBuilder bld, int port)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if(port < 1) throw new ArgumentOutOfRangeException(nameof(port));
|
||||
bld.Port = port;
|
||||
return bld;
|
||||
@@ -88,6 +145,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithoutDefaultPort(this UriBuilder bld)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if (bld.Uri.IsDefaultPort) bld.Port = -1;
|
||||
return bld;
|
||||
}
|
||||
@@ -101,6 +162,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithPathSegment(this UriBuilder bld, string pathSegment)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(pathSegment))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(pathSegment));
|
||||
@@ -119,6 +184,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithScheme(this UriBuilder bld, string scheme)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(scheme)) throw new ArgumentNullException(nameof(scheme));
|
||||
bld.Scheme = scheme;
|
||||
return bld;
|
||||
@@ -133,6 +202,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithHost(this UriBuilder bld, string host)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
if(string.IsNullOrWhiteSpace(host)) throw new ArgumentNullException(nameof(host));
|
||||
bld.Host = host;
|
||||
return bld;
|
||||
@@ -148,6 +221,10 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static UriBuilder UseHttps(this UriBuilder bld, bool predicate = true)
|
||||
{
|
||||
if(bld == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bld));
|
||||
}
|
||||
bld.Scheme = predicate ? "https" : "http";
|
||||
return bld;
|
||||
}
|
||||
@@ -166,5 +243,23 @@ namespace System
|
||||
/// <returns></returns>
|
||||
public static string ToEscapeDataString(this UriBuilder bld) => Uri.EscapeDataString(bld.Uri.ToString());
|
||||
|
||||
/// <summary>
|
||||
/// Appends x-www-form-urlencoded key and valuesEnum into initialValue.
|
||||
/// </summary>
|
||||
private static string AppendKeyValue(this string intitialValue, string key, IEnumerable<object> 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(',');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,11 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<Authors>Tommy Parnell</Authors>
|
||||
<TargetFrameworks>netstandard1.1;net40;net45</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard1.1;net40;net45;netstandard2.0</TargetFrameworks>
|
||||
<AssemblyName>UriBuilder.Fluent</AssemblyName>
|
||||
<PackageId>UriBuilder.Fluent</PackageId>
|
||||
<PackageTags>Url building;Uri;Uri building;fluent;extension</PackageTags>
|
||||
<PackageProjectUrl>https://github.com/TerribleDev/UriBuilder.Fluent</PackageProjectUrl>
|
||||
<DebugType>full</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
|
||||
|
||||
Reference in New Issue
Block a user