Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/FakeManagementServer.cs
T
Joseph Doherty 47a7c0dde4 fix(cli): exit 1 on success:false, atomic bundle export, invariant parsing, export timeout, re-subscribe retry (CLI-1,3,4,5,6,7, WP7 tasks 7.1-7.3)
- cached-call retry|discard and data-connection certs list|trust|remove exit 1 when the relayed record carries success:false
- bundle export probes the output path first, writes a temp file, compares the byte count, then moves it into place
- bodyless / textless HTTP errors report 'HTTP <status> <reason>'; bad --trigger-config, sms update, unreadable bundle file and out-of-range --since give the JSON error envelope
- numeric alarm setpoint options and --from/--to date options parse with the invariant culture (de-DE test); offset-less dates are UTC
- audit export gets --timeout-seconds (default 600) for both the headers wait and the mid-stream idle deadline
- debug stream retries a failed re-subscribe and exits 1 with SUBSCRIBE_FAILED if it cannot recover
2026-12-03 09:43:10 -05:00

54 lines
2.5 KiB
C#

using System.Net;
using System.Net.Sockets;
using System.Text;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
/// <summary>
/// Single-request fake management endpoint: accepts one connection, records the request line
/// and body, and answers with a fixed status and body. The CLI tests are pinned to HTTP Basic
/// (<see cref="CliTestAssemblySetup"/>) so one accepted socket is the whole conversation.
/// </summary>
internal static class FakeManagementServer
{
/// <summary>Runs <paramref name="invoke"/> against a one-shot fake server.</summary>
/// <param name="status">HTTP status line text, e.g. <c>200 OK</c>.</param>
/// <param name="responseBody">The response body (may be empty).</param>
/// <param name="invoke">Receives the base URL; returns the CLI exit code.</param>
/// <returns>The exit code and the request body the server saw.</returns>
public static async Task<(int Exit, string RequestBody)> RunAsync(
string status, string responseBody, Func<string, Task<int>> invoke)
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
try
{
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
var server = Task.Run(async () =>
{
using var client = await listener.AcceptTcpClientAsync();
await using var stream = client.GetStream();
using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true);
await reader.ReadLineAsync();
var length = 0;
while (await reader.ReadLineAsync() is { Length: > 0 } header)
if (header.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase))
length = int.Parse(header.Split(':')[1]);
var buffer = new char[length];
if (length > 0)
await reader.ReadBlockAsync(buffer, 0, length);
var body = Encoding.UTF8.GetBytes(responseBody);
var head = Encoding.ASCII.GetBytes(
$"HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {body.Length}\r\nConnection: close\r\n\r\n");
await stream.WriteAsync(head);
await stream.WriteAsync(body);
return new string(buffer);
});
var exit = await invoke($"http://127.0.0.1:{port}");
var requestBody = await server.WaitAsync(TimeSpan.FromSeconds(5));
return (exit, requestBody);
}
finally { listener.Stop(); }
}
}