Browse Source

move parsing logic into new function for testing

pull/1260/head
Chris Johnston 6 years ago
parent
commit
5adacdc77a
3 changed files with 49 additions and 45 deletions
  1. +30
    -24
      src/Discord.Net.Webhook/DiscordWebhookClient.cs
  2. +18
    -20
      test/Discord.Net.Tests/Tests.DiscordWebhookClient.cs
  3. +1
    -1
      test/Discord.Net.Tests/Tests.cs

+ 30
- 24
src/Discord.Net.Webhook/DiscordWebhookClient.cs View File

@@ -59,30 +59,10 @@ namespace Discord.Webhook
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="webhookUrl"/> is null or whitespace.</exception> /// <exception cref="ArgumentNullException">Thrown if the <paramref name="webhookUrl"/> is null or whitespace.</exception>
public DiscordWebhookClient(string webhookUrl, DiscordRestConfig config) : this(config) public DiscordWebhookClient(string webhookUrl, DiscordRestConfig config) : this(config)
{ {
if (string.IsNullOrWhiteSpace(webhookUrl))
throw new ArgumentNullException(paramName: nameof(webhookUrl), message:
"The given webhook Url cannot be null or whitespace.");

// thrown when groups are not populated/valid, or when there is no match
ArgumentException ex(string reason = null)
=> new ArgumentException(paramName: nameof(webhookUrl), message:
$"The given webhook Url was not in a valid format. {reason}");
var match = WebhookUrlRegex.Match(webhookUrl);
if (match != null)
{
// ensure that the first group is a ulong, set the _webhookId
// 0th group is always the entire match, so start at index 1
if (!(match.Groups[1].Success && ulong.TryParse(match.Groups[1].Value, out _webhookId)))
throw ex("The webhook Id could not be parsed.");

if (!match.Groups[2].Success)
throw ex("The webhook token could not be parsed.");

ApiClient.LoginAsync(TokenType.Webhook, match.Groups[2].Value).GetAwaiter().GetResult();
Webhook = WebhookClientHelper.GetWebhookAsync(this, _webhookId).GetAwaiter().GetResult();
}
else
throw ex();
string token;
ParseWebhookUrl(webhookUrl, out _webhookId, out token);
ApiClient.LoginAsync(TokenType.Webhook, token).GetAwaiter().GetResult();
Webhook = WebhookClientHelper.GetWebhookAsync(this, _webhookId).GetAwaiter().GetResult();
} }


private DiscordWebhookClient(DiscordRestConfig config) private DiscordWebhookClient(DiscordRestConfig config)
@@ -136,5 +116,31 @@ namespace Discord.Webhook
{ {
ApiClient?.Dispose(); ApiClient?.Dispose();
} }

internal static void ParseWebhookUrl(string webhookUrl, out ulong webhookId, out string webhookToken)
{
if (string.IsNullOrWhiteSpace(webhookUrl))
throw new ArgumentNullException(paramName: nameof(webhookUrl), message:
"The given webhook Url cannot be null or whitespace.");

// thrown when groups are not populated/valid, or when there is no match
ArgumentException ex(string reason = null)
=> new ArgumentException(paramName: nameof(webhookUrl), message:
$"The given webhook Url was not in a valid format. {reason}");
var match = WebhookUrlRegex.Match(webhookUrl);
if (match != null)
{
// ensure that the first group is a ulong, set the _webhookId
// 0th group is always the entire match, so start at index 1
if (!(match.Groups[1].Success && ulong.TryParse(match.Groups[1].Value, out webhookId)))
throw ex("The webhook Id could not be parsed.");

if (!match.Groups[2].Success)
throw ex("The webhook token could not be parsed.");
webhookToken = match.Groups[2].Value;
}
else
throw ex();
}
} }
} }

+ 18
- 20
test/Discord.Net.Tests/Tests.DiscordWebhookClient.cs View File

@@ -7,32 +7,30 @@ using Xunit;
namespace Discord namespace Discord
{ {
/// <summary> /// <summary>
/// Tests that the <see cref=""/>
/// Tests the <see cref="DiscordWebhookClient.ParseWebhookUrl(string, out ulong, out string)"/> function.
/// </summary> /// </summary>
public class DiscordWebhookClientTests public class DiscordWebhookClientTests
{ {
[Theory] [Theory]
[InlineData("https://discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
[InlineData("https://discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// ptb, canary, etc will have slightly different urls // ptb, canary, etc will have slightly different urls
[InlineData("https://ptb.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
[InlineData("https://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
[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 // don't care about https
[InlineData("http://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
[InlineData("http://canary.discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
// this is the minimum that the regex cares about // this is the minimum that the regex cares about
[InlineData("discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
public void TestWebhook_Valid(string webhookurl)
[InlineData("discordapp.com/api/webhooks/123412347732897802/_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK",
123412347732897802, "_abcde123456789-ABCDEFGHIJKLMNOP12345678-abcdefghijklmnopABCDEFGHIJK")]
public void TestWebhook_Valid(string webhookurl, ulong expectedId, string expectedToken)
{ {
try
{
_ = new DiscordWebhookClient(webhookurl);
}
catch (InvalidOperationException)
{
// ignore, thrown because webhook urls are invalid
}
// pass if no exception thrown
Assert.True(true);
DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);

Assert.Equal(expectedId, id);
Assert.Equal(expectedToken, token);
} }


[Theory] [Theory]
@@ -43,7 +41,7 @@ namespace Discord
{ {
Assert.Throws<ArgumentNullException>(() => Assert.Throws<ArgumentNullException>(() =>
{ {
_ = new DiscordWebhookClient(webhookurl);
DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
}); });
} }


@@ -55,7 +53,7 @@ namespace Discord
{ {
Assert.Throws<ArgumentException>(() => Assert.Throws<ArgumentException>(() =>
{ {
_ = new DiscordWebhookClient(webhookurl);
DiscordWebhookClient.ParseWebhookUrl(webhookurl, out ulong id, out string token);
}); });
} }
} }


+ 1
- 1
test/Discord.Net.Tests/Tests.cs View File

@@ -50,4 +50,4 @@ namespace Discord
_guild = fixture._guild; _guild = fixture._guild;
} }
} }
}
}

Loading…
Cancel
Save