Skip to content

Add support of System.Uri to UrlAttribute #114992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ public UrlAttribute()

public override bool IsValid(object? value)
{
if (value == null)
switch (value)
{
return true;
case Uri valueAsUri:
{
return valueAsUri.Scheme == Uri.UriSchemeHttp
|| valueAsUri.Scheme == Uri.UriSchemeHttps
|| valueAsUri.Scheme == Uri.UriSchemeFtp;
}
case string valueAsString:
{
return valueAsString.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| valueAsString.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
|| valueAsString.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase);
}
case null:
{
return true;
}
default:
{
return false;
}
}

return value is string valueAsString &&
(valueAsString.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| valueAsString.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
|| valueAsString.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ protected override IEnumerable<TestCase> ValidValues()
yield return new TestCase(new UrlAttribute(), "http://foo.bar");
yield return new TestCase(new UrlAttribute(), "https://foo.bar");
yield return new TestCase(new UrlAttribute(), "ftp://foo.bar");
yield return new TestCase(new UrlAttribute(), new Uri("http://foo.bar"));
yield return new TestCase(new UrlAttribute(), new Uri("https://foo.bar"));
yield return new TestCase(new UrlAttribute(), new Uri("ftp://foo.bar"));
}

protected override IEnumerable<TestCase> InvalidValues()
{
yield return new TestCase(new UrlAttribute(), "file:///foo.bar");
yield return new TestCase(new UrlAttribute(), "foo.png");
yield return new TestCase(new UrlAttribute(), new object());
yield return new TestCase(new UrlAttribute(), new Uri("file:///foo.bar"));
yield return new TestCase(new UrlAttribute(), new Uri("//foo.png"));
}

[Fact]
Expand Down
Loading