DownloadHandlersTest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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() { option_ = std::make_shared<Option>(); }
  26. void testGetMemoryPreDownloadHandler();
  27. #ifdef ENABLE_METALINK
  28. void testGetMetalinkPreDownloadHandler_extension();
  29. void testGetMetalinkPreDownloadHandler_contentType();
  30. #endif // ENABLE_METALINK
  31. #ifdef ENABLE_BITTORRENT
  32. void testGetBtPreDownloadHandler_extension();
  33. void testGetBtPreDownloadHandler_contentType();
  34. #endif // ENABLE_BITTORRENT
  35. };
  36. CPPUNIT_TEST_SUITE_REGISTRATION(DownloadHandlersTest);
  37. void DownloadHandlersTest::testGetMemoryPreDownloadHandler()
  38. {
  39. CPPUNIT_ASSERT(
  40. download_handlers::getMemoryPreDownloadHandler()->canHandle(nullptr));
  41. }
  42. #ifdef ENABLE_METALINK
  43. void DownloadHandlersTest::testGetMetalinkPreDownloadHandler_extension()
  44. {
  45. auto dctx = std::make_shared<DownloadContext>(0, 0, "test.metalink");
  46. RequestGroup rg(GroupId::create(), option_);
  47. rg.setDownloadContext(dctx);
  48. auto handler = download_handlers::getMetalinkPreDownloadHandler();
  49. CPPUNIT_ASSERT(handler->canHandle(&rg));
  50. dctx->getFirstFileEntry()->setPath("test.metalink2");
  51. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  52. }
  53. void DownloadHandlersTest::testGetMetalinkPreDownloadHandler_contentType()
  54. {
  55. auto dctx = std::make_shared<DownloadContext>(0, 0, "test");
  56. dctx->getFirstFileEntry()->setContentType("application/metalink+xml");
  57. RequestGroup rg(GroupId::create(), option_);
  58. rg.setDownloadContext(dctx);
  59. auto handler = download_handlers::getMetalinkPreDownloadHandler();
  60. CPPUNIT_ASSERT(handler->canHandle(&rg));
  61. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  62. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  63. }
  64. #endif // ENABLE_METALINK
  65. #ifdef ENABLE_BITTORRENT
  66. void DownloadHandlersTest::testGetBtPreDownloadHandler_extension()
  67. {
  68. auto dctx =
  69. std::make_shared<DownloadContext>(0, 0, A2_TEST_DIR "/test.torrent");
  70. RequestGroup rg(GroupId::create(), option_);
  71. rg.setDownloadContext(dctx);
  72. auto handler = download_handlers::getBtPreDownloadHandler();
  73. CPPUNIT_ASSERT(handler->canHandle(&rg));
  74. dctx->getFirstFileEntry()->setPath(A2_TEST_DIR "/test.torrent2");
  75. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  76. }
  77. void DownloadHandlersTest::testGetBtPreDownloadHandler_contentType()
  78. {
  79. auto dctx = std::make_shared<DownloadContext>(0, 0, "test");
  80. dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
  81. RequestGroup rg(GroupId::create(), option_);
  82. rg.setDownloadContext(dctx);
  83. auto handler = download_handlers::getBtPreDownloadHandler();
  84. CPPUNIT_ASSERT(handler->canHandle(&rg));
  85. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  86. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  87. }
  88. #endif // ENABLE_BITTORRENT
  89. } // namespace aria2