Created
August 19, 2020 18:03
-
-
Save irontoby/7455c7956e1fcbaf421556e8ccfbe1ee to your computer and use it in GitHub Desktop.
Test whether Flurl + Uri combine URLs as expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Flurl; | |
using Xunit; | |
namespace FlurlVsUri | |
{ | |
public class UrlCombineTests | |
{ | |
private const string HostWithSlash = "https://hostname.com/"; | |
private const string HostWithoutSlash = "https://hostname.com"; | |
private const string HostAndBaseWithSlash = "https://hostname.com/v2/"; | |
private const string HostAndBaseWithoutSlash = "https://hostname.com/v2"; | |
private const string RelWithSlash = "/api/foo"; | |
private const string RelWithoutSlash = "api/foo"; | |
private const string ExpectedWithBase = "https://hostname.com/v2/api/foo"; | |
private const string ExpectedWithoutBase = "https://hostname.com/api/foo"; | |
[Theory] | |
[InlineData(HostWithSlash, RelWithSlash)] | |
[InlineData(HostWithoutSlash, RelWithSlash)] | |
[InlineData(HostWithSlash, RelWithoutSlash)] | |
[InlineData(HostWithoutSlash, RelWithoutSlash)] | |
public void FlurlCombineWithoutBase(string first, string more) | |
{ | |
var url = Url.Combine(first, more); | |
Assert.Equal(ExpectedWithoutBase, url); | |
} | |
[Theory] | |
[InlineData(HostAndBaseWithSlash, RelWithSlash)] | |
[InlineData(HostAndBaseWithoutSlash, RelWithSlash)] | |
[InlineData(HostAndBaseWithSlash, RelWithoutSlash)] | |
[InlineData(HostAndBaseWithoutSlash, RelWithoutSlash)] | |
public void FlurlCombineWithBase(string first, string more) | |
{ | |
var url = Url.Combine(first, more); | |
Assert.Equal(ExpectedWithBase, url); | |
} | |
[Theory] | |
[InlineData(HostWithSlash, RelWithSlash)] | |
[InlineData(HostWithoutSlash, RelWithSlash)] | |
[InlineData(HostWithSlash, RelWithoutSlash)] | |
[InlineData(HostWithoutSlash, RelWithoutSlash)] | |
public void UrlCombineWithoutBaseUsingString(string first, string more) | |
{ | |
var url = CombineWithUrlUsingString(first, more); | |
Assert.Equal(ExpectedWithoutBase, url); | |
} | |
[Theory] | |
[InlineData(HostAndBaseWithSlash, RelWithSlash)] | |
[InlineData(HostAndBaseWithoutSlash, RelWithSlash)] | |
[InlineData(HostAndBaseWithSlash, RelWithoutSlash)] | |
[InlineData(HostAndBaseWithoutSlash, RelWithoutSlash)] | |
public void UrlCombineWithBaseUsingString(string first, string more) | |
{ | |
var url = CombineWithUrlUsingString(first, more); | |
Assert.Equal(ExpectedWithBase, url); | |
} | |
[Theory] | |
[InlineData(HostWithSlash, RelWithSlash)] | |
[InlineData(HostWithoutSlash, RelWithSlash)] | |
[InlineData(HostWithSlash, RelWithoutSlash)] | |
[InlineData(HostWithoutSlash, RelWithoutSlash)] | |
public void UrlCombineWithoutBaseUsingUrl(string first, string more) | |
{ | |
var url = CombineWithUrlUsingUrl(first, more); | |
Assert.Equal(ExpectedWithoutBase, url); | |
} | |
[Theory] | |
[InlineData(HostAndBaseWithSlash, RelWithSlash)] | |
[InlineData(HostAndBaseWithoutSlash, RelWithSlash)] | |
[InlineData(HostAndBaseWithSlash, RelWithoutSlash)] | |
[InlineData(HostAndBaseWithoutSlash, RelWithoutSlash)] | |
public void UrlCombineWithBaseUsingUrl(string first, string more) | |
{ | |
var url = CombineWithUrlUsingUrl(first, more); | |
Assert.Equal(ExpectedWithBase, url); | |
} | |
private string CombineWithUrlUsingString(string first, string more) | |
{ | |
var baseUri = new Uri(first); | |
var url = new Uri(baseUri, more); | |
return url.ToString(); | |
} | |
private string CombineWithUrlUsingUrl(string first, string more) | |
{ | |
var baseUri = new Uri(first); | |
var relUri = new Uri(more, UriKind.RelativeOrAbsolute); | |
var url = new Uri(baseUri, relUri); | |
return url.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment