You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_server_test.cc 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "ps/comm/http_server.h"
  17. #include "common/common_test.h"
  18. #include <gtest/gtest.h>
  19. #include <algorithm>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <iostream>
  24. #include <string>
  25. #include <thread>
  26. namespace mindspore {
  27. namespace ps {
  28. namespace comm {
  29. class TestHttpServer : public UT::Common {
  30. public:
  31. TestHttpServer() = default;
  32. static void testGetHandler(HttpMessageHandler *resp) {
  33. std::string host = resp->GetRequestHost();
  34. EXPECT_STREQ(host.c_str(), "127.0.0.1");
  35. std::string path_param = resp->GetPathParam("key1");
  36. std::string header_param = resp->GetHeadParam("headerKey");
  37. std::string post_param = resp->GetPostParam("postKey");
  38. std::string post_message = resp->GetPostMsg();
  39. EXPECT_STREQ(path_param.c_str(), "value1");
  40. EXPECT_STREQ(header_param.c_str(), "headerValue");
  41. EXPECT_STREQ(post_param.c_str(), "postValue");
  42. EXPECT_STREQ(post_message.c_str(), "postKey=postValue");
  43. const std::string rKey("headKey");
  44. const std::string rVal("headValue");
  45. const std::string rBody("post request success!\n");
  46. resp->AddRespHeadParam(rKey, rVal);
  47. resp->AddRespString(rBody);
  48. resp->SetRespCode(200);
  49. resp->SendResponse();
  50. }
  51. void SetUp() override {
  52. server_ = new HttpServer("0.0.0.0", 9999);
  53. std::function<void(HttpMessageHandler *)> http_get_func = std::bind(
  54. [](HttpMessageHandler *resp) {
  55. EXPECT_STREQ(resp->GetPathParam("key1").c_str(), "value1");
  56. EXPECT_STREQ(resp->GetUriQuery().c_str(), "key1=value1");
  57. EXPECT_STREQ(resp->GetRequestUri().c_str(), "/httpget?key1=value1");
  58. EXPECT_STREQ(resp->GetUriPath().c_str(), "/httpget");
  59. resp->QuickResponse(200, "get request success!\n");
  60. },
  61. std::placeholders::_1);
  62. std::function<void(HttpMessageHandler *)> http_handler_func = std::bind(
  63. [](HttpMessageHandler *resp) {
  64. std::string host = resp->GetRequestHost();
  65. EXPECT_STREQ(host.c_str(), "127.0.0.1");
  66. std::string path_param = resp->GetPathParam("key1");
  67. std::string header_param = resp->GetHeadParam("headerKey");
  68. std::string post_param = resp->GetPostParam("postKey");
  69. std::string post_message = resp->GetPostMsg();
  70. EXPECT_STREQ(path_param.c_str(), "value1");
  71. EXPECT_STREQ(header_param.c_str(), "headerValue");
  72. EXPECT_STREQ(post_param.c_str(), "postValue");
  73. EXPECT_STREQ(post_message.c_str(), "postKey=postValue");
  74. const std::string rKey("headKey");
  75. const std::string rVal("headValue");
  76. const std::string rBody("post request success!\n");
  77. resp->AddRespHeadParam(rKey, rVal);
  78. resp->AddRespString(rBody);
  79. resp->SetRespCode(200);
  80. resp->SendResponse();
  81. },
  82. std::placeholders::_1);
  83. server_->RegisterRoute("/httpget", &http_get_func);
  84. server_->RegisterRoute("/handler", &http_handler_func);
  85. std::unique_ptr<std::thread> http_server_thread_(nullptr);
  86. http_server_thread_ = std::make_unique<std::thread>([&]() { server_->Start(); });
  87. http_server_thread_->detach();
  88. }
  89. void TearDown() override { server_->Stop(); }
  90. private:
  91. HttpServer *server_;
  92. };
  93. TEST_F(TestHttpServer, httpGetQequest) {
  94. char buffer[100];
  95. FILE *file;
  96. std::string cmd = "curl -X GET http://127.0.0.1:9999/httpget?key1=value1";
  97. std::string result;
  98. const char *sysCommand = cmd.data();
  99. if ((file = popen(sysCommand, "r")) == nullptr) {
  100. return;
  101. }
  102. while (fgets(buffer, sizeof(buffer) - 1, file) != nullptr) {
  103. result += buffer;
  104. }
  105. EXPECT_STREQ("get request success!\n", result.c_str());
  106. pclose(file);
  107. }
  108. TEST_F(TestHttpServer, messageHandler) {
  109. char buffer[100];
  110. FILE *file;
  111. std::string cmd =
  112. R"(curl -X POST -d 'postKey=postValue' -i -H "Accept: application/json" -H "headerKey: headerValue" http://127.0.0.1:9999/handler?key1=value1)";
  113. std::string result;
  114. const char *sysCommand = cmd.data();
  115. if ((file = popen(sysCommand, "r")) == nullptr) {
  116. return;
  117. }
  118. while (fgets(buffer, sizeof(buffer) - 1, file) != nullptr) {
  119. result += buffer;
  120. }
  121. EXPECT_STREQ("post request success!\n", result.substr(result.find("post")).c_str());
  122. pclose(file);
  123. }
  124. TEST_F(TestHttpServer, portErrorNoException) {
  125. HttpServer *server_exception = new HttpServer("0.0.0.0", -1);
  126. std::function<void(HttpMessageHandler *)> http_handler_func =
  127. std::bind(TestHttpServer::testGetHandler, std::placeholders::_1);
  128. EXPECT_NO_THROW(server_exception->RegisterRoute("/handler", &http_handler_func));
  129. }
  130. TEST_F(TestHttpServer, addressException) {
  131. HttpServer *server_exception = new HttpServer("12344.0.0.0", 9998);
  132. std::function<void(HttpMessageHandler *)> http_handler_func =
  133. std::bind(TestHttpServer::testGetHandler, std::placeholders::_1);
  134. ASSERT_THROW(server_exception->RegisterRoute("/handler", &http_handler_func), std::exception);
  135. }
  136. } // namespace comm
  137. } // namespace ps
  138. } // namespace mindspore