DownloadHandlersTest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "download_handlers.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "RequestGroup.h"
  4. #include "Option.h"
  5. #include "DownloadContext.h"
  6. #include "MemoryBufferPreDownloadHandler.h"
  7. #include "FileEntry.h"
  8. #include "RequestGroupCriteria.h"
  9. namespace aria2 {
  10. class DownloadHandlersTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(DownloadHandlersTest);
  12. CPPUNIT_TEST(testGetMemoryPreDownloadHandler);
  13. #ifdef ENABLE_METALINK
  14. CPPUNIT_TEST(testGetMetalinkPreDownloadHandler_extension);
  15. CPPUNIT_TEST(testGetMetalinkPreDownloadHandler_contentType);
  16. #endif // ENABLE_METALINK
  17. #ifdef ENABLE_BITTORRENT
  18. CPPUNIT_TEST(testGetBtPreDownloadHandler_extension);
  19. CPPUNIT_TEST(testGetBtPreDownloadHandler_contentType);
  20. #endif // ENABLE_BITTORRENT
  21. CPPUNIT_TEST_SUITE_END();
  22. private:
  23. std::shared_ptr<Option> option_;
  24. public:
  25. void setUp()
  26. {
  27. option_ = std::make_shared<Option>();
  28. }
  29. void testGetMemoryPreDownloadHandler();
  30. #ifdef ENABLE_METALINK
  31. void testGetMetalinkPreDownloadHandler_extension();
  32. void testGetMetalinkPreDownloadHandler_contentType();
  33. #endif // ENABLE_METALINK
  34. #ifdef ENABLE_BITTORRENT
  35. void testGetBtPreDownloadHandler_extension();
  36. void testGetBtPreDownloadHandler_contentType();
  37. #endif // ENABLE_BITTORRENT
  38. };
  39. CPPUNIT_TEST_SUITE_REGISTRATION( DownloadHandlersTest );
  40. void DownloadHandlersTest::testGetMemoryPreDownloadHandler()
  41. {
  42. CPPUNIT_ASSERT(download_handlers::getMemoryPreDownloadHandler()
  43. ->canHandle(nullptr));
  44. }
  45. #ifdef ENABLE_METALINK
  46. void DownloadHandlersTest::testGetMetalinkPreDownloadHandler_extension()
  47. {
  48. auto dctx = std::make_shared<DownloadContext>(0, 0, "test.metalink");
  49. RequestGroup rg(GroupId::create(), option_);
  50. rg.setDownloadContext(dctx);
  51. auto handler = download_handlers::getMetalinkPreDownloadHandler();
  52. CPPUNIT_ASSERT(handler->canHandle(&rg));
  53. dctx->getFirstFileEntry()->setPath("test.metalink2");
  54. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  55. }
  56. void DownloadHandlersTest::testGetMetalinkPreDownloadHandler_contentType()
  57. {
  58. auto dctx = std::make_shared<DownloadContext>(0, 0, "test");
  59. dctx->getFirstFileEntry()->setContentType("application/metalink+xml");
  60. RequestGroup rg(GroupId::create(), option_);
  61. rg.setDownloadContext(dctx);
  62. auto handler = download_handlers::getMetalinkPreDownloadHandler();
  63. CPPUNIT_ASSERT(handler->canHandle(&rg));
  64. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  65. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  66. }
  67. #endif // ENABLE_METALINK
  68. #ifdef ENABLE_BITTORRENT
  69. void DownloadHandlersTest::testGetBtPreDownloadHandler_extension()
  70. {
  71. auto dctx = std::make_shared<DownloadContext>(0, 0,
  72. A2_TEST_DIR"/test.torrent");
  73. RequestGroup rg(GroupId::create(), option_);
  74. rg.setDownloadContext(dctx);
  75. auto handler = download_handlers::getBtPreDownloadHandler();
  76. CPPUNIT_ASSERT(handler->canHandle(&rg));
  77. dctx->getFirstFileEntry()->setPath(A2_TEST_DIR"/test.torrent2");
  78. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  79. }
  80. void DownloadHandlersTest::testGetBtPreDownloadHandler_contentType()
  81. {
  82. auto dctx = std::make_shared<DownloadContext>(0, 0, "test");
  83. dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
  84. RequestGroup rg(GroupId::create(), option_);
  85. rg.setDownloadContext(dctx);
  86. auto handler = download_handlers::getBtPreDownloadHandler();
  87. CPPUNIT_ASSERT(handler->canHandle(&rg));
  88. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  89. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  90. }
  91. #endif // ENABLE_BITTORRENT
  92. } // namespace aria2