using ZB.MOM.WW.ScadaBridge.CLI; using ZB.MOM.WW.ScadaBridge.CLI.Commands; namespace ZB.MOM.WW.ScadaBridge.CLI.Tests; /// /// Regression tests for an empty success body and a non-JSON success /// body) — both previously crashed table rendering with an unhandled exception. /// [Collection("Console")] public class ResponseRenderingTests { /// Verifies that an empty response body in table format does not throw and yields exit code zero. [Fact] public void HandleResponse_EmptyBody_TableFormat_DoesNotThrow_ReturnsZero() { // A 200/204 with an empty body must be treated as "succeeded, no output". var writer = new StringWriter(); Console.SetOut(writer); try { var response = new ManagementResponse(204, "", null, null); var exitCode = CommandHelpers.HandleResponse(response, "table"); Assert.Equal(0, exitCode); } finally { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }); } } /// Verifies that a whitespace-only response body in JSON format does not throw and yields exit code zero. [Fact] public void HandleResponse_EmptyBody_JsonFormat_DoesNotThrow_ReturnsZero() { var writer = new StringWriter(); Console.SetOut(writer); try { var response = new ManagementResponse(200, " ", null, null); var exitCode = CommandHelpers.HandleResponse(response, "json"); Assert.Equal(0, exitCode); } finally { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }); } } /// Verifies that a non-JSON success body in table format falls back to the raw body and yields exit code zero. [Fact] public void HandleResponse_NonJsonBody_TableFormat_FallsBackToRaw_ReturnsZero() { // A success status with a non-JSON body (e.g. proxy HTML error page) // must not crash; it should print the raw body verbatim. var writer = new StringWriter(); Console.SetOut(writer); try { var response = new ManagementResponse(200, "Service Unavailable", null, null); var exitCode = CommandHelpers.HandleResponse(response, "table"); Assert.Equal(0, exitCode); Assert.Contains("Service Unavailable", writer.ToString()); } finally { Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }); } } }