Sfoglia il codice sorgente

Make getOption RPC method return option for stopped downloads

Tatsuhiro Tsujikawa 12 anni fa
parent
commit
d444a6cc9e
2 ha cambiato i file con 47 aggiunte e 5 eliminazioni
  1. 10 5
      src/RpcMethodImpl.cc
  2. 37 0
      test/RpcMethodTest.cc

+ 10 - 5
src/RpcMethodImpl.cc

@@ -1155,13 +1155,18 @@ SharedHandle<ValueBase> GetOptionRpcMethod::process
 
   a2_gid_t gid = str2Gid(gidParam);
   SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
+  SharedHandle<Dict> result = Dict::g();
   if(!group) {
-    throw DL_ABORT_EX(fmt("Cannot get option for GID#%s",
-                          GroupId::toHex(gid).c_str()));
+    SharedHandle<DownloadResult> dr =
+      e->getRequestGroupMan()->findDownloadResult(gid);
+    if(!dr) {
+      throw DL_ABORT_EX(fmt("Cannot get option for GID#%s",
+                            GroupId::toHex(gid).c_str()));
+    }
+    pushRequestOption(result, dr->option, getOptionParser());
+  } else {
+    pushRequestOption(result, group->getOption(), getOptionParser());
   }
-  SharedHandle<Dict> result = Dict::g();
-  SharedHandle<Option> option = group->getOption();
-  pushRequestOption(result, option, getOptionParser());
   return result;
 }
 

+ 37 - 0
test/RpcMethodTest.cc

@@ -51,6 +51,7 @@ class RpcMethodTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testAddMetalink_notBase64Metalink);
   CPPUNIT_TEST(testAddMetalink_withPosition);
 #endif // ENABLE_METALINK
+  CPPUNIT_TEST(testGetOption);
   CPPUNIT_TEST(testChangeOption);
   CPPUNIT_TEST(testChangeOption_withBadOption);
   CPPUNIT_TEST(testChangeOption_withNotAllowedOption);
@@ -86,6 +87,7 @@ public:
     option_.reset(new Option());
     option_->put(PREF_DIR, A2_TEST_OUT_DIR"/aria2_RpcMethodTest");
     option_->put(PREF_PIECE_LENGTH, "1048576");
+    option_->put(PREF_MAX_DOWNLOAD_RESULT, "10");
     File(option_->get(PREF_DIR)).mkdirs();
     e_.reset
       (new DownloadEngine(SharedHandle<EventPoll>(new SelectEventPoll())));
@@ -114,6 +116,7 @@ public:
   void testAddMetalink_notBase64Metalink();
   void testAddMetalink_withPosition();
 #endif // ENABLE_METALINK
+  void testGetOption();
   void testChangeOption();
   void testChangeOption_withBadOption();
   void testChangeOption_withNotAllowedOption();
@@ -484,6 +487,40 @@ void RpcMethodTest::testAddMetalink_withPosition()
 
 #endif // ENABLE_METALINK
 
+void RpcMethodTest::testGetOption()
+{
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    option_));
+  group->getOption()->put(PREF_DIR, "alpha");
+  e_->getRequestGroupMan()->addReservedGroup(group);
+  SharedHandle<DownloadResult> dr = createDownloadResult(error_code::FINISHED,
+                                                         "http://host/fin");
+  dr->option->put(PREF_DIR, "bravo");
+  e_->getRequestGroupMan()->addDownloadResult(dr);
+
+  GetOptionRpcMethod m;
+  RpcRequest req(GetOptionRpcMethod::getMethodName(), List::g());
+  req.params->append(GroupId::toHex(group->getGID()));
+  RpcResponse res = m.execute(req, e_.get());
+  CPPUNIT_ASSERT_EQUAL(0, res.code);
+  const Dict* resopt = downcast<Dict>(res.param);
+  CPPUNIT_ASSERT_EQUAL(std::string("alpha"),
+                       downcast<String>(resopt->get(PREF_DIR->k))->s());
+
+  req.params = List::g();
+  req.params->append(dr->gid->toHex());
+  res = m.execute(req, e_.get());
+  CPPUNIT_ASSERT_EQUAL(0, res.code);
+  resopt = downcast<Dict>(res.param);
+  CPPUNIT_ASSERT_EQUAL(std::string("bravo"),
+                       downcast<String>(resopt->get(PREF_DIR->k))->s());
+  // Invalid GID
+  req.params = List::g();
+  req.params->append(GroupId::create()->toHex());
+  res = m.execute(req, e_.get());
+  CPPUNIT_ASSERT_EQUAL(1, res.code);
+}
+
 void RpcMethodTest::testChangeOption()
 {
   SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),