Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/libraries/System.Private.Uri/src/System/UriExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre

return GetException(err);
}

if (uriKind == UriKind.Relative)
{
// Here we know that we can create an absolute Uri, but the user has requested only a relative one
return GetException(ParsingError.CannotCreateRelative);
}
}
else
{
Expand All @@ -148,12 +142,12 @@ private void CreateThis(string? uri, bool dontEscape, UriKind uriKind, in UriCre
// we use = here to clear all parsing flags for a uri that we think is invalid.
_flags = Flags.UserDrivenParsing | (_flags & Flags.UserEscaped);
}
else if (uriKind == UriKind.Relative)
{
// Here we know that custom parser can create an absolute Uri, but the user has requested only a
// relative one
return GetException(ParsingError.CannotCreateRelative);
}
}

if (uriKind == UriKind.Relative)
{
// Here we know that we can create an absolute Uri, but the user has requested only a relative one
return GetException(ParsingError.CannotCreateRelative);
}

if (hasUnicode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,5 @@ public void DisableCanonicalization_GetComponentsThrowsForPathAndQuery(UriFormat
Assert.Throws<InvalidOperationException>(() => uri.GetComponents(UriComponents.PathAndQuery, format));
Assert.Throws<InvalidOperationException>(() => uri.GetComponents(UriComponents.AbsoluteUri, format));
}


private sealed class CustomUriParser : UriParser { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -576,5 +576,50 @@ public static void NetTcpStyleUriParser_ctor()
new NetTcpStyleUriParser();
}
#endregion UriParser template tests

[Theory]
[InlineData(UriKind.Absolute, true)]
[InlineData(UriKind.Absolute, false)]
[InlineData(UriKind.RelativeOrAbsolute, true)]
[InlineData(UriKind.RelativeOrAbsolute, false)]
[InlineData(UriKind.Relative, true)]
[InlineData(UriKind.Relative, false)]
public static void CustomParserCanRecoverOnInvalidUri(UriKind uriKind, bool recover)
{
string scheme = $"custom-recover-on-invalid-{uriKind}-{recover}";
UriParser.Register(new CustomParser_RecoversOnInvalidUri(recover), scheme, -1);

var uriString = recover ? $"{scheme}:not a valid host" : $"{scheme}://host";

if (uriKind == UriKind.Relative)
{
Assert.Throws<UriFormatException>(() => new Uri(uriString, uriKind));
Assert.False(Uri.TryCreate(uriString, uriKind, out _));
}
else
{
var uri1 = new Uri(uriString, uriKind);
Assert.True(Uri.TryCreate(uriString, uriKind, out Uri? uri2));
Assert.Same(uri1.OriginalString, uri2.OriginalString);
Assert.Equal("foo", uri1.Host);
Assert.Equal("foo", uri2.Host);
}
}

private sealed class CustomParser_RecoversOnInvalidUri(bool recover) : GenericUriParser(GenericUriParserOptions.Default)
{
protected override void InitializeAndValidate(Uri uri, out UriFormatException? parsingError)
{
base.InitializeAndValidate(uri, out parsingError);

if (recover)
{
Assert.NotNull(parsingError);
parsingError = null;
}
}

protected override string GetComponents(Uri uri, UriComponents components, UriFormat format) => "foo";
}
}
}
Loading