using System.Net;
using ZB.MOM.WW.ScadaBridge.CLI;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// CLI-6: a bodyless or textless HTTP error must still produce a readable message.
public class ErrorMessageTests
{
/// Verifies that an empty error body synthesises "HTTP {status} {reason}".
/// The HTTP status.
/// The response body.
/// The expected error text.
[Theory]
[InlineData(502, "", "HTTP 502 Bad Gateway")]
[InlineData(307, "", "HTTP 307 Temporary Redirect")]
[InlineData(500, "{\"error\":\"\",\"code\":\"X\"}", "HTTP 500 Internal Server Error")]
[InlineData(400, "{\"error\":\"bad thing\",\"code\":\"BAD\"}", "bad thing")]
[InlineData(400, "plain text", "plain text")]
public void ToErrorResponse_NeverReturnsEmptyError(int code, string body, string expected)
{
using var response = new HttpResponseMessage((HttpStatusCode)code);
var result = ManagementHttpClient.ToErrorResponse(response, body);
Assert.Equal(expected, result.Error);
Assert.Equal(code, result.StatusCode);
Assert.Null(result.JsonData);
}
/// Verifies that the server's error code is preserved.
[Fact]
public void ToErrorResponse_KeepsServerErrorCode()
{
using var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
var result = ManagementHttpClient.ToErrorResponse(response, "{\"error\":\"e\",\"code\":\"BAD\"}");
Assert.Equal("BAD", result.ErrorCode);
}
}