using System.Net;
using SmsSluzba.Api.Gateway;
namespace SmsSluzba.Api.Tests.Gateway;
public class GatewayResponseTests
{
[Theory]
[InlineData("400;1", nameof(GatewayErrorKind.InvalidRecipient))]
[InlineData("400;2", nameof(GatewayErrorKind.MissingText))]
[InlineData("400", nameof(GatewayErrorKind.Rejected))]
[InlineData("401", nameof(GatewayErrorKind.AuthFailed))]
[InlineData("401;1", nameof(GatewayErrorKind.AuthFailed))]
[InlineData("402", nameof(GatewayErrorKind.InsufficientCredit))]
[InlineData("503", nameof(GatewayErrorKind.Unavailable))]
[InlineData("999", nameof(GatewayErrorKind.Unavailable))]
public void Interpret_StatusInBody_ThrowsMappedKind(string code, string expectedKind)
{
var body = $"{code}Popis";
var ex = Assert.Throws(() => GatewayResponse.Interpret(HttpStatusCode.OK, body));
Assert.Equal(expectedKind, ex.Kind.ToString());
Assert.Equal(code, ex.ProviderCode);
Assert.Equal("Popis", ex.ProviderMessage);
}
[Theory]
[InlineData(HttpStatusCode.BadRequest, nameof(GatewayErrorKind.Rejected))]
[InlineData(HttpStatusCode.Unauthorized, nameof(GatewayErrorKind.AuthFailed))]
[InlineData(HttpStatusCode.PaymentRequired, nameof(GatewayErrorKind.InsufficientCredit))]
[InlineData(HttpStatusCode.BadGateway, nameof(GatewayErrorKind.Unavailable))]
public void Interpret_HttpErrorWithoutXml_ThrowsMappedKind(HttpStatusCode status, string expectedKind)
{
var ex = Assert.Throws(() => GatewayResponse.Interpret(status, "error200OK");
Assert.NotNull(document);
}
[Fact]
public void Interpret_EmptySuccess_ReturnsNull()
{
Assert.Null(GatewayResponse.Interpret(HttpStatusCode.OK, string.Empty));
}
[Fact]
public void Interpret_ErrorCodeInBodyWinsOverHttpStatus()
{
var ex = Assert.Throws(() =>
GatewayResponse.Interpret(HttpStatusCode.BadRequest, "400;1x"));
Assert.Equal(GatewayErrorKind.InvalidRecipient, ex.Kind);
}
}