XmlRpcMethodTest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "XmlRpcMethod.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "DownloadEngine.h"
  4. #include "SelectEventPoll.h"
  5. #include "Option.h"
  6. #include "RequestGroupMan.h"
  7. #include "ServerStatMan.h"
  8. #include "RequestGroup.h"
  9. #include "XmlRpcMethodImpl.h"
  10. #include "BDE.h"
  11. #include "OptionParser.h"
  12. #include "OptionHandler.h"
  13. #include "XmlRpcRequest.h"
  14. #include "XmlRpcResponse.h"
  15. #include "prefs.h"
  16. namespace aria2 {
  17. namespace xmlrpc {
  18. class XmlRpcMethodTest:public CppUnit::TestFixture {
  19. CPPUNIT_TEST_SUITE(XmlRpcMethodTest);
  20. CPPUNIT_TEST(testAddUri);
  21. CPPUNIT_TEST(testChangeOption);
  22. CPPUNIT_TEST(testChangeGlobalOption);
  23. CPPUNIT_TEST(testNoSuchMethod);
  24. CPPUNIT_TEST_SUITE_END();
  25. private:
  26. SharedHandle<DownloadEngine> _e;
  27. SharedHandle<Option> _option;
  28. public:
  29. void setUp()
  30. {
  31. RequestGroup::resetGIDCounter();
  32. _option.reset(new Option());
  33. _e.reset(new DownloadEngine(SharedHandle<EventPoll>(new SelectEventPoll())));
  34. _e->option = _option.get();
  35. _e->_requestGroupMan.reset
  36. (new RequestGroupMan(std::deque<SharedHandle<RequestGroup> >(),
  37. 1, _option.get()));
  38. }
  39. void tearDown() {}
  40. void testAddUri();
  41. void testChangeOption();
  42. void testChangeGlobalOption();
  43. void testNoSuchMethod();
  44. };
  45. CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcMethodTest);
  46. void XmlRpcMethodTest::testAddUri()
  47. {
  48. AddUriXmlRpcMethod m;
  49. XmlRpcRequest req("aria2.addUri", BDE::list());
  50. req._params << BDE::list();
  51. req._params[0] << BDE("http://localhost/");
  52. XmlRpcResponse res = m.execute(req, _e.get());
  53. CPPUNIT_ASSERT_EQUAL(0, res._code);
  54. const std::deque<SharedHandle<RequestGroup> > rgs =
  55. _e->_requestGroupMan->getReservedGroups();
  56. CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size());
  57. CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"),
  58. rgs.front()->getRemainingUris().front());
  59. }
  60. void XmlRpcMethodTest::testChangeOption()
  61. {
  62. SharedHandle<RequestGroup> group
  63. (new RequestGroup(_option, std::deque<std::string>()));
  64. _e->_requestGroupMan->addReservedGroup(group);
  65. ChangeOptionXmlRpcMethod m;
  66. XmlRpcRequest req("aria2.changeOption", BDE::list());
  67. req._params << BDE("1");
  68. BDE opt = BDE::dict();
  69. opt[PREF_MAX_DOWNLOAD_LIMIT] = BDE("100K");
  70. opt[PREF_MAX_UPLOAD_LIMIT] = BDE("50K");
  71. req._params << opt;
  72. XmlRpcResponse res = m.execute(req, _e.get());
  73. CPPUNIT_ASSERT_EQUAL(0, res._code);
  74. CPPUNIT_ASSERT_EQUAL((unsigned int)100*1024,
  75. group->getMaxDownloadSpeedLimit());
  76. CPPUNIT_ASSERT_EQUAL((unsigned int)50*1024, group->getMaxUploadSpeedLimit());
  77. }
  78. void XmlRpcMethodTest::testChangeGlobalOption()
  79. {
  80. ChangeGlobalOptionXmlRpcMethod m;
  81. XmlRpcRequest req("aria2.changeGlobalOption", BDE::list());
  82. BDE opt = BDE::dict();
  83. opt[PREF_MAX_OVERALL_DOWNLOAD_LIMIT] = BDE("100K");
  84. opt[PREF_MAX_OVERALL_UPLOAD_LIMIT] = BDE("50K");
  85. req._params << opt;
  86. XmlRpcResponse res = m.execute(req, _e.get());
  87. CPPUNIT_ASSERT_EQUAL(0, res._code);
  88. CPPUNIT_ASSERT_EQUAL((unsigned int)100*1024,
  89. _e->_requestGroupMan->getMaxOverallDownloadSpeedLimit());
  90. CPPUNIT_ASSERT_EQUAL((unsigned int)50*1024,
  91. _e->_requestGroupMan->getMaxOverallUploadSpeedLimit());
  92. }
  93. void XmlRpcMethodTest::testNoSuchMethod()
  94. {
  95. NoSuchMethodXmlRpcMethod m;
  96. XmlRpcRequest req("make.hamburger", BDE::none);
  97. XmlRpcResponse res = m.execute(req, 0);
  98. CPPUNIT_ASSERT_EQUAL(1, res._code);
  99. CPPUNIT_ASSERT_EQUAL(std::string("No such method: make.hamburger"),
  100. res._param["faultString"].s());
  101. CPPUNIT_ASSERT_EQUAL
  102. (std::string("<?xml version=\"1.0\"?>"
  103. "<methodResponse>"
  104. "<fault>"
  105. "<value>"
  106. "<struct>"
  107. "<member>"
  108. "<name>faultCode</name><value><int>1</int></value>"
  109. "</member>"
  110. "<member>"
  111. "<name>faultString</name>"
  112. "<value>"
  113. "<string>No such method: make.hamburger</string>"
  114. "</value>"
  115. "</member>"
  116. "</struct>"
  117. "</value>"
  118. "</fault>"
  119. "</methodResponse>"),
  120. res.toXml());
  121. }
  122. } // namespace xmlrpc
  123. } // namespace aria2