|
2 | 2 | // For the license information refer to glaze.hpp |
3 | 3 |
|
4 | 4 | #include "glaze/net/http_router.hpp" |
| 5 | +#include "glaze/rpc/registry.hpp" |
5 | 6 |
|
6 | 7 | #include <cassert> |
7 | 8 | #include <regex> |
@@ -432,4 +433,151 @@ suite path_param_decoding_tests = [] { |
432 | 433 | }; |
433 | 434 | }; |
434 | 435 |
|
| 436 | +// Service composition test types (GitHub issue #2401) |
| 437 | + |
| 438 | +struct Task |
| 439 | +{ |
| 440 | + int id{}; |
| 441 | + std::string title{}; |
| 442 | +}; |
| 443 | + |
| 444 | +class TaskService |
| 445 | +{ |
| 446 | + std::vector<Task> tasks_; |
| 447 | + |
| 448 | + public: |
| 449 | + std::vector<Task> getAllTasks() { return tasks_; } |
| 450 | +}; |
| 451 | + |
| 452 | +template <> |
| 453 | +struct glz::meta<TaskService> |
| 454 | +{ |
| 455 | + using T = TaskService; |
| 456 | + static constexpr auto value = object(&T::getAllTasks); |
| 457 | +}; |
| 458 | + |
| 459 | +struct Post |
| 460 | +{ |
| 461 | + int id{}; |
| 462 | + std::string title{}; |
| 463 | +}; |
| 464 | + |
| 465 | +class PostService |
| 466 | +{ |
| 467 | + std::vector<Post> posts_; |
| 468 | + |
| 469 | + public: |
| 470 | + std::vector<Post> getAllPosts() { return posts_; } |
| 471 | +}; |
| 472 | + |
| 473 | +template <> |
| 474 | +struct glz::meta<PostService> |
| 475 | +{ |
| 476 | + using T = PostService; |
| 477 | + static constexpr auto value = object(&T::getAllPosts); |
| 478 | +}; |
| 479 | + |
| 480 | +struct Config |
| 481 | +{ |
| 482 | + int id{}; |
| 483 | + std::string title{}; |
| 484 | +}; |
| 485 | + |
| 486 | +class ConfigService |
| 487 | +{ |
| 488 | + std::vector<Config> configs_; |
| 489 | + |
| 490 | + public: |
| 491 | + std::vector<Config> getAllConfigs() { return configs_; } |
| 492 | +}; |
| 493 | + |
| 494 | +template <> |
| 495 | +struct glz::meta<ConfigService> |
| 496 | +{ |
| 497 | + using T = ConfigService; |
| 498 | + static constexpr auto value = object(&T::getAllConfigs); |
| 499 | +}; |
| 500 | + |
| 501 | +suite route_replacement_tests = [] { |
| 502 | + "duplicate_direct_route_replaces_handler"_test = [] { |
| 503 | + glz::http_router router; |
| 504 | + |
| 505 | + router.get("", [](const glz::request&, glz::response& res) { res.body("first"); }); |
| 506 | + router.get("", [](const glz::request&, glz::response& res) { res.body("second"); }); |
| 507 | + |
| 508 | + auto [handler, params] = router.match(glz::http_method::GET, ""); |
| 509 | + expect(handler != nullptr); |
| 510 | + |
| 511 | + glz::request req{.method = glz::http_method::GET, .target = ""}; |
| 512 | + glz::response res; |
| 513 | + handler(req, res); |
| 514 | + expect(res.response_body == "second") << "Last registered handler should win"; |
| 515 | + }; |
| 516 | + |
| 517 | + "duplicate_route_different_methods_coexist"_test = [] { |
| 518 | + glz::http_router router; |
| 519 | + |
| 520 | + router.get("/path", [](const glz::request&, glz::response& res) { res.body("get"); }); |
| 521 | + router.put("/path", [](const glz::request&, glz::response& res) { res.body("put"); }); |
| 522 | + router.get("/path", [](const glz::request&, glz::response& res) { res.body("get2"); }); |
| 523 | + |
| 524 | + auto [get_handler, get_params] = router.match(glz::http_method::GET, "/path"); |
| 525 | + auto [put_handler, put_params] = router.match(glz::http_method::PUT, "/path"); |
| 526 | + expect(get_handler != nullptr); |
| 527 | + expect(put_handler != nullptr); |
| 528 | + |
| 529 | + glz::request req{.method = glz::http_method::GET, .target = "/path"}; |
| 530 | + glz::response get_res, put_res; |
| 531 | + get_handler(req, get_res); |
| 532 | + put_handler(req, put_res); |
| 533 | + expect(get_res.response_body == "get2") << "Replaced GET should use new handler"; |
| 534 | + expect(put_res.response_body == "put") << "PUT should be unchanged"; |
| 535 | + }; |
| 536 | +}; |
| 537 | + |
| 538 | +suite service_composition_tests = [] { |
| 539 | + "multiple_services_on_single_registry"_test = [] { |
| 540 | + TaskService taskService; |
| 541 | + PostService postService; |
| 542 | + ConfigService configService; |
| 543 | + |
| 544 | + glz::registry<glz::opts{}, glz::REST> registry; |
| 545 | + |
| 546 | + // Registering multiple services should not produce errors |
| 547 | + registry.on(taskService); |
| 548 | + registry.on(postService); |
| 549 | + registry.on(configService); |
| 550 | + |
| 551 | + auto check = [&](const std::string& path, bool should_exist) { |
| 552 | + auto [handler, params] = registry.endpoints.match(glz::http_method::GET, path); |
| 553 | + expect((handler != nullptr) == should_exist) << path; |
| 554 | + }; |
| 555 | + |
| 556 | + // Each service's endpoints should be accessible |
| 557 | + check("/getAllTasks", true); |
| 558 | + check("/getAllPosts", true); |
| 559 | + check("/getAllConfigs", true); |
| 560 | + |
| 561 | + // Root endpoint should exist (last registered service wins) |
| 562 | + check("", true); |
| 563 | + }; |
| 564 | + |
| 565 | + "single_service_root_endpoint_works"_test = [] { |
| 566 | + TaskService taskService; |
| 567 | + |
| 568 | + glz::registry<glz::opts{}, glz::REST> registry; |
| 569 | + registry.on(taskService); |
| 570 | + |
| 571 | + // Root should have both GET and PUT |
| 572 | + auto [get_handler, get_params] = registry.endpoints.match(glz::http_method::GET, ""); |
| 573 | + auto [put_handler, put_params] = registry.endpoints.match(glz::http_method::PUT, ""); |
| 574 | + expect(get_handler != nullptr); |
| 575 | + expect(put_handler != nullptr); |
| 576 | + |
| 577 | + // Sub-endpoint should work |
| 578 | + auto [tasks_handler, tasks_params] = registry.endpoints.match(glz::http_method::GET, "/getAllTasks"); |
| 579 | + expect(tasks_handler != nullptr); |
| 580 | + }; |
| 581 | +}; |
| 582 | + |
435 | 583 | int main() {} |
0 commit comments