CookieTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Basic;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class CookieTest extends TestCase
  5. {
  6. /**
  7. * test cookie decoding.
  8. *
  9. * @group issue140
  10. */
  11. public function testIssue140()
  12. {
  13. $this->getSession()->visit($this->pathTo('/issue140.php'));
  14. $this->getSession()->getPage()->fillField('cookie_value', 'some:value;');
  15. $this->getSession()->getPage()->pressButton('Set cookie');
  16. $this->getSession()->visit($this->pathTo('/issue140.php?show_value'));
  17. $this->assertEquals('some:value;', $this->getSession()->getCookie('tc'));
  18. $this->assertEquals('some:value;', $this->getSession()->getPage()->getText());
  19. }
  20. public function testCookie()
  21. {
  22. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  23. $this->assertContains('Previous cookie: NO', $this->getSession()->getPage()->getText());
  24. $this->assertNull($this->getSession()->getCookie('srvr_cookie'));
  25. $this->getSession()->setCookie('srvr_cookie', 'client cookie set');
  26. $this->getSession()->reload();
  27. $this->assertContains('Previous cookie: client cookie set', $this->getSession()->getPage()->getText());
  28. $this->assertEquals('client cookie set', $this->getSession()->getCookie('srvr_cookie'));
  29. $this->getSession()->setCookie('srvr_cookie', null);
  30. $this->getSession()->reload();
  31. $this->assertContains('Previous cookie: NO', $this->getSession()->getPage()->getText());
  32. $this->getSession()->visit($this->pathTo('/cookie_page1.php'));
  33. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  34. $this->assertContains('Previous cookie: srv_var_is_set', $this->getSession()->getPage()->getText());
  35. $this->getSession()->setCookie('srvr_cookie', null);
  36. $this->getSession()->reload();
  37. $this->assertContains('Previous cookie: NO', $this->getSession()->getPage()->getText());
  38. }
  39. public function testCookieWithSemicolon()
  40. {
  41. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  42. $this->getSession()->setCookie('srvr_cookie', 'foo;bar;baz');
  43. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  44. $this->assertEquals('foo;bar;baz', $this->getSession()->getCookie('srvr_cookie'));
  45. $this->assertContains('Previous cookie: foo;bar;baz', $this->getSession()->getPage()->getText());
  46. }
  47. /**
  48. * @dataProvider cookieWithPathsDataProvider
  49. */
  50. public function testCookieWithPaths($cookieRemovalMode)
  51. {
  52. // start clean
  53. $session = $this->getSession();
  54. $session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
  55. $this->assertContains('Previous cookie: NO', $session->getPage()->getText());
  56. // cookie from root path is accessible in sub-folder
  57. $session->visit($this->pathTo('/cookie_page1.php'));
  58. $session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
  59. $this->assertContains('Previous cookie: srv_var_is_set', $session->getPage()->getText());
  60. // cookie from sub-folder overrides cookie from root path
  61. $session->visit($this->pathTo('/sub-folder/cookie_page1.php'));
  62. $session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
  63. $this->assertContains('Previous cookie: srv_var_is_set_sub_folder', $session->getPage()->getText());
  64. if ($cookieRemovalMode == 'session_reset') {
  65. $session->reset();
  66. } elseif ($cookieRemovalMode == 'cookie_delete') {
  67. $session->setCookie('srvr_cookie', null);
  68. }
  69. // cookie is removed from all paths
  70. $session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
  71. $this->assertContains('Previous cookie: NO', $session->getPage()->getText());
  72. }
  73. public function cookieWithPathsDataProvider()
  74. {
  75. return array(
  76. array('session_reset'),
  77. array('cookie_delete'),
  78. );
  79. }
  80. public function testReset()
  81. {
  82. $this->getSession()->visit($this->pathTo('/cookie_page1.php'));
  83. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  84. $this->assertContains('Previous cookie: srv_var_is_set', $this->getSession()->getPage()->getText());
  85. $this->getSession()->reset();
  86. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  87. $this->assertContains('Previous cookie: NO', $this->getSession()->getPage()->getText());
  88. $this->getSession()->setCookie('srvr_cookie', 'test_cookie');
  89. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  90. $this->assertContains('Previous cookie: test_cookie', $this->getSession()->getPage()->getText());
  91. $this->getSession()->reset();
  92. $this->getSession()->visit($this->pathTo('/cookie_page2.php'));
  93. $this->assertContains('Previous cookie: NO', $this->getSession()->getPage()->getText());
  94. $this->getSession()->setCookie('client_cookie1', 'some_val');
  95. $this->getSession()->setCookie('client_cookie2', 123);
  96. $this->getSession()->visit($this->pathTo('/session_test.php'));
  97. $this->getSession()->visit($this->pathTo('/cookie_page1.php'));
  98. $this->getSession()->visit($this->pathTo('/print_cookies.php'));
  99. $this->assertContains(
  100. "'client_cookie1' = 'some_val'",
  101. $this->getSession()->getPage()->getText()
  102. );
  103. $this->assertContains(
  104. "'client_cookie2' = '123'",
  105. $this->getSession()->getPage()->getText()
  106. );
  107. $this->assertContains(
  108. "_SESS' = ",
  109. $this->getSession()->getPage()->getText()
  110. );
  111. $this->assertContains(
  112. " 'srvr_cookie' = 'srv_var_is_set'",
  113. $this->getSession()->getPage()->getText()
  114. );
  115. $this->getSession()->reset();
  116. $this->getSession()->visit($this->pathTo('/print_cookies.php'));
  117. $this->assertContains('array ( )', $this->getSession()->getPage()->getText());
  118. }
  119. public function testHttpOnlyCookieIsDeleted()
  120. {
  121. $this->getSession()->restart();
  122. $this->getSession()->visit($this->pathTo('/cookie_page3.php'));
  123. $this->assertEquals('Has Cookie: false', $this->findById('cookie-status')->getText());
  124. $this->getSession()->reload();
  125. $this->assertEquals('Has Cookie: true', $this->findById('cookie-status')->getText());
  126. $this->getSession()->restart();
  127. $this->getSession()->visit($this->pathTo('/cookie_page3.php'));
  128. $this->assertEquals('Has Cookie: false', $this->findById('cookie-status')->getText());
  129. }
  130. public function testSessionPersistsBetweenRequests()
  131. {
  132. $this->getSession()->visit($this->pathTo('/session_test.php'));
  133. $webAssert = $this->getAssertSession();
  134. $node = $webAssert->elementExists('css', '#session-id');
  135. $sessionId = $node->getText();
  136. $this->getSession()->visit($this->pathTo('/session_test.php'));
  137. $node = $webAssert->elementExists('css', '#session-id');
  138. $this->assertEquals($sessionId, $node->getText());
  139. $this->getSession()->visit($this->pathTo('/session_test.php?login'));
  140. $node = $webAssert->elementExists('css', '#session-id');
  141. $this->assertNotEquals($sessionId, $newSessionId = $node->getText());
  142. $this->getSession()->visit($this->pathTo('/session_test.php'));
  143. $node = $webAssert->elementExists('css', '#session-id');
  144. $this->assertEquals($newSessionId, $node->getText());
  145. }
  146. }