DiscordApi/Discord.API.Tests/Rest/RestClientTests.cs

51 lines
1.6 KiB
C#

using System.Net;
using Discord.API.Rest;
using RichardSzalay.MockHttp;
namespace Discord.API.Tests;
public class RestClientTests{
[Fact]
public async void GetGatewayTest(){
// Arrange
var mock = new MockHttpMessageHandler();
mock.Expect(HttpMethod.Get, "https://localhost/gateway").Respond(HttpStatusCode.OK,"application/json", """
{"url":"wss://test.gateway.api/"}
""");
// Act
var rest_client = new RestClient("https://localhost/", "api_key", mock);
var res = await rest_client.GetGateway();
// Assert
Assert.IsType<RestSuccessResponse<GetGatewayResponse>>(res);
Assert.True(res.Success);
Assert.Equal("wss://test.gateway.api/", res.Value.Url);
mock.VerifyNoOutstandingExpectation();
}
[Fact]
public async void GetGatewayErrorTest(){
// Arrange
var mock = new MockHttpMessageHandler();
mock.Expect(HttpMethod.Get, "https://localhost/gateway").Respond(HttpStatusCode.TooManyRequests,"application/json", """
{
"message": "You are being rate limited.",
"retry_after": 64.57,
"global": false
}
""");
// Act
var rest_client = new RestClient("https://localhost/", "api_key", mock);
var res = await rest_client.GetGateway();
// Assert
Assert.IsType<RestErrorResponse<GetGatewayResponse>>(res);
Assert.False(res.Success);
Assert.Equal("You are being rate limited.", res.Error.Message);
mock.VerifyNoOutstandingExpectation();
}
}