fix: Upload file size limit (#2313)

This commit is contained in:
Tripletri
2022-05-23 11:43:38 +05:00
committed by GitHub
parent 437c8a7f43
commit 54a5af7db4
6 changed files with 91 additions and 6 deletions

View File

@@ -14,6 +14,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

View File

@@ -0,0 +1,53 @@
using Discord.API;
using Discord.API.Rest;
using Discord.Net;
using Discord.Rest;
using FluentAssertions;
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;
namespace Discord;
[CollectionDefinition(nameof(DiscordRestApiClientTests), DisableParallelization = true)]
public class DiscordRestApiClientTests : IClassFixture<RestGuildFixture>, IAsyncDisposable
{
private readonly DiscordRestApiClient _apiClient;
private readonly IGuild _guild;
private readonly ITextChannel _channel;
public DiscordRestApiClientTests(RestGuildFixture guildFixture)
{
_guild = guildFixture.Guild;
_apiClient = guildFixture.Client.ApiClient;
_channel = _guild.CreateTextChannelAsync("testChannel").Result;
}
public async ValueTask DisposeAsync()
{
await _channel.DeleteAsync();
}
[Fact]
public async Task UploadFile_WithMaximumSize_DontThrowsException()
{
var fileSize = GuildHelper.GetUploadLimit(_guild);
using var stream = new MemoryStream(new byte[fileSize]);
await _apiClient.UploadFileAsync(_channel.Id, new UploadFileParams(new FileAttachment(stream, "filename")));
}
[Fact]
public async Task UploadFile_WithOverSize_ThrowsException()
{
var fileSize = GuildHelper.GetUploadLimit(_guild) + 1;
using var stream = new MemoryStream(new byte[fileSize]);
Func<Task> upload = async () =>
await _apiClient.UploadFileAsync(_channel.Id, new UploadFileParams(new FileAttachment(stream, "filename")));
await upload.Should().ThrowExactlyAsync<HttpException>()
.Where(e => e.DiscordCode == DiscordErrorCode.RequestEntityTooLarge);
}
}