DownloadHandlerFactoryTest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "DownloadHandlerFactory.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. namespace aria2 {
  9. class DownloadHandlerFactoryTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(DownloadHandlerFactoryTest);
  11. #ifdef ENABLE_METALINK
  12. CPPUNIT_TEST(testGetMetalinkPreDownloadHandler_extension);
  13. CPPUNIT_TEST(testGetMetalinkPreDownloadHandler_contentType);
  14. #endif // ENABLE_METALINK
  15. #ifdef ENABLE_BITTORRENT
  16. CPPUNIT_TEST(testGetBtPreDownloadHandler_extension);
  17. CPPUNIT_TEST(testGetBtPreDownloadHandler_contentType);
  18. #endif // ENABLE_BITTORRENT
  19. CPPUNIT_TEST_SUITE_END();
  20. private:
  21. SharedHandle<Option> option_;
  22. public:
  23. void setUp()
  24. {
  25. option_.reset(new Option());
  26. }
  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( DownloadHandlerFactoryTest );
  37. #ifdef ENABLE_METALINK
  38. void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_extension()
  39. {
  40. SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test.metalink"));
  41. RequestGroup rg(option_);
  42. rg.setDownloadContext(dctx);
  43. SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getMetalinkPreDownloadHandler();
  44. CPPUNIT_ASSERT(handler->canHandle(&rg));
  45. dctx->getFirstFileEntry()->setPath("test.metalink2");
  46. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  47. }
  48. void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_contentType()
  49. {
  50. SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
  51. dctx->getFirstFileEntry()->setContentType("application/metalink+xml");
  52. RequestGroup rg(option_);
  53. rg.setDownloadContext(dctx);
  54. SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getMetalinkPreDownloadHandler();
  55. CPPUNIT_ASSERT(handler->canHandle(&rg));
  56. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  57. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  58. }
  59. #endif // ENABLE_METALINK
  60. #ifdef ENABLE_BITTORRENT
  61. void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_extension()
  62. {
  63. SharedHandle<DownloadContext> dctx
  64. (new DownloadContext(0, 0, A2_TEST_DIR"/test.torrent"));
  65. RequestGroup rg(option_);
  66. rg.setDownloadContext(dctx);
  67. SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getBtPreDownloadHandler();
  68. CPPUNIT_ASSERT(handler->canHandle(&rg));
  69. dctx->getFirstFileEntry()->setPath(A2_TEST_DIR"/test.torrent2");
  70. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  71. }
  72. void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_contentType()
  73. {
  74. SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
  75. dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
  76. RequestGroup rg(option_);
  77. rg.setDownloadContext(dctx);
  78. SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getBtPreDownloadHandler();
  79. CPPUNIT_ASSERT(handler->canHandle(&rg));
  80. dctx->getFirstFileEntry()->setContentType("application/octet-stream");
  81. CPPUNIT_ASSERT(!handler->canHandle(&rg));
  82. }
  83. #endif // ENABLE_BITTORRENT
  84. } // namespace aria2