30 #include <gtest/gtest.h> 43 using XmlRpcClient::doConnect;
44 using XmlRpcClient::setupConnection;
45 using XmlRpcClient::generateRequest;
46 using XmlRpcClient::generateHeader;
47 using XmlRpcClient::writeRequest;
48 using XmlRpcClient::readHeader;
49 using XmlRpcClient::readResponse;
50 using XmlRpcClient::parseResponse;
52 using XmlRpcClient::ClientConnectionState;
53 using XmlRpcClient::NO_CONNECTION;
54 using XmlRpcClient::CONNECTING;
55 using XmlRpcClient::WRITE_REQUEST;
56 using XmlRpcClient::READ_HEADER;
57 using XmlRpcClient::READ_RESPONSE;
58 using XmlRpcClient::IDLE;
60 using XmlRpcClient::_connectionState;
61 using XmlRpcClient::connectionStateStr;
73 XmlRpc::XmlRpcDispatch::SourceList::const_iterator itr;
74 for (itr = list.begin(); itr != list.end(); itr++) {
75 if (itr->getSource() == source) {
86 #define TEST_STATE(state) \ 87 EXPECT_STREQ(#state, XmlRpcClientForTest::connectionStateStr(XmlRpcClientForTest::state)) 99 EXPECT_EQ(
"localhost", a.
getHost());
101 EXPECT_EQ(
"/RPC2", a.
getUri());
105 EXPECT_EQ(
false, a.
_eof);
107 EXPECT_EQ(-1, a.
getfd());
115 EXPECT_EQ(
"nowhere.com", b.
getHost());
117 EXPECT_EQ(
"/where", b.
getUri());
126 ASSERT_EQ(-1, a.
getfd());
130 EXPECT_EQ(-1, a.
getfd());
142 EXPECT_EQ(-1, a.
getfd());
149 EXPECT_EQ(-1, a.
getfd());
187 Expect_setNonBlocking(7,
true);
188 Expect_connect(7,
"localhost", 42,
true);
193 EXPECT_EQ(7, a.
getfd());
216 Expect_setNonBlocking(7,
true);
217 Expect_connect(7,
"localhost", 42,
true);
222 EXPECT_EQ(7, a.
getfd());
246 Expect_setNonBlocking(7,
true);
247 Expect_connect(7,
"localhost", 42,
true);
252 EXPECT_EQ(7, a.
getfd());
272 Expect_setNonBlocking(7,
true);
273 Expect_connect(7,
"localhost", 42,
false);
274 Expect_getError(ECONNREFUSED);
280 EXPECT_EQ(-1, a.
getfd());
298 Expect_setNonBlocking(7,
true);
299 Expect_connect(7,
"localhost", 42,
true);
304 EXPECT_EQ(7, a.
getfd());
320 Expect_setNonBlocking(8,
true);
321 Expect_connect(8,
"localhost", 42,
false);
322 Expect_getError(ECONNREFUSED);
328 EXPECT_EQ(-1, a.
getfd());
347 Expect_setNonBlocking(7,
true);
348 Expect_connect(7,
"localhost", 42,
true);
353 EXPECT_EQ(7, a.
getfd());
366 Expect_getError(ENFILE);
371 EXPECT_EQ(-1, a.
getfd());
380 Expect_setNonBlocking(7,
false);
381 Expect_getError(EBADF);
387 EXPECT_EQ(-1, a.
getfd());
395 Expect_setNonBlocking(7,
true);
396 Expect_connect(7,
"localhost", 42,
false);
397 Expect_getError(ECONNREFUSED);
403 EXPECT_EQ(-1, a.
getfd());
413 std::string
header =
"POST /RPC2 HTTP/1.1\r\n" 414 "User-Agent: XMLRPC++ 0.7\r\n" 415 "Host: localhost:42\r\n" 416 "Content-Type: text/xml\r\n";
421 EXPECT_EQ(header +
"Content-length: 134\r\n\r\n" 422 "<?xml version=\"1.0\"?>\r\n" 423 "<methodCall><methodName>DoFoo</methodName>\r\n" 424 "<params><param><value>Hello</value></param></params></" 430 EXPECT_EQ(header +
"Content-length: 149\r\n\r\n" 431 "<?xml version=\"1.0\"?>\r\n" 432 "<methodCall><methodName>DoFoo</methodName>\r\n" 433 "<params><param><value><boolean>1</boolean></value></" 434 "param></params></methodCall>\r\n",
439 EXPECT_EQ(header +
"Content-length: 140\r\n\r\n" 440 "<?xml version=\"1.0\"?>\r\n" 441 "<methodCall><methodName>DoFoo</methodName>\r\n" 442 "<params><param><value><i4>42</i4></value></param></" 443 "params></methodCall>\r\n",
457 EXPECT_EQ(header +
"Content-length: 382\r\n\r\n" 458 "<?xml version=\"1.0\"?>\r\n" 459 "<methodCall><methodName>DoFoo</methodName>\r\n" 461 "<param><value><i4>2</i4></value></param>" 462 "<param><value><i4>3</i4></value></param>" 463 "<param><value><i4>5</i4></value></param>" 464 "<param><value><i4>7</i4></value></param>" 465 "<param><value><i4>11</i4></value></param>" 466 "<param><value><i4>13</i4></value></param>" 467 "<param><value><i4>17</i4></value></param>" 468 "</params></methodCall>\r\n",
488 EXPECT_FALSE(empty.
valid());
490 EXPECT_EQ(header +
"Content-length: 84\r\n\r\n" 491 "<?xml version=\"1.0\"?>\r\n" 492 "<methodCall><methodName>DoEmpty</methodName>\r\n" 503 std::string
header =
"POST /RPC2 HTTP/1.1\r\n" 504 "User-Agent: XMLRPC++ 0.7\r\n" 505 "Host: localhost:42\r\n" 506 "Content-Type: text/xml\r\n";
509 EXPECT_EQ(header +
"Content-length: 30\r\n\r\n", a.
generateHeader(30));
512 EXPECT_EQ(header +
"Content-length: 0\r\n\r\n", a.
generateHeader(0));
533 Expect_nbWrite(7,
"FAKE_REQUEST", 12,
true);
560 Expect_nbWrite(7,
"FAKE_REQUEST", 5,
true);
569 Expect_nbWrite(7,
"REQUEST", 7,
true);
593 Expect_nbWrite(7,
"FAKE_REQUEST", 5,
true);
602 Expect_nbWrite(7,
"REQUEST", 0,
false);
605 Expect_getError(ECONNREFUSED);
627 Expect_nbWrite(7,
"FAKE_REQUEST", 0,
false);
629 Expect_getError(ECONNREFUSED);
640 const std::string
header =
"HTTP/1.1 200 OK\r\n" 641 "Server: XMLRPC++ 0.7\r\n" 642 "Content-Type: text/xml\r\n" 643 "Content-length: 114\r\n\r\n";
645 const std::string
header2 =
"HTTP/1.0 200 OK\r\n" 646 "Server: BaseHTTP/0.3 Python/2.7.6\r\n" 647 "Date: Mon, 30 Oct 2017 22:28:12 GMT\r\n" 648 "Content-type: text/xml\r\n" 649 "Content-length: 114\r\n\r\n";
651 const std::string
header3 =
"HTTP/1.1 200 OK\r\n" 652 "Server: XMLRPC++ 0.7\r\n" 653 "Content-Type: text/xml\r\n" 656 const std::string
response =
"<?xml version=\"1.0\"?>\r\n" 657 "<methodResponse><params><param>\r\n" 658 "<value>Hello</value>\r\n" 659 "</param></params></methodResponse>\r\n";
741 Expect_nbRead(7,
header +
" ",
false,
true);
766 std::string header_part1 =
header.substr(0, 32);
767 std::string header_part2 =
header.substr(32);
770 Expect_nbRead(7, header_part1,
false,
true);
775 EXPECT_EQ(header_part1, a.
_header);
781 Expect_nbRead(7, header_part2 +
" ",
false,
true);
805 Expect_nbRead(7,
"",
false,
false);
810 Expect_setNonBlocking(8,
true);
811 Expect_connect(8,
"localhost", 42,
true);
827 Expect_nbRead(8,
"",
true,
true);
828 Expect_getError(ENOTCONN);
851 Expect_nbRead(7,
"",
true,
true);
856 Expect_setNonBlocking(8,
true);
857 Expect_connect(8,
"localhost", 42,
true);
873 Expect_nbRead(8,
"",
true,
true);
874 Expect_getError(ENOTCONN);
896 std::string header_part1 =
header.substr(0, 32);
899 Expect_nbRead(7, header_part1,
false,
true);
904 EXPECT_EQ(header_part1, a.
_header);
910 Expect_nbRead(7,
"",
false,
false);
914 Expect_setNonBlocking(8,
true);
915 Expect_connect(8,
"localhost", 42,
true);
937 std::string header_cl =
header3;
938 header_cl += std::to_string(
size_t(__INT_MAX__) + 1);
939 header_cl +=
"\r\n\r\n ";
941 Expect_nbRead(7, header_cl,
false,
true);
1002 Expect_nbRead(7,
response,
false,
true);
1029 ASSERT_EQ(114u,
response.length());
1031 const std::string response_part1 =
response.substr(0, 60);
1032 const std::string response_part2 =
response.substr(60);
1035 Expect_nbRead(7, response_part1,
false,
true);
1047 Expect_nbRead(7, response_part2,
false,
true);
1076 ASSERT_EQ(114u,
response.length());
1079 Expect_nbRead(7,
"",
false,
false);
1081 Expect_getError(ENOTCONN);
1104 ASSERT_EQ(114u,
response.length());
1107 Expect_nbRead(7,
"",
true,
true);
1132 ::testing::InitGoogleTest(&argc, argv);
1133 return RUN_ALL_TESTS();
virtual bool generateRequest(const char *method, XmlRpcValue const ¶ms)
int getfd() const
Return the file descriptor being monitored.
RPC method arguments and results are represented by Values.
virtual bool readHeader()
bool valid() const
Return true if the value has been set to something.
const std::string & getHost()
TEST(XmlRpcClient, connectionStateStr)
int main(int argc, char **argv)
virtual void close()
Close the connection.
ClientConnectionState _connectionState
TEST_F(MockSocketTest, constructor)
void setfd(int fd)
Specify the file descriptor to monitor.
const std::string header3
bool sourceInList(XmlRpc::XmlRpcSource *source, const XmlRpc::XmlRpcDispatch::SourceList &list)
An RPC source represents a file descriptor to monitor.
static const char * connectionStateStr(ClientConnectionState state)
const std::string & getUri()
void PrintTo(const XmlRpcClientForTest::ClientConnectionState &state, ::std::ostream *os)
const std::string header2
A class to send XML RPC requests to a server and return the results.
virtual std::string generateHeader(size_t length) const
bool getKeepOpen() const
Return whether the file descriptor should be kept open if it is no longer monitored.
std::list< MonitoredSource > SourceList
#define TEST_STATE(state)
virtual bool setupConnection()
virtual bool readResponse()
const std::string response
virtual bool writeRequest()
XmlRpcClientForTest(const char *host, int port, const char *uri=0)