101 lines
2.4 KiB
C#
101 lines
2.4 KiB
C#
using Grpc.Core;
|
|
using Grpc.Net.Client;
|
|
using Svrnty.CQRS.Grpc.Sample.Grpc;
|
|
|
|
Console.WriteLine("=== gRPC Client Validation Test ===");
|
|
Console.WriteLine();
|
|
|
|
// Create a gRPC channel
|
|
using var channel = GrpcChannel.ForAddress("http://localhost:5000");
|
|
|
|
// Create the gRPC client
|
|
var client = new CommandService.CommandServiceClient(channel);
|
|
|
|
// Test 1: Valid request
|
|
Console.WriteLine("Test 1: Valid AddUser request...");
|
|
var validRequest = new AddUserCommandRequest
|
|
{
|
|
Name = "John Doe",
|
|
Email = "john.doe@example.com",
|
|
Age = 30
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await client.AddUserAsync(validRequest);
|
|
Console.WriteLine($"✓ Success! User added with ID: {response.Result}");
|
|
}
|
|
catch (RpcException ex)
|
|
{
|
|
Console.WriteLine($"✗ Unexpected error: {ex.Status.Detail}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
|
|
// Test 2: Invalid email (empty)
|
|
Console.WriteLine("Test 2: Invalid email (empty)...");
|
|
var invalidEmailRequest = new AddUserCommandRequest
|
|
{
|
|
Name = "Jane Doe",
|
|
Email = "",
|
|
Age = 25
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await client.AddUserAsync(invalidEmailRequest);
|
|
Console.WriteLine($"✗ Unexpected success! Validation should have failed.");
|
|
}
|
|
catch (RpcException ex)
|
|
{
|
|
Console.WriteLine($"✓ Validation caught! Status: {ex.StatusCode}");
|
|
Console.WriteLine($" Message: {ex.Status.Detail}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
|
|
// Test 3: Invalid email format
|
|
Console.WriteLine("Test 3: Invalid email format...");
|
|
var badEmailRequest = new AddUserCommandRequest
|
|
{
|
|
Name = "Bob Smith",
|
|
Email = "not-an-email",
|
|
Age = 40
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await client.AddUserAsync(badEmailRequest);
|
|
Console.WriteLine($"✗ Unexpected success! Validation should have failed.");
|
|
}
|
|
catch (RpcException ex)
|
|
{
|
|
Console.WriteLine($"✓ Validation caught! Status: {ex.StatusCode}");
|
|
Console.WriteLine($" Message: {ex.Status.Detail}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
|
|
// Test 4: Invalid age (0)
|
|
Console.WriteLine("Test 4: Invalid age (0)...");
|
|
var invalidAgeRequest = new AddUserCommandRequest
|
|
{
|
|
Name = "Alice Brown",
|
|
Email = "alice@example.com",
|
|
Age = 0
|
|
};
|
|
|
|
try
|
|
{
|
|
var response = await client.AddUserAsync(invalidAgeRequest);
|
|
Console.WriteLine($"✗ Unexpected success! Validation should have failed.");
|
|
}
|
|
catch (RpcException ex)
|
|
{
|
|
Console.WriteLine($"✓ Validation caught! Status: {ex.StatusCode}");
|
|
Console.WriteLine($" Message: {ex.Status.Detail}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("All tests completed!");
|