Skip to content

Commit 61ca2bd

Browse files
committed
Add ProxyCommand unit tests for headers and error cases
Added two unit tests to ApiCommandActionTests.cs to verify ProxyCommand behavior with various headers and whitespace paths. Also added System.Text.Json.Nodes using directive for JSON support in tests. #61
1 parent 6f11055 commit 61ca2bd

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

‎tests/SynentraCtl.UnitTests/Commands/ApiCommandActionTests.cs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Synentra.Client.Models.Tokens;
1111
using SynentraCtl.Commands;
1212
using SynentraCtl.Core.Services.Logger;
13+
using System.Text.Json.Nodes;
1314

1415
namespace SynentraCtl.UnitTests.Commands;
1516

@@ -496,4 +497,67 @@ public async Task TokenCommand_Generate_Exception_WritesError()
496497

497498
provider.GetRequiredService<ISynentraCtlLogger>().Received().WriteError(Arg.Any<string>());
498499
}
500+
501+
[Fact]
502+
public async Task ProxyCommand_Create_InvokedWithHeaders_WritesResponse()
503+
{
504+
var provider = BuildProvider(s =>
505+
{
506+
var client = Substitute.For<ISynentraClient>();
507+
var proxyClient = Substitute.For<ISynentraProxyClient>();
508+
509+
proxyClient.ExecuteAsync(
510+
Arg.Any<string>(),
511+
Arg.Any<string>(),
512+
Arg.Any<JsonNode>(),
513+
Arg.Any<Dictionary<string, string>>(),
514+
Arg.Any<CancellationToken>())
515+
.Returns(JsonNode.Parse("{\"status\":\"ok\"}"));
516+
517+
client.Proxy.Returns(proxyClient);
518+
s.AddSingleton(client);
519+
});
520+
521+
var cmd = ProxyCommand.Create(provider);
522+
await InvokeAsync(cmd, [
523+
"--method", "POST",
524+
"--path", "/health",
525+
"--body", "{}",
526+
"--header", "Authorization:Bearer token",
527+
"--header", "InvalidHeader",
528+
"--header", "X-Test:123"
529+
]);
530+
531+
provider.GetRequiredService<ISynentraCtlLogger>().Received().Write(Arg.Is<string>(s => s.Contains("status")));
532+
}
533+
534+
[Fact]
535+
public async Task ProxyCommand_WhitespacePath_WritesError()
536+
{
537+
var provider = BuildProvider(s =>
538+
{
539+
var client = Substitute.For<ISynentraClient>();
540+
var proxyClient = Substitute.For<ISynentraProxyClient>();
541+
542+
proxyClient.ExecuteAsync(
543+
Arg.Any<string>(),
544+
Arg.Any<string>(),
545+
Arg.Any<JsonNode>(),
546+
Arg.Any<Dictionary<string, string>>(),
547+
Arg.Any<CancellationToken>())
548+
.Throws(new Exception("server error"));
549+
550+
client.Proxy.Returns(proxyClient);
551+
s.AddSingleton(client);
552+
});
553+
554+
var cmd = ProxyCommand.Create(provider);
555+
await InvokeAsync(cmd, [
556+
"--path", " ",
557+
"--body", "{}",
558+
"--header", "A:1"
559+
]);
560+
561+
provider.GetRequiredService<ISynentraCtlLogger>().Received().WriteError(Arg.Any<string>());
562+
}
499563
}

0 commit comments

Comments
 (0)