XmlRpcRequestProcessorTest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "XmlRpcRequestProcessor.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "XmlRpcRequestParserStateMachine.h"
  4. #include "RecoverableException.h"
  5. namespace aria2 {
  6. namespace xmlrpc {
  7. class XmlRpcRequestProcessorTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(XmlRpcRequestProcessorTest);
  9. CPPUNIT_TEST(testParseMemory);
  10. CPPUNIT_TEST(testParseMemory_shouldFail);
  11. CPPUNIT_TEST_SUITE_END();
  12. public:
  13. void setUp() {}
  14. void tearDown() {}
  15. void testParseMemory();
  16. void testParseMemory_shouldFail();
  17. };
  18. CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcRequestProcessorTest);
  19. void XmlRpcRequestProcessorTest::testParseMemory()
  20. {
  21. XmlRpcRequestProcessor proc;
  22. XmlRpcRequest req =
  23. proc.parseMemory("<?xml version=\"1.0\"?>"
  24. "<methodCall>"
  25. " <methodName>aria2.addURI</methodName>"
  26. " <params>"
  27. " <param>"
  28. " <value><i4>100</i4></value>"
  29. " </param>"
  30. " <param>"
  31. " <value>"
  32. " <struct>"
  33. " <member>"
  34. " <name>max-count</name>"
  35. " <value><i4>65535</i4></value>"
  36. " </member>"
  37. " <member>"
  38. " <name>seed-ratio</name>"
  39. " <value><double>0.99</double></value>"
  40. " </member>"
  41. " </struct>"
  42. " </value>"
  43. " </param>"
  44. " <param>"
  45. " <value>"
  46. " <array>"
  47. " <data>"
  48. " <value><string>pudding</string></value>"
  49. " <value><base64>aGVsbG8gd29ybGQ=</base64></value>"
  50. " </data>"
  51. " </array>"
  52. " </value>"
  53. " </param>"
  54. " </params>"
  55. "</methodCall>");
  56. CPPUNIT_ASSERT_EQUAL(std::string("aria2.addURI"), req.methodName);
  57. CPPUNIT_ASSERT_EQUAL((size_t)3, req.params->size());
  58. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)100,
  59. asInteger(req.params->get(0))->i());
  60. const Dict* dict = asDict(req.params->get(1));
  61. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)65535,
  62. asInteger(dict->get("max-count"))->i());
  63. // Current implementation handles double as string.
  64. CPPUNIT_ASSERT_EQUAL(std::string("0.99"),
  65. asString(dict->get("seed-ratio"))->s());
  66. const List* list = asList(req.params->get(2));
  67. CPPUNIT_ASSERT_EQUAL(std::string("pudding"), asString(list->get(0))->s());
  68. CPPUNIT_ASSERT_EQUAL(std::string("hello world"), asString(list->get(1))->s());
  69. }
  70. void XmlRpcRequestProcessorTest::testParseMemory_shouldFail()
  71. {
  72. XmlRpcRequestProcessor proc;
  73. try {
  74. proc.parseMemory("<methodCall>"
  75. " <methodName>aria2.addURI</methodName>"
  76. " <params>"
  77. " <param>"
  78. " <value><i4>100</i4></value>"
  79. " </param>");
  80. CPPUNIT_FAIL("exception must be thrown.");
  81. } catch(RecoverableException& e) {
  82. // success
  83. }
  84. {
  85. XmlRpcRequest req =
  86. proc.parseMemory("<methodCall>"
  87. " <methodName>aria2.addURI</methodName>"
  88. " <params>"
  89. " </params>"
  90. "</methodCall>");
  91. CPPUNIT_ASSERT(req.params);
  92. }
  93. try {
  94. XmlRpcRequest req =
  95. proc.parseMemory("<methodCall>"
  96. " <methodName>aria2.addURI</methodName>"
  97. "</methodCall>");
  98. CPPUNIT_FAIL("exception must be thrown.");
  99. } catch(RecoverableException& e) {
  100. // success
  101. }
  102. }
  103. } // namespace xmlrpc
  104. } // namespace aria2