|
- #include <gmock/gmock.h>
-
- #include <cppmicrohttpd.h>
- #include "../helper/libmicrohttpd_mock.h"
-
- using namespace ::testing;
- using namespace ::cppmicrohttpd;
-
- struct test_daemon
- : public daemon
- {
-
- inline test_daemon()
- : daemon(123, daemon_flags::empty())
- {
- using namespace ::testing;
-
- ON_CALL(*this, create_request(_, _, _, _))
- .WillByDefault(Invoke(this, &test_daemon::base_create_request));
- ON_CALL(*this, create_response(_))
- .WillByDefault(Invoke(this, &test_daemon::base_create_response));
- }
-
- inline request_ptr_u base_create_request(
- MHD_Connection * const p_connection,
- const char * const p_url,
- const char * const p_method,
- const char * const p_version)
- { return daemon::create_request(p_connection, p_url, p_method, p_version); }
-
- inline response_ptr_u base_create_response(
- const ::cppmicrohttpd::request& p_request)
- { return daemon::create_response(p_request); }
-
- MOCK_METHOD4(
- create_request,
- request_ptr_u (struct MHD_Connection * const connection,
- const char * const url,
- const char * const method,
- const char * const version));
-
- MOCK_METHOD1(
- create_response,
- response_ptr_u (const ::cppmicrohttpd::request& p_request));
-
- };
-
- TEST(cppmicrohttpd_tests, simpleRequest_get_success)
- {
- auto * daemonPtr =
- reinterpret_cast<MHD_Daemon *>(0xDEADBEEF);
- auto * connectionPtr =
- reinterpret_cast<MHD_Connection *>(0x12345678);
- auto * responsePtr =
- reinterpret_cast<MHD_Response *>(0x45128945);
- auto * response =
- "<html><head><title>"
- "200 - OK"
- "</title></head>"
- "<body><h1>"
- "200 - OK"
- "</h1>"
- "Request successfull"
- "</body></html>";
-
- Sequence seq;
- StrictMock<libmicrohttpd_mock> microhttpd;
-
- EXPECT_CALL(microhttpd, MHD_start_daemon(0, 123, nullptr, nullptr, _, _))
- .InSequence(seq)
- .WillOnce(Return(daemonPtr));
-
- StrictMock<test_daemon> daemon;
- EXPECT_CALL(daemon, create_request(connectionPtr, StrEq("/test-url"), StrEq("GET"), StrEq("HTTP/1.1")))
- .InSequence(seq);
- EXPECT_CALL(daemon, create_response(_))
- .InSequence(seq)
- .WillOnce(Invoke([](auto& req){
- return response::build_simple_html_response(req, 200, "200 - OK", "200 - OK", "Request successfull");
- }));
- EXPECT_CALL(microhttpd, MHD_create_response_from_buffer(98, StrEq(response), MHD_RESPMEM_PERSISTENT))
- .InSequence(seq)
- .WillOnce(Return(responsePtr));
- EXPECT_CALL(microhttpd, MHD_add_response_header(responsePtr, StrEq("Content-Type"), StrEq("text/html;charset=utf-8")))
- .InSequence(seq)
- .WillOnce(Return(MHD_YES));
- EXPECT_CALL(microhttpd, MHD_queue_response(connectionPtr, 200, responsePtr))
- .InSequence(seq)
- .WillOnce(Return(MHD_YES));
- EXPECT_CALL(microhttpd, MHD_stop_daemon(daemonPtr))
- .InSequence(seq);
-
- size_t size = 0;
- void * con_cls = nullptr;
-
- /* first call will initialize the request */
- EXPECT_EQ(
- MHD_YES,
- daemon::mhd_access_handler_callback(
- &daemon,
- connectionPtr,
- "/test-url",
- "GET",
- "HTTP/1.1",
- nullptr,
- &size,
- &con_cls));
-
- /* second call will render the response */
- EXPECT_EQ(
- MHD_YES,
- daemon::mhd_access_handler_callback(
- &daemon,
- connectionPtr,
- "/test-url",
- "GET",
- "HTTP/1.1",
- nullptr,
- &size,
- &con_cls));
- }
|