| 12 | #include "fl/stl/asio/http/test_utils/mock_http_client.h" |
| 13 | |
| 14 | FL_TEST_FILE(FL_FILEPATH) { |
| 15 | |
| 16 | using namespace fl; |
| 17 | using namespace fl::net::http; |
| 18 | |
| 19 | // Helper to create JSON-RPC request |
| 20 | static json createRequest(const char* method, const json& params, const json& id) { |
| 21 | json req = json::object(); |
| 22 | req.set("jsonrpc", "2.0"); |
| 23 | req.set("method", method); |
| 24 | req.set("params", params); |
| 25 | req.set("id", id); |
| 26 | return req; |
| 27 | } |
| 28 | |
| 29 | // Helper to extract result from JSON-RPC response |
| 30 | static fl::optional<json> getResult(const json& response) { |
| 31 | if (!response.contains("result")) { |
| 32 | return fl::nullopt; |
| 33 | } |
| 34 | return response["result"]; |
| 35 | } |
| 36 | |
| 37 | // Helper to extract error from JSON-RPC response |
| 38 | static fl::optional<json> getError(const json& response) { |
| 39 | if (!response.contains("error")) { |
| 40 | return fl::nullopt; |
| 41 | } |
| 42 | return response["error"]; |
| 43 | } |
| 44 | |
| 45 | //============================================================================= |
| 46 | // TEST CASE: SYNC Mode - Immediate Response |
| 47 | //============================================================================= |
| 48 | |
| 49 | FL_TEST_CASE("RPC-HTTP - SYNC mode - Simple add function") { |
| 50 | // Setup: Server with RPC method |
| 51 | MockHttpServer server(47701); |
| 52 | server.connect(); |
| 53 | |
| 54 | Remote remoteServer( |
| 55 | [&server]() { return server.readRequest(); }, |
| 56 | [&server](const json& r) { server.writeResponse(r); } |
| 57 | ); |
| 58 | |
| 59 | remoteServer.bind("add", [](int a, int b) -> int { |
| 60 | return a + b; |
| 61 | }); |
| 62 | |
| 63 | // Setup: Client |
| 64 | MockHttpClient client(server); |
| 65 | client.connect(); |
| 66 | |
| 67 | // Create request |
| 68 | json params = json::array(); |
| 69 | params.push_back(json(5)); |
| 70 | params.push_back(json(7)); |
| 71 | json request = createRequest("add", params, json(1)); |
nothing calls this directly
no test coverage detected