You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiscordRestApiClientTests.cs 1.7 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Discord.API;
  2. using Discord.API.Rest;
  3. using Discord.Net;
  4. using Discord.Rest;
  5. using FluentAssertions;
  6. using System;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using Xunit;
  10. namespace Discord;
  11. [CollectionDefinition(nameof(DiscordRestApiClientTests), DisableParallelization = true)]
  12. public class DiscordRestApiClientTests : IClassFixture<RestGuildFixture>, IAsyncDisposable
  13. {
  14. private readonly DiscordRestApiClient _apiClient;
  15. private readonly IGuild _guild;
  16. private readonly ITextChannel _channel;
  17. public DiscordRestApiClientTests(RestGuildFixture guildFixture)
  18. {
  19. _guild = guildFixture.Guild;
  20. _apiClient = guildFixture.Client.ApiClient;
  21. _channel = _guild.CreateTextChannelAsync("testChannel").Result;
  22. }
  23. public async ValueTask DisposeAsync()
  24. {
  25. await _channel.DeleteAsync();
  26. }
  27. [Fact]
  28. public async Task UploadFile_WithMaximumSize_DontThrowsException()
  29. {
  30. var fileSize = GuildHelper.GetUploadLimit(_guild);
  31. using var stream = new MemoryStream(new byte[fileSize]);
  32. await _apiClient.UploadFileAsync(_channel.Id, new UploadFileParams(new FileAttachment(stream, "filename")));
  33. }
  34. [Fact]
  35. public async Task UploadFile_WithOverSize_ThrowsException()
  36. {
  37. var fileSize = GuildHelper.GetUploadLimit(_guild) + 1;
  38. using var stream = new MemoryStream(new byte[fileSize]);
  39. Func<Task> upload = async () =>
  40. await _apiClient.UploadFileAsync(_channel.Id, new UploadFileParams(new FileAttachment(stream, "filename")));
  41. await upload.Should().ThrowExactlyAsync<HttpException>()
  42. .Where(e => e.DiscordCode == DiscordErrorCode.RequestEntityTooLarge);
  43. }
  44. }