Last active
January 27, 2017 02:01
-
-
Save piccaso/414a72f580ba5f75d0fc8c2f4298facc to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Linq; | |
using FluentAssertions; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace UriAppend | |
{ | |
public static class UriExtensions | |
{ | |
public static Uri Append(this Uri baseUri, string relativeUrl) => new Uri(baseUri, relativeUrl); | |
public static Uri Append(this Uri baseUri, params string[] relativeUrls) => relativeUrls.Aggregate(baseUri, (current, rel) => current?.Append(rel)); | |
} | |
[TestClass] | |
public class Tests | |
{ | |
[TestMethod] | |
public void Test() | |
{ | |
new Uri("http://loclahost/") | |
.Append("x/", "y/", "./z", "?p=1").ShouldBe("http://loclahost/x/y/z?p=1") | |
.Append("#hash").ShouldBe("http://loclahost/x/y/z?p=1#hash") | |
.Append("/moo").ShouldBe("http://loclahost/moo") | |
.Append("//host/", "dir/", "file", "?par=1", "#h").ShouldBe("http://host/dir/file?par=1#h") | |
.Invoking(u => u.Append("///")).ShouldThrow<UriFormatException>(); | |
new Uri("http://test.com/mydirectory/").Append("helloworld.aspx") | |
.ShouldBe("http://test.com/mydirectory/helloworld.aspx"); | |
new Uri("https://xx.yy").Append("//localhost/", "d1/", "../d2/../d3/").ShouldBe("https://localhost/d3/") | |
.Append(".").ShouldBe("https://localhost/d3/") | |
.Append("./x").ShouldBe("https://localhost/d3/x") | |
.Append("//h/", "s/../x", "?x=1", "?y=2").ShouldBe("https://h/x?y=2"); | |
} | |
} | |
public static class TestHelperExt | |
{ | |
public static Uri ShouldBe(this Uri t, string what) | |
{ | |
t.ShouldBeEquivalentTo(what); | |
return t; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment