Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f7ca24155 | ||
|
|
628f66b82a | ||
|
|
abb2c1d18b | ||
|
|
c67a34f993 | ||
|
|
697a745510 | ||
|
|
7e303fd08b | ||
|
|
737c647cbe | ||
|
|
730ee07822 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -248,4 +248,5 @@ ModelManifest.xml
|
|||||||
coverage.opencover.xml
|
coverage.opencover.xml
|
||||||
coverage.json
|
coverage.json
|
||||||
coverage.info
|
coverage.info
|
||||||
|
.DS_Store
|
||||||
output
|
output
|
||||||
@@ -81,3 +81,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`
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
@@ -16,7 +16,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithPathSegment(pathWithSlashes);
|
.WithPathSegment(pathWithSlashes);
|
||||||
Assert.Equal("http://awesome.com/" + expectedPath, url.Uri.ToString());
|
Assert.Equal("http://awesome.com/" + expectedPath, url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -25,7 +25,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithPathSegment("yodawg")
|
.WithPathSegment("yodawg")
|
||||||
.WithPathSegment("/immahslash/");
|
.WithPathSegment("/immahslash/");
|
||||||
Assert.Equal("http://awesome.com/yodawg/immahslash/", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/yodawg/immahslash/", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -33,7 +33,17 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithParameter("awesome", "yodawg");
|
.WithParameter("awesome", "yodawg");
|
||||||
Assert.Equal("http://awesome.com/?awesome=yodawg", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?awesome=yodawg", url.ToUriString());
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void TestAddUrlParameterAsTupleList()
|
||||||
|
{
|
||||||
|
var url = new UriBuilder("http://awesome.com")
|
||||||
|
.WithParameter(new List<(string, string)>()
|
||||||
|
{
|
||||||
|
("awesome", "yodawg")
|
||||||
|
});
|
||||||
|
Assert.Equal("http://awesome.com/?awesome=yodawg", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -52,7 +62,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithParameter("awesome", "cool", "dawg");
|
.WithParameter("awesome", "cool", "dawg");
|
||||||
Assert.Equal("http://awesome.com/?awesome=cool,dawg", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?awesome=cool,dawg", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -60,7 +70,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithParameter("awesome", new List<int>() { 1, 2 }.Cast<object>());
|
.WithParameter("awesome", new List<int>() { 1, 2 }.Cast<object>());
|
||||||
Assert.Equal("http://awesome.com/?awesome=1,2", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?awesome=1,2", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -68,7 +78,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithParameter("awesome");
|
.WithParameter("awesome");
|
||||||
Assert.Equal("http://awesome.com/?awesome", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?awesome", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -76,7 +86,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithFragment("awesome", "cool", "dawg");
|
.WithFragment("awesome", "cool", "dawg");
|
||||||
Assert.Equal("http://awesome.com/#awesome=cool,dawg", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/#awesome=cool,dawg", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -84,7 +94,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithFragment("awesome", new List<int>() { 1, 2 }.Cast<object>());
|
.WithFragment("awesome", new List<int>() { 1, 2 }.Cast<object>());
|
||||||
Assert.Equal("http://awesome.com/#awesome=1,2", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/#awesome=1,2", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -92,7 +102,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithFragment("awesome");
|
.WithFragment("awesome");
|
||||||
Assert.Equal("http://awesome.com/#awesome", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/#awesome", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -107,15 +117,15 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com:80")
|
var url = new UriBuilder("http://awesome.com:80")
|
||||||
.WithoutDefaultPort();
|
.WithoutDefaultPort();
|
||||||
Assert.Equal("http://awesome.com/", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/", url.ToUriString());
|
||||||
|
|
||||||
url = new UriBuilder("http://awesome.com:443")
|
url = new UriBuilder("http://awesome.com:443")
|
||||||
.WithoutDefaultPort();
|
.WithoutDefaultPort();
|
||||||
Assert.Equal("http://awesome.com:443/", url.Uri.ToString());
|
Assert.Equal("http://awesome.com:443/", url.ToUriString());
|
||||||
|
|
||||||
url = new UriBuilder("https://awesome.com:443")
|
url = new UriBuilder("https://awesome.com:443")
|
||||||
.WithoutDefaultPort();
|
.WithoutDefaultPort();
|
||||||
Assert.Equal("https://awesome.com/", url.Uri.ToString());
|
Assert.Equal("https://awesome.com/", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -155,7 +165,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
.WithParameter("awesome", "yodawg")
|
.WithParameter("awesome", "yodawg")
|
||||||
.WithParameter("fun", null)
|
.WithParameter("fun", null)
|
||||||
.WithParameter("cool", string.Empty);
|
.WithParameter("cool", string.Empty);
|
||||||
Assert.Equal("http://awesome.com/?awesome=yodawg&fun&cool", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?awesome=yodawg&fun&cool", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -165,7 +175,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
.WithParameter("awesome", "yodawg")
|
.WithParameter("awesome", "yodawg")
|
||||||
.WithParameter("supg", "no2")
|
.WithParameter("supg", "no2")
|
||||||
.WithParameter("supgf", "no22");
|
.WithParameter("supgf", "no22");
|
||||||
Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?awesome=yodawg&supg=no2&supgf=no22", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -179,7 +189,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
};
|
};
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithParameter(dictionary);
|
.WithParameter(dictionary);
|
||||||
Assert.Equal("http://awesome.com/?yo=dawg&troll=toll&hammer", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/?yo=dawg&troll=toll&hammer", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -189,7 +199,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
.WithFragment("awesome", "yodawg")
|
.WithFragment("awesome", "yodawg")
|
||||||
.WithFragment("fun", null)
|
.WithFragment("fun", null)
|
||||||
.WithFragment("cool", string.Empty);
|
.WithFragment("cool", string.Empty);
|
||||||
Assert.Equal("http://awesome.com/#awesome=yodawg&fun&cool", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/#awesome=yodawg&fun&cool", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -199,7 +209,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
.WithFragment("awesome", "yodawg")
|
.WithFragment("awesome", "yodawg")
|
||||||
.WithFragment("supg", "no2")
|
.WithFragment("supg", "no2")
|
||||||
.WithFragment("supgf", "no22");
|
.WithFragment("supgf", "no22");
|
||||||
Assert.Equal("http://awesome.com/#awesome=yodawg&supg=no2&supgf=no22", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/#awesome=yodawg&supg=no2&supgf=no22", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -213,7 +223,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
};
|
};
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithFragment(dictionary);
|
.WithFragment(dictionary);
|
||||||
Assert.Equal("http://awesome.com/#yo=dawg&troll=toll&hammer", url.Uri.ToString());
|
Assert.Equal("http://awesome.com/#yo=dawg&troll=toll&hammer", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -221,15 +231,8 @@ namespace FluentUriBuilder.Tests
|
|||||||
{
|
{
|
||||||
var url = new UriBuilder("http://awesome.com")
|
var url = new UriBuilder("http://awesome.com")
|
||||||
.WithParameter("yo", "dawg<");
|
.WithParameter("yo", "dawg<");
|
||||||
Assert.Equal("http://awesome.com/?yo=dawg%3C", url.ToEscapeString());
|
Assert.Equal("http://awesome.com/?yo=dawg<", url.ToUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void TestToEscapedDataString()
|
|
||||||
{
|
|
||||||
var url = new UriBuilder("http://awesome.com")
|
|
||||||
.WithParameter("yo", "dawg<");
|
|
||||||
Assert.Equal("http%3A%2F%2Fawesome.com%2F%3Fyo%3Ddawg%3C", url.ToEscapeDataString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithScheme(null));
|
Assert.Throws<ArgumentNullException>(() => tstObj.WithScheme(null));
|
||||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithHost(null));
|
Assert.Throws<ArgumentNullException>(() => tstObj.WithHost(null));
|
||||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter((IEnumerable<KeyValuePair<string, string>>)null));
|
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter((IEnumerable<KeyValuePair<string, string>>)null));
|
||||||
|
Assert.Throws<ArgumentNullException>(() => tstObj.WithParameter((IEnumerable<(string, string)>)null));
|
||||||
Assert.Throws<ArgumentNullException>(() => tstObj.WithFragment(fragmentDictionary: null));
|
Assert.Throws<ArgumentNullException>(() => tstObj.WithFragment(fragmentDictionary: null));
|
||||||
Assert.Throws<ArgumentOutOfRangeException>(() => tstObj.WithPort(-1));
|
Assert.Throws<ArgumentOutOfRangeException>(() => tstObj.WithPort(-1));
|
||||||
UriBuilder nullPtr = null;
|
UriBuilder nullPtr = null;
|
||||||
@@ -32,6 +33,7 @@ namespace FluentUriBuilder.Tests
|
|||||||
Assert.Throws<ArgumentNullException>(() => nullPtr.WithPort(1));
|
Assert.Throws<ArgumentNullException>(() => nullPtr.WithPort(1));
|
||||||
Assert.Throws<ArgumentNullException>(() => nullPtr.WithHost("yo"));
|
Assert.Throws<ArgumentNullException>(() => nullPtr.WithHost("yo"));
|
||||||
Assert.Throws<ArgumentNullException>(() => nullPtr.UseHttps());
|
Assert.Throws<ArgumentNullException>(() => nullPtr.UseHttps());
|
||||||
|
Assert.Throws<ArgumentNullException>(() => nullPtr.WithParameter((IEnumerable<(string, string)>)null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data.SqlTypes;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace System
|
namespace System
|
||||||
{
|
{
|
||||||
public static class TerribleDevUriExtensions
|
public static partial class TerribleDevUriExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 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.
|
/// 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.
|
||||||
@@ -266,24 +269,38 @@ namespace System
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string ToEscapeDataString(this UriBuilder bld) => Uri.EscapeDataString(bld.Uri.ToString());
|
public static string ToEscapeDataString(this UriBuilder bld) => Uri.EscapeDataString(bld.Uri.ToString());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the Uri string
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bld"></param>
|
||||||
|
/// <exception cref="ArgumentNullException"></exception>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string ToUriString(this UriBuilder bld) => bld.Uri.ToString();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Appends x-www-form-urlencoded key and valuesEnum into initialValue.
|
/// Appends x-www-form-urlencoded key and valuesEnum into initialValue.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="ArgumentNullException"></exception>
|
/// <exception cref="ArgumentNullException"></exception>
|
||||||
private static string AppendKeyValue(this string intitialValue, string key, IEnumerable<object> valuesEnum)
|
private static string AppendKeyValue(this string intitialValue, string key, IEnumerable<object> valuesEnum)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder($"{intitialValue}{key}");
|
var encodedKey = Uri.EscapeDataString(key);
|
||||||
var validValueHit = false;
|
|
||||||
foreach (var value in valuesEnum)
|
var sb = new StringBuilder($"{intitialValue}{encodedKey}");
|
||||||
|
|
||||||
|
var Values = (
|
||||||
|
from x in valuesEnum
|
||||||
|
let v = x?.ToString()
|
||||||
|
where !string.IsNullOrWhiteSpace(v)
|
||||||
|
select Uri.EscapeDataString(v)
|
||||||
|
).ToArray();
|
||||||
|
|
||||||
|
if (Values.Length > 0)
|
||||||
{
|
{
|
||||||
var toSValue = value?.ToString();
|
sb.Append("=");
|
||||||
if (string.IsNullOrWhiteSpace(toSValue)) continue;
|
sb.Append(string.Join(",", Values));
|
||||||
// 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(',');
|
|
||||||
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
32
src/UriBuilder.Fluent/TerribleDevUriExtensionsTuple.cs
Normal file
32
src/UriBuilder.Fluent/TerribleDevUriExtensionsTuple.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace System
|
||||||
|
{
|
||||||
|
public static partial class TerribleDevUriExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Appends query strings from dictionary
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bld"></param>
|
||||||
|
/// <param name="parameterDictionary"></param>
|
||||||
|
/// <exception cref="ArgumentNullException"></exception>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static UriBuilder WithParameter(this UriBuilder bld, IEnumerable<(string, string)> parameterDictionary)
|
||||||
|
{
|
||||||
|
if (bld == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(bld));
|
||||||
|
}
|
||||||
|
if (parameterDictionary == null) throw new ArgumentNullException(nameof(parameterDictionary));
|
||||||
|
foreach (var item in parameterDictionary)
|
||||||
|
{
|
||||||
|
bld.WithParameter(item.Item1, item.Item2);
|
||||||
|
}
|
||||||
|
return bld;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\..\Readme.md" Pack="true" PackagePath="\" />
|
<None Include="..\..\Readme.md" Pack="true" PackagePath="\" />
|
||||||
<None Include="..\..\UriBulider.Fluent.png" Pack="true" PackagePath="\" />
|
<None Include="..\..\UriBulider.Fluent.png" Pack="true" PackagePath="\" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user