From 25b04c4a97fc7692b739b0e66d917176922d60c3 Mon Sep 17 00:00:00 2001 From: Yeba <31899118+yebafan@users.noreply.github.com> Date: Wed, 28 Apr 2021 15:48:15 +0200 Subject: [PATCH] misc: Remove URI check from EmbedBuilder (#1778) `Uri.IsWellFormedUriString()` doesn't return the expected result for specific urls, removed until the DotNet team actually resolves it ( https://github.com/dotnet/runtime/issues/21626 ) --- .../Entities/Messages/EmbedBuilder.cs | 56 ++----------------- .../Extensions/StringExtensions.cs | 10 ---- .../EmbedBuilderTests.cs | 54 ------------------ 3 files changed, 6 insertions(+), 114 deletions(-) delete mode 100644 src/Discord.Net.Core/Extensions/StringExtensions.cs diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs index 555fd95df..f1238ddcf 100644 --- a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -12,7 +12,6 @@ namespace Discord { private string _title; private string _description; - private string _url; private EmbedImage? _image; private EmbedThumbnail? _thumbnail; private List _fields; @@ -70,26 +69,14 @@ namespace Discord /// Gets or sets the URL of an . /// Url is not a well-formed . /// The URL of the embed. - public string Url - { - get => _url; - set - { - if (!value.IsNullOrUri()) throw new ArgumentException(message: "Url must be a well-formed URI.", paramName: nameof(Url)); - _url = value; - } - } + public string Url { get; set; } /// Gets or sets the thumbnail URL of an . /// Url is not a well-formed . /// The thumbnail URL of the embed. public string ThumbnailUrl { get => _thumbnail?.Url; - set - { - if (!value.IsNullOrUri()) throw new ArgumentException(message: "Url must be a well-formed URI.", paramName: nameof(ThumbnailUrl)); - _thumbnail = new EmbedThumbnail(value, null, null, null); - } + set => _thumbnail = new EmbedThumbnail(value, null, null, null); } /// Gets or sets the image URL of an . /// Url is not a well-formed . @@ -97,11 +84,7 @@ namespace Discord public string ImageUrl { get => _image?.Url; - set - { - if (!value.IsNullOrUri()) throw new ArgumentException(message: "Url must be a well-formed URI.", paramName: nameof(ImageUrl)); - _image = new EmbedImage(value, null, null, null); - } + set => _image = new EmbedImage(value, null, null, null); } /// Gets or sets the list of of an . @@ -553,8 +536,6 @@ namespace Discord public class EmbedAuthorBuilder { private string _name; - private string _url; - private string _iconUrl; /// /// Gets the maximum author name length allowed by Discord. /// @@ -585,15 +566,7 @@ namespace Discord /// /// The URL of the author field. /// - public string Url - { - get => _url; - set - { - if (!value.IsNullOrUri()) throw new ArgumentException(message: "Url must be a well-formed URI.", paramName: nameof(Url)); - _url = value; - } - } + public string Url { get; set; } /// /// Gets or sets the icon URL of the author field. /// @@ -601,15 +574,7 @@ namespace Discord /// /// The icon URL of the author field. /// - public string IconUrl - { - get => _iconUrl; - set - { - if (!value.IsNullOrUri()) throw new ArgumentException(message: "Url must be a well-formed URI.", paramName: nameof(IconUrl)); - _iconUrl = value; - } - } + public string IconUrl { get; set; } /// /// Sets the name of the author field. @@ -671,7 +636,6 @@ namespace Discord public class EmbedFooterBuilder { private string _text; - private string _iconUrl; /// /// Gets the maximum footer length allowed by Discord. @@ -703,15 +667,7 @@ namespace Discord /// /// The icon URL of the footer field. /// - public string IconUrl - { - get => _iconUrl; - set - { - if (!value.IsNullOrUri()) throw new ArgumentException(message: "Url must be a well-formed URI.", paramName: nameof(IconUrl)); - _iconUrl = value; - } - } + public string IconUrl { get; set; } /// /// Sets the name of the footer field. diff --git a/src/Discord.Net.Core/Extensions/StringExtensions.cs b/src/Discord.Net.Core/Extensions/StringExtensions.cs deleted file mode 100644 index c0ebb2626..000000000 --- a/src/Discord.Net.Core/Extensions/StringExtensions.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace Discord -{ - internal static class StringExtensions - { - public static bool IsNullOrUri(this string url) => - string.IsNullOrEmpty(url) || Uri.IsWellFormedUriString(url, UriKind.Absolute); - } -} diff --git a/test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs b/test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs index 12ec1a0bd..6cfdc83b2 100644 --- a/test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs +++ b/test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs @@ -190,42 +190,6 @@ namespace Discord Assert.Equal(result.ThumbnailUrl, url); } - /// - /// Tests that invalid urls throw an . - /// - /// The url to set. - [Theory] - [InlineData(" ")] - [InlineData("not a url")] - public void Url_Invalid(string url) - { - Assert.Throws(() - => new EmbedBuilder() - .WithUrl(url)); - Assert.Throws(() - => new EmbedBuilder() - .WithImageUrl(url)); - Assert.Throws(() - => new EmbedBuilder() - .WithThumbnailUrl(url)); - - Assert.Throws(() => - { - var b = new EmbedBuilder(); - b.Url = url; - }); - Assert.Throws(() => - { - var b = new EmbedBuilder(); - b.ImageUrl = url; - }); - Assert.Throws(() => - { - var b = new EmbedBuilder(); - b.ThumbnailUrl = url; - }); - } - /// /// Tests the value of the property when there are no fields set. /// @@ -343,24 +307,6 @@ namespace Discord Assert.Equal(name, footer.Text); } /// - /// Tests that invalid URLs throw an . - /// - [Fact] - public void EmbedFooterBuilder_InvalidURL() - { - IEnumerable InvalidUrls() - { - yield return "not a url"; - } - foreach (var url in InvalidUrls()) - { - Assert.Throws(() => - { - new EmbedFooterBuilder().WithIconUrl(url); - }); - } - } - /// /// Tests that invalid text throws an . /// [Fact]