Skip to content

Commit

Permalink
#406 Url ctor that takes Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed Jan 20, 2019
1 parent b051bd9 commit d90e27c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
11 changes: 10 additions & 1 deletion Test/Flurl.Test/UrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public class UrlBuilderTests
Assert.AreEqual("foo", url.Fragment);
}

[Test]
public void can_construct_from_uri() {
var s = "http://www.mysite.com/more?x=1&y=2#foo";
var uri = new Uri(s);
var url = new Url(uri);
Assert.AreEqual(s, url.ToString());
}

[Test]
public void can_parse_query_params() {
var q = new Url("http://www.mysite.com/more?x=1&y=2&z=3&y=4&abc&xyz&foo=&=bar&y=6").QueryParams;
Expand Down Expand Up @@ -199,7 +207,8 @@ public class UrlBuilderTests

[Test]
public void constructor_requires_nonnull_arg() {
Assert.Throws<ArgumentNullException>(() => new Url(null));
Assert.Throws<ArgumentNullException>(() => new Url((string)null));
Assert.Throws<ArgumentNullException>(() => new Url((Uri)null));
}

[Test]
Expand Down
30 changes: 23 additions & 7 deletions src/Flurl/Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@ public class Url
/// </summary>
public QueryParamCollection QueryParams { get; private set; }

/// <summary>
/// Constructs a Url object from a string.
/// </summary>
/// <param name="baseUrl">The URL to use as a starting point (required)</param>
/// <exception cref="ArgumentNullException"><paramref name="baseUrl"/> is <see langword="null" />.</exception>
public Url(string baseUrl) {
/// <summary>
/// Constructs a Url object from a string.
/// </summary>
/// <param name="baseUrl">The URL to use as a starting point (required)</param>
/// <exception cref="ArgumentNullException"><paramref name="baseUrl"/> is <see langword="null" />.</exception>
public Url(string baseUrl) {
if (baseUrl == null)
throw new ArgumentNullException(nameof(baseUrl));

var parts = baseUrl.SplitOnFirstOccurence('#');
Parse(baseUrl);
}

/// <summary>
/// Constructs a Url object from a System.Uri.
/// </summary>
/// <param name="uri">The System.Uri (required)</param>
/// <exception cref="ArgumentNullException"><paramref name="uri"/> is <see langword="null" />.</exception>
public Url(Uri uri) {
if (uri == null)
throw new ArgumentNullException(nameof(uri));

Parse(uri.ToString());
}

private void Parse(string url) {
var parts = url.SplitOnFirstOccurence('#');
Fragment = (parts.Length == 2) ? parts[1] : "";
parts = parts[0].SplitOnFirstOccurence('?');
Query = (parts.Length == 2) ? parts[1] : "";
Expand Down

0 comments on commit d90e27c

Please sign in to comment.