5 Commits

Author SHA1 Message Date
khyteang-lim
9b1d037a88 Added ToEscapeString() and ToEscapeDataString() (#11)
* Added ToEscapeString() and ToEscapeDataString(), added desciption in readme, and added unit tests

* added changes from PR feedback
2017-01-30 17:42:11 -05:00
Tommy Parnell
be92d91ef6 readd 461 2016-12-13 13:47:17 -05:00
Tommy Parnell
a1527c058c add path and query 2016-12-13 13:44:06 -05:00
Tommy Parnell
908d0e2433 target 45 2016-12-13 10:57:40 -05:00
Tommy Parnell
e9f3e5ed8b target 40 2016-12-06 07:48:41 -05:00
7 changed files with 109 additions and 79 deletions

View File

@@ -6,6 +6,6 @@ if($env:APPVEYOR_REPO_TAG -eq "true")
nuget install OpenCover -Version 4.6.519 -OutputDirectory tools nuget install OpenCover -Version 4.6.519 -OutputDirectory tools
nuget install coveralls.net -Version 0.7.0 -OutputDirectory tools nuget install coveralls.net -Version 0.7.0 -OutputDirectory tools
.\tools\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:" test "".\src\UriBuilder.Fluent.UnitTests"" -f net462" -register:user -filter:"+[UriBuilder*]* -[*Tests]*" -returntargetcode -output:opencover_results.xml .\tools\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:" test "".\src\UriBuilder.Fluent.UnitTests"" -f net461" -register:user -filter:"+[UriBuilder*]* -[*Tests]*" -returntargetcode -output:opencover_results.xml
.\tools\coveralls.net.0.7.0\tools\csmacnz.Coveralls.exe --opencover -i .\opencover_results.xml .\tools\coveralls.net.0.7.0\tools\csmacnz.Coveralls.exe --opencover -i .\opencover_results.xml

View File

@@ -1,10 +1,15 @@
## UriBuilder.Fluent # UriBuilder.Fluent
[![Coverage Status](https://coveralls.io/repos/github/TerribleDev/UriBuilder.Fluent/badge.svg)](https://coveralls.io/github/TerribleDev/UriBuilder.Fluent) [![Build status](https://ci.appveyor.com/api/projects/status/cp704w3bgaerufxm/branch/master?svg=true)](https://ci.appveyor.com/project/tparnell8/uribuilder-fluent/branch/master) [![Coverage Status](https://coveralls.io/repos/github/TerribleDev/UriBuilder.Fluent/badge.svg)](https://coveralls.io/github/TerribleDev/UriBuilder.Fluent) [![Build status](https://ci.appveyor.com/api/projects/status/cp704w3bgaerufxm/branch/master?svg=true)](https://ci.appveyor.com/project/tparnell8/uribuilder-fluent/branch/master)
This places extension methods over System.UriBuilder to help deal with query string parameters, and create more of a fluent interface. Unlike other projects, this NetStandardLibrary compliant package builds ontop of trusty UriBuilder, does not use custom Uri generators, or have outside dependencies. Unit tests continue to be a first class citizen! This places extension methods over System.UriBuilder to help deal with query string parameters, and create more of a fluent interface. Unlike other projects, this NetStandardLibrary compliant package builds ontop of trusty UriBuilder, does not use custom Uri generators, or have outside dependencies. Unit tests continue to be a first class citizen!
This lets you do things like ## Getting started
Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you!
## Code Example
This lets you do things like
```csharp ```csharp
@@ -16,8 +21,6 @@ new UriBuilder()
.UseHttps() .UseHttps()
.ToString() .ToString()
``` ```
result: `https://awesome.com/seg?awesome=yodawg&fun=cool,yay` result: `https://awesome.com/seg?awesome=yodawg&fun=cool,yay`
@@ -35,16 +38,44 @@ result: `https://awesome.com/yo?id=5`
you can even pass a dictionary of parameters you can even pass a dictionary of parameters
```csharp ```csharp
var dictionary = new Dictionary<string, string>()
{ var dictionary = new Dictionary<string, string>()
["yo"] = "dawg" {
}; ["yo"] = "dawg"
new UriBuilder("http://awesome.com") };
.WithParameter(dictionary);
http://awesome.com/?yo=dawg new UriBuilder("http://awesome.com")
.WithParameter(dictionary);
```
result: `http://awesome.com/?yo=dawg`
### ToEscapeString() vs. ToEscapeDataString()
There are two extension methods for performing Uri encoding.
#### ToEscapeString()
Performs encoding only on the Uri query string and returns the Uri as a string.
```csharp
var uri = new UriBuilder("https://awesome.com")
.WithParameter("yo", "dawg<")
.ToEscapeString();
``` ```
## Getting started result: `http://awesome.com/?yo=dawg%3C`
Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you! #### ToEscapeDataString()
Performs encoding on the whole Uri and returns the Uri as a string.
```csharp
var uri = new UriBuilder("https://awesome.com")
.WithParameter("yo", "dawg<")
.ToEscapeDataString();
```
result: `http%3A%2F%2Fawesome.com%2F%3Fyo%3Ddawg%3C`

View File

@@ -12,16 +12,15 @@ init:
before_build: before_build:
- ps: invoke-webrequest https://go.microsoft.com/fwlink/?LinkID=827524 -OutFile core.exe - ps: invoke-webrequest https://go.microsoft.com/fwlink/?LinkID=827524 -OutFile core.exe
- ps: .\core.exe /install /quiet /norestart - ps: .\core.exe /install /quiet /norestart
- ps: iwr -Uri https://download.microsoft.com/download/E/F/D/EFD52638-B804-4865-BB57-47F4B9C80269/NDP462-DevPack-KB3151934-ENU.exe -OutFile net452.exe
- ps: start-process .\net452.exe -Wait '/install /quiet /norestart'
build_script: build_script:
- ps: dotnet restore - ps: dotnet restore
- ps: dotnet build -c Release .\src\UriBuilder.Fluent - ps: dotnet build -c Release .\src\UriBuilder.Fluent
- ps: dotnet publish -c Release .\src\UriBuilder.Fluent.UnitTests - ps: dotnet build -c Release .\src\UriBuilder.Fluent.UnitTests
test_script: test_script:
- ps: dotnet test -c Release .\src\UriBuilder.Fluent.UnitTests
- ps: .\Coverage.ps1 - ps: .\Coverage.ps1
- ps: .\update-projectjson.ps1 - ps: .\update-projectjson.ps1
- ps: dotnet restore - ps: dotnet restore

View File

@@ -35,6 +35,17 @@ namespace FluentUriBuilder.Tests
.WithParameter("awesome", "yodawg"); .WithParameter("awesome", "yodawg");
Assert.Equal("http://awesome.com/?awesome=yodawg", url.Uri.ToString()); Assert.Equal("http://awesome.com/?awesome=yodawg", url.Uri.ToString());
} }
[Fact]
public void PathAndQuery()
{
var url = new UriBuilder().WithPathSegment("/awesome/v1/").WithParameter("awesome", "cool").PathAndQuery();
Assert.Equal("/awesome/v1/?awesome=cool", url);
url = new UriBuilder().WithPathSegment("/awesome/v1").WithParameter("awesome", "cool").PathAndQuery();
Assert.Equal("/awesome/v1?awesome=cool", url);
url = new UriBuilder().WithPathSegment("/awesome/v1").PathAndQuery();
Assert.Equal("/awesome/v1", url);
}
[Fact] [Fact]
public void TestAddParameterArray() public void TestAddParameterArray()
@@ -116,16 +127,6 @@ namespace FluentUriBuilder.Tests
.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.Uri.ToString());
} }
[Fact]
public void TestSerializedParameters()
{
var url = new UriBuilder("http://awesome.com")
.WithSerializedObject(new {yo="dawg"});
Assert.Equal("http://awesome.com/?yo=dawg", url.Uri.ToString());
url = new UriBuilder("http://awesome.com")
.WithSerializedObject(new {yo=""});
Assert.Equal("http://awesome.com/?yo", url.Uri.ToString());
}
[Fact] [Fact]
public void AddDictOfParams() public void AddDictOfParams()
@@ -140,5 +141,21 @@ namespace FluentUriBuilder.Tests
.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.Uri.ToString());
} }
[Fact]
public void TestToEscapedString()
{
var url = new UriBuilder("http://awesome.com")
.WithParameter("yo","dawg<");
Assert.Equal("http://awesome.com/?yo=dawg%3C", url.ToEscapeString());
}
[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());
}
} }
} }

View File

@@ -9,8 +9,7 @@
}, },
"frameworks": { "frameworks": {
"net462": { "net461":{},
},
"netcoreapp1.0": { "netcoreapp1.0": {
"dependencies": { "dependencies": {
"Microsoft.NETCore.App": { "Microsoft.NETCore.App": {

View File

@@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -18,7 +17,6 @@ namespace System
/// <returns></returns> /// <returns></returns>
public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values); public static UriBuilder WithParameter(this UriBuilder bld, string key, params string[] values) => bld.WithParameter(key, valuesEnum: values);
public static UriBuilder WithParameter(this UriBuilder bld, string key, object value) => bld.WithParameter(key, new object[1] { value });
/// <summary> /// <summary>
/// Appends query strings from dictionary /// Appends query strings from dictionary
/// </summary> /// </summary>
@@ -129,6 +127,8 @@ namespace System
return bld; return bld;
} }
public static string PathAndQuery(this UriBuilder bld) => (bld.Path + bld.Query);
/// <summary> /// <summary>
/// Use Https? /// Use Https?
/// </summary> /// </summary>
@@ -141,19 +141,19 @@ namespace System
return bld; return bld;
} }
#if (netstandard15) /// <summary>
/// Escape Url query string
/// </summary>
/// <param name="bld"></param>
/// <returns></returns>
public static string ToEscapeString(this UriBuilder bld) => Uri.EscapeUriString(bld.Uri.ToString());
public static UriBuilder WithSerializedObject(this UriBuilder bld, object serialize) /// <summary>
{ /// Escape the whole Url string
var types = serialize.GetType().GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty); /// </summary>
foreach(var type in types) /// <param name="bld"></param>
{ /// <returns></returns>
bld.WithParameter(type.Name, type.GetValue(serialize, null)); public static string ToEscapeDataString(this UriBuilder bld) => Uri.EscapeDataString(bld.Uri.ToString());
}
return bld;
}
#endif
} }
} }

View File

@@ -1,48 +1,32 @@
{ {
"version": "1.0.0-*", "version": "1.0.0-*",
"packOptions": { "packOptions": {
"owners": [ "owners": [
"Tommy Parnell" "Tommy Parnell"
], ],
"repository": { "projectUrl": "https://github.com/TerribleDev/UriBuilder.Fluent",
"url": "https://github.com/TerribleDev/UriBuilder.Fluent" "summary": "Fluent extensions for UriBuilder",
}, "tags": [
"summary": "Fluent extensions for UriBuilder", "Url building",
"tags": [ "Uri",
"Url building", "Uri building",
"Uri", "fluent",
"Uri building", "extension"
"fluent", ]
"extension"
]
}, },
"authors": [ "authors": [
"Tommy Parnell" "Tommy Parnell"
], ],
"frameworks": { "frameworks": {
"netstandard1.1": { "netstandard1.1": {
"buildOptions": { "imports": "dnxcore50",
"define": [ "netstandard11" ] "dependencies": {
}, "NETStandard.Library": "1.6.0"
"dependencies": {
"NETStandard.Library": "1.6.0"
} }
}, },
"netstandard1.5": { "net40": {
"dependencies": { "imports": "dnxcore50"
"NETStandard.Library": "1.6.0"
},
"buildOptions": {
"define": [ "netstandard15" ]
}
}, },
"net45":{ "net45":{}
"buildOptions": {
"define": [ "netstandard15" ]
},
"frameworkAssemblies": {
"System.Runtime":"4.0.0"
}
}
} }
} }