Browse Source

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 )
tags/2.4.0
Yeba GitHub 4 years ago
parent
commit
25b04c4a97
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 114 deletions
  1. +6
    -50
      src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs
  2. +0
    -10
      src/Discord.Net.Core/Extensions/StringExtensions.cs
  3. +0
    -54
      test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs

+ 6
- 50
src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs View File

@@ -12,7 +12,6 @@ namespace Discord
{
private string _title;
private string _description;
private string _url;
private EmbedImage? _image;
private EmbedThumbnail? _thumbnail;
private List<EmbedFieldBuilder> _fields;
@@ -70,26 +69,14 @@ namespace Discord
/// <summary> Gets or sets the URL of an <see cref="Embed"/>. </summary>
/// <exception cref="ArgumentException" accessor="set">Url is not a well-formed <see cref="Uri"/>.</exception>
/// <returns> The URL of the embed.</returns>
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; }
/// <summary> Gets or sets the thumbnail URL of an <see cref="Embed"/>. </summary>
/// <exception cref="ArgumentException" accessor="set">Url is not a well-formed <see cref="Uri"/>.</exception>
/// <returns> The thumbnail URL of the embed.</returns>
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);
}
/// <summary> Gets or sets the image URL of an <see cref="Embed"/>. </summary>
/// <exception cref="ArgumentException" accessor="set">Url is not a well-formed <see cref="Uri"/>.</exception>
@@ -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);
}

/// <summary> Gets or sets the list of <see cref="EmbedFieldBuilder"/> of an <see cref="Embed"/>. </summary>
@@ -553,8 +536,6 @@ namespace Discord
public class EmbedAuthorBuilder
{
private string _name;
private string _url;
private string _iconUrl;
/// <summary>
/// Gets the maximum author name length allowed by Discord.
/// </summary>
@@ -585,15 +566,7 @@ namespace Discord
/// <returns>
/// The URL of the author field.
/// </returns>
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; }
/// <summary>
/// Gets or sets the icon URL of the author field.
/// </summary>
@@ -601,15 +574,7 @@ namespace Discord
/// <returns>
/// The icon URL of the author field.
/// </returns>
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; }

/// <summary>
/// Sets the name of the author field.
@@ -671,7 +636,6 @@ namespace Discord
public class EmbedFooterBuilder
{
private string _text;
private string _iconUrl;

/// <summary>
/// Gets the maximum footer length allowed by Discord.
@@ -703,15 +667,7 @@ namespace Discord
/// <returns>
/// The icon URL of the footer field.
/// </returns>
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; }

/// <summary>
/// Sets the name of the footer field.


+ 0
- 10
src/Discord.Net.Core/Extensions/StringExtensions.cs View File

@@ -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);
}
}

+ 0
- 54
test/Discord.Net.Tests.Unit/EmbedBuilderTests.cs View File

@@ -190,42 +190,6 @@ namespace Discord
Assert.Equal(result.ThumbnailUrl, url);
}

/// <summary>
/// Tests that invalid urls throw an <see cref="ArgumentException"/>.
/// </summary>
/// <param name="url">The url to set.</param>
[Theory]
[InlineData(" ")]
[InlineData("not a url")]
public void Url_Invalid(string url)
{
Assert.Throws<ArgumentException>(()
=> new EmbedBuilder()
.WithUrl(url));
Assert.Throws<ArgumentException>(()
=> new EmbedBuilder()
.WithImageUrl(url));
Assert.Throws<ArgumentException>(()
=> new EmbedBuilder()
.WithThumbnailUrl(url));

Assert.Throws<ArgumentException>(() =>
{
var b = new EmbedBuilder();
b.Url = url;
});
Assert.Throws<ArgumentException>(() =>
{
var b = new EmbedBuilder();
b.ImageUrl = url;
});
Assert.Throws<ArgumentException>(() =>
{
var b = new EmbedBuilder();
b.ThumbnailUrl = url;
});
}

/// <summary>
/// Tests the value of the <see cref="EmbedBuilder.Length"/> property when there are no fields set.
/// </summary>
@@ -343,24 +307,6 @@ namespace Discord
Assert.Equal(name, footer.Text);
}
/// <summary>
/// Tests that invalid URLs throw an <see cref="ArgumentException"/>.
/// </summary>
[Fact]
public void EmbedFooterBuilder_InvalidURL()
{
IEnumerable<string> InvalidUrls()
{
yield return "not a url";
}
foreach (var url in InvalidUrls())
{
Assert.Throws<ArgumentException>(() =>
{
new EmbedFooterBuilder().WithIconUrl(url);
});
}
}
/// <summary>
/// Tests that invalid text throws an <see cref="ArgumentException"/>.
/// </summary>
[Fact]


Loading…
Cancel
Save