test_web_service.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <kbf/wifi_legacy.h>
  2. #include <kbf/http/client.h>
  3. #include <kbf/web_service.h>
  4. #include <unity.h>
  5. using namespace kbf;
  6. extern const unsigned char cert_pem_start[] asm("_binary_cert_pem_start");
  7. extern const unsigned char cert_pem_end[] asm("_binary_cert_pem_end");
  8. extern const unsigned char key_pem_start[] asm("_binary_key_pem_start");
  9. extern const unsigned char key_pem_end[] asm("_binary_key_pem_end");
  10. class CounterController : public WebService::Controller {
  11. public:
  12. CounterController() : Controller("/counter") {}
  13. http::Response get(const http::Request &request) override {
  14. return http::Response(std::to_string(counter));
  15. }
  16. void onResponseSent(const http::Response &response) override {
  17. counter++;
  18. }
  19. private:
  20. int counter = 0;
  21. };
  22. class EchoController : public WebService::Controller {
  23. public:
  24. EchoController() : Controller("/echo") {};
  25. http::Response get(const http::Request &request) override {
  26. return http::Response(request.query.at("str"));
  27. }
  28. http::Response post(const http::Request &request) override {
  29. auto requestJson = request.json();
  30. return http::Response(requestJson.find("str")->get<string>());
  31. }
  32. };
  33. TEST_CASE("WebService", "[kbf_web_service]") {
  34. wifi_legacy::start();
  35. auto webService = WebService();
  36. webService.controller<CounterController>();
  37. webService.controller<EchoController>();
  38. webService.startSSL(cert_pem_start, cert_pem_end, key_pem_start, key_pem_end);
  39. http::Client::addCert(cert_pem_start, cert_pem_end);
  40. auto client = http::Client();
  41. auto response = client.get("https://localhost/counter");
  42. TEST_ASSERT_EQUAL(200, response->status);
  43. TEST_ASSERT_EQUAL_STRING("0", response->body.c_str());
  44. response = client.get("https://localhost/counter");
  45. TEST_ASSERT_EQUAL_STRING("1", response->body.c_str());
  46. response = client.post("https://localhost/counter", {{"str", "notallowed"}});
  47. TEST_ASSERT_EQUAL(405, response->status);
  48. TEST_ASSERT_EQUAL_STRING("method not allowed", response->body.c_str());
  49. response = client.get("https://localhost/echo?str=foo");
  50. TEST_ASSERT_EQUAL(200, response->status);
  51. TEST_ASSERT_EQUAL_STRING("foo", response->body.c_str());
  52. response = client.post("https://localhost/echo", {{"str", "bar"}});
  53. TEST_ASSERT_EQUAL(200, response->status);
  54. TEST_ASSERT_EQUAL_STRING("bar", response->body.c_str());
  55. webService.stop();
  56. wifi_legacy::stop();
  57. }
  58. class HeaderToParamMiddleware : public WebService::Middleware {
  59. public:
  60. http::Response run(const http::Request &request, WebService &webService) override {
  61. string str = request.readHeader("X-Request");
  62. auto changedRequest = request;
  63. if (request.method == http::GET) {
  64. changedRequest.query["str"] = str;
  65. } else if (request.method == http::POST) {
  66. changedRequest.body = R"({"str":")" + str + "\"}";
  67. } else {
  68. TEST_FAIL();
  69. }
  70. auto response = webService.next(changedRequest);
  71. response.headers["X-Response"] = response.body;
  72. response.body = "changed";
  73. return response;
  74. }
  75. };
  76. class ReverseMiddleware : public WebService::Middleware {
  77. public:
  78. http::Response run(const http::Request &request, WebService &webService) override {
  79. string str;
  80. if (request.method == http::GET) {
  81. str = request.query.at("str");
  82. } else if (request.method == http::POST) {
  83. str = request.json().find("str")->get<string>();
  84. } else {
  85. TEST_FAIL();
  86. }
  87. std::reverse(str.begin(), str.end());
  88. auto changedRequest = request;
  89. if (request.method == http::GET) {
  90. changedRequest.query["str"] = str;
  91. } else {
  92. changedRequest.body = R"({"str":")" + str + "\"}";
  93. }
  94. return webService.next(changedRequest);
  95. }
  96. };
  97. TEST_CASE("WebService Middleware", "[kbf_web_service]") {
  98. wifi_legacy::start();
  99. auto webService = WebService();
  100. webService.controller<EchoController>();
  101. webService.middleware<HeaderToParamMiddleware>();
  102. webService.middleware<ReverseMiddleware>();
  103. webService.start();
  104. auto client = http::Client();
  105. auto response = client.get("http://localhost/echo", {{{"X-Request", "hello"}}});
  106. TEST_ASSERT_EQUAL_STRING("changed", response->body.c_str());
  107. TEST_ASSERT_EQUAL_STRING("olleh", response->headers.at("X-Response").c_str());
  108. response = client.post("http://localhost/echo", nullptr,{{{"X-Request", "foo"}}});
  109. TEST_ASSERT_EQUAL_STRING("changed", response->body.c_str());
  110. TEST_ASSERT_EQUAL_STRING("oof", response->headers.at("X-Response").c_str());
  111. webService.stop();
  112. wifi_legacy::stop();
  113. }