feature: Add webhook url overload for DiscordWebhookClient (#1260)

* Add webhook url overload for DiscordWebhookClient

Adds an overloaded constructor for `DiscordWebhookClient` which accepts the webhook URL. This URL is parsed using a regex for the id and token. If the token is invalid an `ArgumentException` is thrown.

* add null or whitespace check

* add some tests for the new DiscordWebhookClient constructor

* make the Regex static, specify flags

* update regex to look for "discordapp"

* specify reason why exception is thrown despite regex match

* move parsing logic into new function for testing
This commit is contained in:
Chris Johnston
2019-05-04 14:14:32 -07:00
committed by Christopher F
parent 1ae42207f8
commit f2113c7c2b
3 changed files with 109 additions and 0 deletions

View File

@@ -19,6 +19,7 @@
<ProjectReference Include="../../src/Discord.Net.Core/Discord.Net.Core.csproj" />
<ProjectReference Include="../../src/Discord.Net.Rest/Discord.Net.Rest.csproj" />
<ProjectReference Include="../../src/Discord.Net.Analyzers/Discord.Net.Analyzers.csproj" />
<ProjectReference Include="..\..\src\Discord.Net.Webhook\Discord.Net.Webhook.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Akavache" Version="6.0.31" />

View File

@@ -0,0 +1,60 @@
using Discord.Webhook;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace Discord
{
/// <summary>
/// Tests the <see cref="DiscordWebhookClient.ParseWebhookUrl(string, out ulong, out string)"/> function.
/// </summary>
public class DiscordWebhookClientTests
{
[Theory]
[InlineData("https://discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// ptb, canary, etc will have slightly different urls
[InlineData("https://ptb.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
[InlineData("https://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// don't care about https
[InlineData("http://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// this is the minimum that the regex cares about
[InlineData("discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
public void TestWebhook_Valid(string webhookurl, ulong expectedId, string expectedToken)
{
DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
Assert.Equal(expectedId, id);
Assert.Equal(expectedToken, token);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestWebhook_Null(string webhookurl)
{
Assert.Throws<ArgumentNullException>(() =>
{
DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
});
}
[Theory]
[InlineData("123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// trailing slash
[InlineData("https://discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK/")]
public void TestWebhook_Invalid(string webhookurl)
{
Assert.Throws<ArgumentException>(() =>
{
DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
});
}
}
}