RequestTest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "Request.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. class RequestTest:public CppUnit::TestFixture {
  4. CPPUNIT_TEST_SUITE(RequestTest);
  5. CPPUNIT_TEST(testSetUrl1);
  6. CPPUNIT_TEST(testSetUrl2);
  7. CPPUNIT_TEST(testSetUrl3);
  8. CPPUNIT_TEST(testSetUrl4);
  9. CPPUNIT_TEST(testSetUrl5);
  10. CPPUNIT_TEST(testSetUrl6);
  11. CPPUNIT_TEST(testSetUrl7);
  12. CPPUNIT_TEST(testSetUrl8);
  13. CPPUNIT_TEST(testSetUrl9);
  14. CPPUNIT_TEST(testSetUrl10);
  15. CPPUNIT_TEST(testRedirectUrl);
  16. CPPUNIT_TEST(testRedirectUrl2);
  17. CPPUNIT_TEST(testResetUrl);
  18. CPPUNIT_TEST(testSafeChar);
  19. CPPUNIT_TEST_SUITE_END();
  20. public:
  21. void testSetUrl1();
  22. void testSetUrl2();
  23. void testSetUrl3();
  24. void testSetUrl4();
  25. void testSetUrl5();
  26. void testSetUrl6();
  27. void testSetUrl7();
  28. void testSetUrl8();
  29. void testSetUrl9();
  30. void testSetUrl10();
  31. void testRedirectUrl();
  32. void testRedirectUrl2();
  33. void testResetUrl();
  34. void testSafeChar();
  35. };
  36. CPPUNIT_TEST_SUITE_REGISTRATION( RequestTest );
  37. void RequestTest::testSetUrl1() {
  38. Request req;
  39. bool v = req.setUrl("http://aria.rednoah.com/");
  40. CPPUNIT_ASSERT(v);
  41. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getUrl());
  42. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getCurrentUrl());
  43. CPPUNIT_ASSERT_EQUAL(string(""), req.getPreviousUrl());
  44. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  45. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  46. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  47. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  48. CPPUNIT_ASSERT_EQUAL(string(""), req.getFile());
  49. }
  50. void RequestTest::testSetUrl2() {
  51. Request req;
  52. bool v = req.setUrl("http://aria.rednoah.com:8080/index.html");
  53. req.setReferer("http://aria.rednoah.com:8080");
  54. CPPUNIT_ASSERT(v);
  55. // referer is unchaged
  56. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080"), req.getReferer());
  57. // previousUrl must equal to referer;
  58. CPPUNIT_ASSERT_EQUAL(req.getReferer(), req.getPreviousUrl());
  59. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  60. CPPUNIT_ASSERT_EQUAL(8080, req.getPort());
  61. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  62. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  63. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  64. }
  65. void RequestTest::testSetUrl3() {
  66. Request req;
  67. bool v = req.setUrl("http://aria.rednoah.com/aria2/index.html");
  68. CPPUNIT_ASSERT(v);
  69. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  70. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  71. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  72. CPPUNIT_ASSERT_EQUAL(string("/aria2"), req.getDir());
  73. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  74. }
  75. void RequestTest::testSetUrl4() {
  76. Request req;
  77. bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3/index.html");
  78. CPPUNIT_ASSERT(v);
  79. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  80. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  81. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  82. CPPUNIT_ASSERT_EQUAL(string("/aria2/aria3"), req.getDir());
  83. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  84. }
  85. void RequestTest::testSetUrl5() {
  86. Request req;
  87. bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3/");
  88. CPPUNIT_ASSERT(v);
  89. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  90. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  91. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  92. CPPUNIT_ASSERT_EQUAL(string("/aria2/aria3"), req.getDir());
  93. CPPUNIT_ASSERT_EQUAL(string(""), req.getFile());
  94. }
  95. void RequestTest::testSetUrl6() {
  96. Request req;
  97. bool v = req.setUrl("http://aria.rednoah.com/aria2/aria3");
  98. CPPUNIT_ASSERT(v);
  99. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  100. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  101. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  102. CPPUNIT_ASSERT_EQUAL(string("/aria2"), req.getDir());
  103. CPPUNIT_ASSERT_EQUAL(string("aria3"), req.getFile());
  104. }
  105. void RequestTest::testSetUrl7() {
  106. Request req;
  107. bool v = req.setUrl("http://");
  108. CPPUNIT_ASSERT(!v);
  109. }
  110. void RequestTest::testSetUrl8() {
  111. Request req;
  112. bool v = req.setUrl("http:/aria.rednoah.com");
  113. CPPUNIT_ASSERT(!v);
  114. }
  115. void RequestTest::testSetUrl9() {
  116. Request req;
  117. bool v = req.setUrl("h");
  118. CPPUNIT_ASSERT(!v);
  119. }
  120. void RequestTest::testSetUrl10() {
  121. Request req;
  122. bool v = req.setUrl("");
  123. CPPUNIT_ASSERT(!v);
  124. }
  125. void RequestTest::testRedirectUrl() {
  126. Request req;
  127. bool v = req.setUrl("http://aria.rednoah.com:8080/aria2/index.html");
  128. bool v2 = req.redirectUrl("http://aria.rednoah.co.jp/");
  129. CPPUNIT_ASSERT(v2);
  130. // url must be the same
  131. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/aria2/index.html"),
  132. req.getUrl());
  133. // currentUrl must be updated
  134. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.co.jp/"), req.getCurrentUrl());
  135. // previousUrl must be updated
  136. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/aria2/index.html"), req.getPreviousUrl());
  137. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  138. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.co.jp"), req.getHost());
  139. CPPUNIT_ASSERT_EQUAL(80, req.getPort());
  140. CPPUNIT_ASSERT_EQUAL(string("/"), req.getDir());
  141. CPPUNIT_ASSERT_EQUAL(string(""), req.getFile());
  142. }
  143. void RequestTest::testRedirectUrl2() {
  144. Request req;
  145. bool v = req.setUrl("http://aria.rednoah.com/download.html");
  146. CPPUNIT_ASSERT_EQUAL(string(""), req.getPreviousUrl());
  147. req.setReferer("http://aria.rednoah.com/");
  148. // previousUrl is updated when referer is specified
  149. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getPreviousUrl());
  150. bool v2 = req.redirectUrl("http://aria.rednoah.com/403.html");
  151. // previousUrl is updated
  152. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/download.html"), req.getPreviousUrl());
  153. // referer is unchagned
  154. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/"), req.getReferer());
  155. bool v3 = req.redirectUrl("http://aria.rednoah.com/error.html");
  156. // previousUrl is update
  157. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com/403.html"), req.getPreviousUrl());
  158. }
  159. void RequestTest::testResetUrl() {
  160. Request req;
  161. bool v = req.setUrl("http://aria.rednoah.com:8080/aria2/index.html");
  162. req.setReferer("http://aria.rednoah.com:8080/");
  163. bool v2 = req.redirectUrl("ftp://aria.rednoah.co.jp/");
  164. bool v3 = req.resetUrl();
  165. CPPUNIT_ASSERT(v3);
  166. // currentUrl must equal to url
  167. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/aria2/index.html"), req.getUrl());
  168. CPPUNIT_ASSERT_EQUAL(req.getUrl(), req.getCurrentUrl());
  169. // previousUrl must equal to referer
  170. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/"), req.getPreviousUrl());
  171. // referer is unchanged
  172. CPPUNIT_ASSERT_EQUAL(string("http://aria.rednoah.com:8080/"), req.getReferer());
  173. CPPUNIT_ASSERT_EQUAL(string("http"), req.getProtocol());
  174. CPPUNIT_ASSERT_EQUAL(8080, req.getPort());
  175. CPPUNIT_ASSERT_EQUAL(string("aria.rednoah.com"), req.getHost());
  176. CPPUNIT_ASSERT_EQUAL(string("/aria2"), req.getDir());
  177. CPPUNIT_ASSERT_EQUAL(string("index.html"), req.getFile());
  178. }
  179. void RequestTest::testSafeChar() {
  180. Request req;
  181. bool v = req.setUrl("http://aria.rednoah.com/|<>");
  182. CPPUNIT_ASSERT(!v);
  183. }