cookies_test.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // $Id: cookies_test.php 1506 2007-05-07 00:58:03Z lastcraft $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../cookies.php');
  5. class TestOfCookie extends UnitTestCase {
  6. function testCookieDefaults() {
  7. $cookie = new SimpleCookie("name");
  8. $this->assertFalse($cookie->getValue());
  9. $this->assertEqual($cookie->getPath(), "/");
  10. $this->assertIdentical($cookie->getHost(), false);
  11. $this->assertFalse($cookie->getExpiry());
  12. $this->assertFalse($cookie->isSecure());
  13. }
  14. function testCookieAccessors() {
  15. $cookie = new SimpleCookie(
  16. "name",
  17. "value",
  18. "/path",
  19. "Mon, 18 Nov 2002 15:50:29 GMT",
  20. true);
  21. $this->assertEqual($cookie->getName(), "name");
  22. $this->assertEqual($cookie->getValue(), "value");
  23. $this->assertEqual($cookie->getPath(), "/path/");
  24. $this->assertEqual($cookie->getExpiry(), "Mon, 18 Nov 2002 15:50:29 GMT");
  25. $this->assertTrue($cookie->isSecure());
  26. }
  27. function testFullHostname() {
  28. $cookie = new SimpleCookie("name");
  29. $this->assertTrue($cookie->setHost("host.name.here"));
  30. $this->assertEqual($cookie->getHost(), "host.name.here");
  31. $this->assertTrue($cookie->setHost("host.com"));
  32. $this->assertEqual($cookie->getHost(), "host.com");
  33. }
  34. function testHostTruncation() {
  35. $cookie = new SimpleCookie("name");
  36. $cookie->setHost("this.host.name.here");
  37. $this->assertEqual($cookie->getHost(), "host.name.here");
  38. $cookie->setHost("this.host.com");
  39. $this->assertEqual($cookie->getHost(), "host.com");
  40. $this->assertTrue($cookie->setHost("dashes.in-host.com"));
  41. $this->assertEqual($cookie->getHost(), "in-host.com");
  42. }
  43. function testBadHosts() {
  44. $cookie = new SimpleCookie("name");
  45. $this->assertFalse($cookie->setHost("gibberish"));
  46. $this->assertFalse($cookie->setHost("host.here"));
  47. $this->assertFalse($cookie->setHost("host..com"));
  48. $this->assertFalse($cookie->setHost("..."));
  49. $this->assertFalse($cookie->setHost("host.com."));
  50. }
  51. function testHostValidity() {
  52. $cookie = new SimpleCookie("name");
  53. $cookie->setHost("this.host.name.here");
  54. $this->assertTrue($cookie->isValidHost("host.name.here"));
  55. $this->assertTrue($cookie->isValidHost("that.host.name.here"));
  56. $this->assertFalse($cookie->isValidHost("bad.host"));
  57. $this->assertFalse($cookie->isValidHost("nearly.name.here"));
  58. }
  59. function testPathValidity() {
  60. $cookie = new SimpleCookie("name", "value", "/path");
  61. $this->assertFalse($cookie->isValidPath("/"));
  62. $this->assertTrue($cookie->isValidPath("/path/"));
  63. $this->assertTrue($cookie->isValidPath("/path/more"));
  64. }
  65. function testSessionExpiring() {
  66. $cookie = new SimpleCookie("name", "value", "/path");
  67. $this->assertTrue($cookie->isExpired(0));
  68. }
  69. function testTimestampExpiry() {
  70. $cookie = new SimpleCookie("name", "value", "/path", 456);
  71. $this->assertFalse($cookie->isExpired(0));
  72. $this->assertTrue($cookie->isExpired(457));
  73. $this->assertFalse($cookie->isExpired(455));
  74. }
  75. function testDateExpiry() {
  76. $cookie = new SimpleCookie(
  77. "name",
  78. "value",
  79. "/path",
  80. "Mon, 18 Nov 2002 15:50:29 GMT");
  81. $this->assertTrue($cookie->isExpired("Mon, 18 Nov 2002 15:50:30 GMT"));
  82. $this->assertFalse($cookie->isExpired("Mon, 18 Nov 2002 15:50:28 GMT"));
  83. }
  84. function testAging() {
  85. $cookie = new SimpleCookie("name", "value", "/path", 200);
  86. $cookie->agePrematurely(199);
  87. $this->assertFalse($cookie->isExpired(0));
  88. $cookie->agePrematurely(2);
  89. $this->assertTrue($cookie->isExpired(0));
  90. }
  91. }
  92. class TestOfCookieJar extends UnitTestCase {
  93. function testAddCookie() {
  94. $jar = new SimpleCookieJar();
  95. $jar->setCookie("a", "A");
  96. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=A'));
  97. }
  98. function testHostFilter() {
  99. $jar = new SimpleCookieJar();
  100. $jar->setCookie('a', 'A', 'my-host.com');
  101. $jar->setCookie('b', 'B', 'another-host.com');
  102. $jar->setCookie('c', 'C');
  103. $this->assertEqual(
  104. $jar->selectAsPairs(new SimpleUrl('my-host.com')),
  105. array('a=A', 'c=C'));
  106. $this->assertEqual(
  107. $jar->selectAsPairs(new SimpleUrl('another-host.com')),
  108. array('b=B', 'c=C'));
  109. $this->assertEqual(
  110. $jar->selectAsPairs(new SimpleUrl('www.another-host.com')),
  111. array('b=B', 'c=C'));
  112. $this->assertEqual(
  113. $jar->selectAsPairs(new SimpleUrl('new-host.org')),
  114. array('c=C'));
  115. $this->assertEqual(
  116. $jar->selectAsPairs(new SimpleUrl('/')),
  117. array('a=A', 'b=B', 'c=C'));
  118. }
  119. function testPathFilter() {
  120. $jar = new SimpleCookieJar();
  121. $jar->setCookie('a', 'A', false, '/path/');
  122. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
  123. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/elsewhere')), array());
  124. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/')), array('a=A'));
  125. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path')), array('a=A'));
  126. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/pa')), array());
  127. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/here')), array('a=A'));
  128. }
  129. function testPathFilterDeeply() {
  130. $jar = new SimpleCookieJar();
  131. $jar->setCookie('a', 'A', false, '/path/more_path/');
  132. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/')), array());
  133. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path')), array());
  134. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/pa')), array());
  135. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/more_path/')), array('a=A'));
  136. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/more_path/and_more')), array('a=A'));
  137. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/path/not_here/')), array());
  138. }
  139. function testMultipleCookieWithDifferentPathsButSameName() {
  140. $jar = new SimpleCookieJar();
  141. $jar->setCookie('a', 'abc', false, '/');
  142. $jar->setCookie('a', '123', false, '/path/here/');
  143. $this->assertEqual(
  144. $jar->selectAsPairs(new SimpleUrl('/')),
  145. array('a=abc'));
  146. $this->assertEqual(
  147. $jar->selectAsPairs(new SimpleUrl('my-host.com/')),
  148. array('a=abc'));
  149. $this->assertEqual(
  150. $jar->selectAsPairs(new SimpleUrl('my-host.com/path/')),
  151. array('a=abc'));
  152. $this->assertEqual(
  153. $jar->selectAsPairs(new SimpleUrl('my-host.com/path/here')),
  154. array('a=abc', 'a=123'));
  155. $this->assertEqual(
  156. $jar->selectAsPairs(new SimpleUrl('my-host.com/path/here/there')),
  157. array('a=abc', 'a=123'));
  158. }
  159. function testOverwrite() {
  160. $jar = new SimpleCookieJar();
  161. $jar->setCookie('a', 'abc', false, '/');
  162. $jar->setCookie('a', 'cde', false, '/');
  163. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=cde'));
  164. }
  165. function testClearSessionCookies() {
  166. $jar = new SimpleCookieJar();
  167. $jar->setCookie('a', 'A', false, '/');
  168. $jar->restartSession();
  169. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
  170. }
  171. function testExpiryFilterByDate() {
  172. $jar = new SimpleCookieJar();
  173. $jar->setCookie('a', 'A', false, '/', 'Wed, 25-Dec-02 04:24:20 GMT');
  174. $jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
  175. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=A'));
  176. $jar->restartSession("Wed, 25-Dec-02 04:24:21 GMT");
  177. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
  178. }
  179. function testExpiryFilterByAgeing() {
  180. $jar = new SimpleCookieJar();
  181. $jar->setCookie('a', 'A', false, '/', 'Wed, 25-Dec-02 04:24:20 GMT');
  182. $jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
  183. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=A'));
  184. $jar->agePrematurely(2);
  185. $jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
  186. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
  187. }
  188. function testCookieClearing() {
  189. $jar = new SimpleCookieJar();
  190. $jar->setCookie('a', 'abc', false, '/');
  191. $jar->setCookie('a', '', false, '/');
  192. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a='));
  193. }
  194. function testCookieClearByLoweringDate() {
  195. $jar = new SimpleCookieJar();
  196. $jar->setCookie('a', 'abc', false, '/', 'Wed, 25-Dec-02 04:24:21 GMT');
  197. $jar->setCookie('a', 'def', false, '/', 'Wed, 25-Dec-02 04:24:19 GMT');
  198. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array('a=def'));
  199. $jar->restartSession('Wed, 25-Dec-02 04:24:20 GMT');
  200. $this->assertEqual($jar->selectAsPairs(new SimpleUrl('/')), array());
  201. }
  202. }
  203. ?>