Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

122 righe
3.7 KiB

  1. #include <gmock/gmock.h>
  2. #include <cppmicrohttpd.h>
  3. #include "../helper/libmicrohttpd_mock.h"
  4. using namespace ::testing;
  5. using namespace ::cppmicrohttpd;
  6. struct test_daemon
  7. : public daemon
  8. {
  9. inline test_daemon()
  10. : daemon(123, daemon_flags::empty())
  11. {
  12. using namespace ::testing;
  13. ON_CALL(*this, create_request(_, _, _, _))
  14. .WillByDefault(Invoke(this, &test_daemon::base_create_request));
  15. ON_CALL(*this, create_response(_))
  16. .WillByDefault(Invoke(this, &test_daemon::base_create_response));
  17. }
  18. inline request_ptr_u base_create_request(
  19. MHD_Connection * const p_connection,
  20. const char * const p_url,
  21. const char * const p_method,
  22. const char * const p_version)
  23. { return daemon::create_request(p_connection, p_url, p_method, p_version); }
  24. inline response_ptr_u base_create_response(
  25. ::cppmicrohttpd::request& p_request)
  26. { return daemon::create_response(p_request); }
  27. MOCK_METHOD4(
  28. create_request,
  29. request_ptr_u (struct MHD_Connection * const connection,
  30. const char * const url,
  31. const char * const method,
  32. const char * const version));
  33. MOCK_METHOD1(
  34. create_response,
  35. response_ptr_u (::cppmicrohttpd::request& p_request));
  36. };
  37. TEST(cppmicrohttpd_tests, simpleRequest_get_success)
  38. {
  39. auto * daemonPtr =
  40. reinterpret_cast<MHD_Daemon *>(0xDEADBEEF);
  41. auto * connectionPtr =
  42. reinterpret_cast<MHD_Connection *>(0x12345678);
  43. auto * responsePtr =
  44. reinterpret_cast<MHD_Response *>(0x45128945);
  45. auto * response =
  46. "<html><head><title>"
  47. "200 - OK"
  48. "</title></head>"
  49. "<body><h1>"
  50. "200 - OK"
  51. "</h1>"
  52. "Request successfull"
  53. "</body></html>";
  54. Sequence seq;
  55. StrictMock<libmicrohttpd_mock> microhttpd;
  56. EXPECT_CALL(microhttpd, MHD_start_daemon(0, 123, nullptr, nullptr, _, _))
  57. .InSequence(seq)
  58. .WillOnce(Return(daemonPtr));
  59. StrictMock<test_daemon> daemon;
  60. EXPECT_CALL(daemon, create_request(connectionPtr, StrEq("/test-url"), StrEq("GET"), StrEq("HTTP/1.1")))
  61. .InSequence(seq);
  62. EXPECT_CALL(daemon, create_response(_))
  63. .InSequence(seq)
  64. .WillOnce(Invoke([](auto& req){
  65. return response::build_simple_html_response(req, 200, "200 - OK", "200 - OK", "Request successfull");
  66. }));
  67. EXPECT_CALL(microhttpd, MHD_create_response_from_buffer(98, StrEq(response), MHD_RESPMEM_PERSISTENT))
  68. .InSequence(seq)
  69. .WillOnce(Return(responsePtr));
  70. EXPECT_CALL(microhttpd, MHD_add_response_header(responsePtr, StrEq("Content-Type"), StrEq("text/html;charset=utf-8")))
  71. .InSequence(seq)
  72. .WillOnce(Return(MHD_YES));
  73. EXPECT_CALL(microhttpd, MHD_queue_response(connectionPtr, 200, responsePtr))
  74. .InSequence(seq)
  75. .WillOnce(Return(MHD_YES));
  76. EXPECT_CALL(microhttpd, MHD_stop_daemon(daemonPtr))
  77. .InSequence(seq);
  78. size_t size = 0;
  79. void * con_cls = nullptr;
  80. /* first call will initialize the request */
  81. EXPECT_EQ(
  82. MHD_YES,
  83. daemon::mhd_access_handler_callback(
  84. &daemon,
  85. connectionPtr,
  86. "/test-url",
  87. "GET",
  88. "HTTP/1.1",
  89. nullptr,
  90. &size,
  91. &con_cls));
  92. /* second call will render the response */
  93. EXPECT_EQ(
  94. MHD_YES,
  95. daemon::mhd_access_handler_callback(
  96. &daemon,
  97. connectionPtr,
  98. "/test-url",
  99. "GET",
  100. "HTTP/1.1",
  101. nullptr,
  102. &size,
  103. &con_cls));
  104. }