|
1 | 1 | const std = @import("std"); |
2 | 2 | const net = std.net; |
3 | 3 |
|
4 | | -pub fn main() !void { |
5 | | - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
6 | | - const allocator = gpa.allocator(); |
7 | | - defer _ = gpa.deinit(); |
8 | | - |
9 | | - // Connect to server |
10 | | - const address = try std.net.Address.parseIp("127.0.0.1", 13370); |
11 | | - const stream = try std.net.tcpConnectToAddress(address); |
12 | | - defer stream.close(); |
13 | | - |
14 | | - // Prepare request |
15 | | - var request = std.ArrayList(u8).init(allocator); |
16 | | - defer request.deinit(); |
17 | | - |
18 | | - const json_request = |
19 | | - \\{"jsonrpc":"2.0","method":"health","id":1} |
20 | | - ; |
| 4 | +fn sendRequest(allocator: std.mem.Allocator, stream: net.Stream, request: []const u8) ![]const u8 { |
| 5 | + var req_buf = std.ArrayList(u8).init(allocator); |
| 6 | + defer req_buf.deinit(); |
21 | 7 |
|
22 | 8 | // Write gRPC header (uncompressed) |
23 | | - try request.append(0); // Uncompressed flag |
| 9 | + try req_buf.append(0); |
24 | 10 | var length_bytes: [4]u8 = undefined; |
25 | | - std.mem.writeInt(u32, &length_bytes, @intCast(json_request.len), .big); |
26 | | - try request.appendSlice(&length_bytes); |
27 | | - try request.appendSlice(json_request); |
| 11 | + std.mem.writeInt(u32, &length_bytes, @intCast(request.len), .big); |
| 12 | + try req_buf.appendSlice(&length_bytes); |
| 13 | + try req_buf.appendSlice(request); |
28 | 14 |
|
29 | 15 | // Send request |
30 | | - try stream.writeAll(request.items); |
| 16 | + std.debug.print("Sending request: {s}\n", .{request}); |
| 17 | + try stream.writeAll(req_buf.items); |
31 | 18 |
|
32 | | - // Read response |
| 19 | + // Read response header |
| 20 | + std.debug.print("Reading response header...\n", .{}); |
33 | 21 | var header_buf: [5]u8 = undefined; |
34 | 22 | const header_size = try stream.readAll(&header_buf); |
35 | | - if (header_size < 5) { |
36 | | - std.debug.print("Incomplete header received\n", .{}); |
37 | | - return; |
38 | | - } |
| 23 | + if (header_size < 5) return error.IncompleteHeader; |
39 | 24 |
|
40 | 25 | const compressed = header_buf[0] == 1; |
| 26 | + if (compressed) return error.CompressionNotSupported; |
| 27 | + |
41 | 28 | const length = std.mem.readInt(u32, header_buf[1..5], .big); |
| 29 | + const response = try allocator.alloc(u8, length); |
| 30 | + errdefer allocator.free(response); |
42 | 31 |
|
43 | | - if (compressed) { |
44 | | - std.debug.print("Compression not supported\n", .{}); |
45 | | - return; |
| 32 | + const response_size = try stream.readAll(response); |
| 33 | + if (response_size < length) { |
| 34 | + allocator.free(response); |
| 35 | + return error.IncompleteResponse; |
46 | 36 | } |
47 | 37 |
|
48 | | - const response_buf = try allocator.alloc(u8, length); |
49 | | - defer allocator.free(response_buf); |
| 38 | + std.debug.print("Received response of size {d}\n", .{response_size}); |
| 39 | + return response; |
| 40 | +} |
50 | 41 |
|
51 | | - const response_size = try stream.readAll(response_buf); |
52 | | - if (response_size < length) { |
53 | | - std.debug.print("Incomplete response received\n", .{}); |
54 | | - return; |
| 42 | +pub fn main() !void { |
| 43 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 44 | + const allocator = gpa.allocator(); |
| 45 | + defer _ = gpa.deinit(); |
| 46 | + |
| 47 | + const address = try std.net.Address.parseIp("127.0.0.1", 13370); |
| 48 | + |
| 49 | + // Test health endpoint |
| 50 | + { |
| 51 | + std.debug.print("\nTesting health endpoint...\n", .{}); |
| 52 | + const stream = try std.net.tcpConnectToAddress(address); |
| 53 | + defer stream.close(); |
| 54 | + |
| 55 | + const health_req = |
| 56 | + \\{"jsonrpc":"2.0","method":"health","id":1} |
| 57 | + ; |
| 58 | + const health_resp = try sendRequest(allocator, stream, health_req); |
| 59 | + defer allocator.free(health_resp); |
| 60 | + std.debug.print("Health response: {s}\n", .{health_resp}); |
55 | 61 | } |
56 | 62 |
|
57 | | - std.debug.print("Response: {s}\n", .{response_buf}); |
| 63 | + // Test getUser endpoint |
| 64 | + { |
| 65 | + std.debug.print("\nTesting getUser endpoint...\n", .{}); |
| 66 | + const stream = try std.net.tcpConnectToAddress(address); |
| 67 | + defer stream.close(); |
| 68 | + |
| 69 | + const get_user_req = |
| 70 | + \\{"jsonrpc":"2.0","method":"getUser","id":2,"params":{"id":1}} |
| 71 | + ; |
| 72 | + const get_user_resp = try sendRequest(allocator, stream, get_user_req); |
| 73 | + defer allocator.free(get_user_resp); |
| 74 | + std.debug.print("GetUser response: {s}\n", .{get_user_resp}); |
| 75 | + } |
| 76 | + |
| 77 | + // Test createUser endpoint |
| 78 | + { |
| 79 | + std.debug.print("\nTesting createUser endpoint...\n", .{}); |
| 80 | + const stream = try std.net.tcpConnectToAddress(address); |
| 81 | + defer stream.close(); |
| 82 | + |
| 83 | + const create_user_req = |
| 84 | + \\{"jsonrpc":"2.0","method":"createUser","id":3,"params":{"name":"Test User","email":"test@example.com"}} |
| 85 | + ; |
| 86 | + const create_user_resp = try sendRequest(allocator, stream, create_user_req); |
| 87 | + defer allocator.free(create_user_resp); |
| 88 | + std.debug.print("CreateUser response: {s}\n", .{create_user_resp}); |
| 89 | + } |
58 | 90 | } |
0 commit comments