PageRenderTime 26ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/api/vendor/guzzle/guzzle/tests/Guzzle/Tests/Plugin/Cookie/CookieJar/ArrayCookieJarTest.php

https://gitlab.com/x33n/respond
PHP | 353 lines | 288 code | 39 blank | 26 comment | 0 complexity | aa7371181d1bc0d7dc354930e393db4a MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Plugin\Cookie\CookieJar;
  3. use Guzzle\Plugin\Cookie\Cookie;
  4. use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
  5. use Guzzle\Http\Message\Response;
  6. use Guzzle\Http\Message\Request;
  7. /**
  8. * @covers Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar
  9. */
  10. class ArrayCookieJarTest extends \Guzzle\Tests\GuzzleTestCase
  11. {
  12. /**
  13. * @var ArrayCookieJar
  14. */
  15. private $jar;
  16. public function setUp()
  17. {
  18. $this->jar = new ArrayCookieJar();
  19. }
  20. protected function getTestCookies()
  21. {
  22. return array(
  23. new Cookie(array('name' => 'foo', 'value' => 'bar', 'domain' => 'foo.com', 'path' => '/', 'discard' => true)),
  24. new Cookie(array('name' => 'test', 'value' => '123', 'domain' => 'baz.com', 'path' => '/foo', 'expires' => 2)),
  25. new Cookie(array('name' => 'you', 'value' => '123', 'domain' => 'bar.com', 'path' => '/boo', 'expires' => time() + 1000))
  26. );
  27. }
  28. /**
  29. * Provides test data for cookie cookieJar retrieval
  30. */
  31. public function getCookiesDataProvider()
  32. {
  33. return array(
  34. array(array('foo', 'baz', 'test', 'muppet', 'googoo'), '', '', '', false),
  35. array(array('foo', 'baz', 'muppet', 'googoo'), '', '', '', true),
  36. array(array('googoo'), 'www.example.com', '', '', false),
  37. array(array('muppet', 'googoo'), 'test.y.example.com', '', '', false),
  38. array(array('foo', 'baz'), 'example.com', '', '', false),
  39. array(array('muppet'), 'x.y.example.com', '/acme/', '', false),
  40. array(array('muppet'), 'x.y.example.com', '/acme/test/', '', false),
  41. array(array('googoo'), 'x.y.example.com', '/test/acme/test/', '', false),
  42. array(array('foo', 'baz'), 'example.com', '', '', false),
  43. array(array('baz'), 'example.com', '', 'baz', false),
  44. );
  45. }
  46. public function testStoresAndRetrievesCookies()
  47. {
  48. $cookies = $this->getTestCookies();
  49. foreach ($cookies as $cookie) {
  50. $this->assertTrue($this->jar->add($cookie));
  51. }
  52. $this->assertEquals(3, count($this->jar));
  53. $this->assertEquals(3, count($this->jar->getIterator()));
  54. $this->assertEquals($cookies, $this->jar->all(null, null, null, false, false));
  55. }
  56. public function testRemovesExpiredCookies()
  57. {
  58. $cookies = $this->getTestCookies();
  59. foreach ($this->getTestCookies() as $cookie) {
  60. $this->jar->add($cookie);
  61. }
  62. $this->jar->removeExpired();
  63. $this->assertEquals(array($cookies[0], $cookies[2]), $this->jar->all());
  64. }
  65. public function testRemovesTemporaryCookies()
  66. {
  67. $cookies = $this->getTestCookies();
  68. foreach ($this->getTestCookies() as $cookie) {
  69. $this->jar->add($cookie);
  70. }
  71. $this->jar->removeTemporary();
  72. $this->assertEquals(array($cookies[2]), $this->jar->all());
  73. }
  74. public function testIsSerializable()
  75. {
  76. $this->assertEquals('[]', $this->jar->serialize());
  77. $this->jar->unserialize('[]');
  78. $this->assertEquals(array(), $this->jar->all());
  79. $cookies = $this->getTestCookies();
  80. foreach ($this->getTestCookies() as $cookie) {
  81. $this->jar->add($cookie);
  82. }
  83. // Remove discard and expired cookies
  84. $serialized = $this->jar->serialize();
  85. $data = json_decode($serialized, true);
  86. $this->assertEquals(1, count($data));
  87. $a = new ArrayCookieJar();
  88. $a->unserialize($serialized);
  89. $this->assertEquals(1, count($a));
  90. }
  91. public function testRemovesSelectively()
  92. {
  93. $cookies = $this->getTestCookies();
  94. foreach ($this->getTestCookies() as $cookie) {
  95. $this->jar->add($cookie);
  96. }
  97. // Remove foo.com cookies
  98. $this->jar->remove('foo.com');
  99. $this->assertEquals(2, count($this->jar));
  100. // Try again, removing no further cookies
  101. $this->jar->remove('foo.com');
  102. $this->assertEquals(2, count($this->jar));
  103. // Remove bar.com cookies with path of /boo
  104. $this->jar->remove('bar.com', '/boo');
  105. $this->assertEquals(1, count($this->jar));
  106. // Remove cookie by name
  107. $this->jar->remove(null, null, 'test');
  108. $this->assertEquals(0, count($this->jar));
  109. }
  110. public function testDoesNotAddIncompleteCookies()
  111. {
  112. $this->assertEquals(false, $this->jar->add(new Cookie()));
  113. $this->assertFalse($this->jar->add(new Cookie(array(
  114. 'name' => 'foo'
  115. ))));
  116. $this->assertFalse($this->jar->add(new Cookie(array(
  117. 'name' => false
  118. ))));
  119. $this->assertFalse($this->jar->add(new Cookie(array(
  120. 'name' => true
  121. ))));
  122. $this->assertFalse($this->jar->add(new Cookie(array(
  123. 'name' => 'foo',
  124. 'domain' => 'foo.com'
  125. ))));
  126. }
  127. public function testDoesAddValidCookies()
  128. {
  129. $this->assertTrue($this->jar->add(new Cookie(array(
  130. 'name' => 'foo',
  131. 'domain' => 'foo.com',
  132. 'value' => 0
  133. ))));
  134. $this->assertTrue($this->jar->add(new Cookie(array(
  135. 'name' => 'foo',
  136. 'domain' => 'foo.com',
  137. 'value' => 0.0
  138. ))));
  139. $this->assertTrue($this->jar->add(new Cookie(array(
  140. 'name' => 'foo',
  141. 'domain' => 'foo.com',
  142. 'value' => '0'
  143. ))));
  144. }
  145. public function testOverwritesCookiesThatAreOlderOrDiscardable()
  146. {
  147. $t = time() + 1000;
  148. $data = array(
  149. 'name' => 'foo',
  150. 'value' => 'bar',
  151. 'domain' => '.example.com',
  152. 'path' => '/',
  153. 'max_age' => '86400',
  154. 'port' => array(80, 8080),
  155. 'version' => '1',
  156. 'secure' => true,
  157. 'discard' => true,
  158. 'expires' => $t
  159. );
  160. // Make sure that the discard cookie is overridden with the non-discard
  161. $this->assertTrue($this->jar->add(new Cookie($data)));
  162. unset($data['discard']);
  163. $this->assertTrue($this->jar->add(new Cookie($data)));
  164. $this->assertEquals(1, count($this->jar));
  165. $c = $this->jar->all();
  166. $this->assertEquals(false, $c[0]->getDiscard());
  167. // Make sure it doesn't duplicate the cookie
  168. $this->jar->add(new Cookie($data));
  169. $this->assertEquals(1, count($this->jar));
  170. // Make sure the more future-ful expiration date supersede the other
  171. $data['expires'] = time() + 2000;
  172. $this->assertTrue($this->jar->add(new Cookie($data)));
  173. $this->assertEquals(1, count($this->jar));
  174. $c = $this->jar->all();
  175. $this->assertNotEquals($t, $c[0]->getExpires());
  176. }
  177. public function testOverwritesCookiesThatHaveChanged()
  178. {
  179. $t = time() + 1000;
  180. $data = array(
  181. 'name' => 'foo',
  182. 'value' => 'bar',
  183. 'domain' => '.example.com',
  184. 'path' => '/',
  185. 'max_age' => '86400',
  186. 'port' => array(80, 8080),
  187. 'version' => '1',
  188. 'secure' => true,
  189. 'discard' => true,
  190. 'expires' => $t
  191. );
  192. // Make sure that the discard cookie is overridden with the non-discard
  193. $this->assertTrue($this->jar->add(new Cookie($data)));
  194. $data['value'] = 'boo';
  195. $this->assertTrue($this->jar->add(new Cookie($data)));
  196. $this->assertEquals(1, count($this->jar));
  197. // Changing the value plus a parameter also must overwrite the existing one
  198. $data['value'] = 'zoo';
  199. $data['secure'] = false;
  200. $this->assertTrue($this->jar->add(new Cookie($data)));
  201. $this->assertEquals(1, count($this->jar));
  202. $c = $this->jar->all();
  203. $this->assertEquals('zoo', $c[0]->getValue());
  204. }
  205. public function testAddsCookiesFromResponseWithNoRequest()
  206. {
  207. $response = new Response(200, array(
  208. 'Set-Cookie' => array(
  209. "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
  210. "FPCK3=AgBNbvoQAGpGEABZLRAAbFsQAF1tEABkDhAAeO0=; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1",
  211. "CH=deleted; expires=Wed, 03-Mar-2010 02:17:39 GMT; path=/; domain=127.0.0.1",
  212. "CH=AgBNbvoQAAEcEAApuhAAMJcQADQvEAAvGxAALe0QAD6uEAATwhAAC1AQAC8t; expires=Sat, 02-Apr-2019 02:17:40 GMT; path=/; domain=127.0.0.1"
  213. )
  214. ));
  215. $this->jar->addCookiesFromResponse($response);
  216. $this->assertEquals(3, count($this->jar));
  217. $this->assertEquals(1, count($this->jar->all(null, null, 'fpc')));
  218. $this->assertEquals(1, count($this->jar->all(null, null, 'FPCK3')));
  219. $this->assertEquals(1, count($this->jar->all(null, null, 'CH')));
  220. }
  221. public function testAddsCookiesFromResponseWithRequest()
  222. {
  223. $response = new Response(200, array(
  224. 'Set-Cookie' => "fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;"
  225. ));
  226. $request = new Request('GET', 'http://www.example.com');
  227. $this->jar->addCookiesFromResponse($response, $request);
  228. $this->assertEquals(1, count($this->jar));
  229. }
  230. public function getMatchingCookiesDataProvider()
  231. {
  232. return array(
  233. array('https://example.com', array(0)),
  234. array('http://example.com', array()),
  235. array('https://example.com:8912', array()),
  236. array('https://foo.example.com', array(0)),
  237. array('http://foo.example.com/test/acme/', array(4))
  238. );
  239. }
  240. /**
  241. * @dataProvider getMatchingCookiesDataProvider
  242. */
  243. public function testReturnsCookiesMatchingRequests($url, $cookies)
  244. {
  245. $bag = array(
  246. new Cookie(array(
  247. 'name' => 'foo',
  248. 'value' => 'bar',
  249. 'domain' => 'example.com',
  250. 'path' => '/',
  251. 'max_age' => '86400',
  252. 'port' => array(443, 8080),
  253. 'version' => '1',
  254. 'secure' => true
  255. )),
  256. new Cookie(array(
  257. 'name' => 'baz',
  258. 'value' => 'foobar',
  259. 'domain' => 'example.com',
  260. 'path' => '/',
  261. 'max_age' => '86400',
  262. 'port' => array(80, 8080),
  263. 'version' => '1',
  264. 'secure' => true
  265. )),
  266. new Cookie(array(
  267. 'name' => 'test',
  268. 'value' => '123',
  269. 'domain' => 'www.foobar.com',
  270. 'path' => '/path/',
  271. 'discard' => true
  272. )),
  273. new Cookie(array(
  274. 'name' => 'muppet',
  275. 'value' => 'cookie_monster',
  276. 'domain' => '.y.example.com',
  277. 'path' => '/acme/',
  278. 'comment' => 'Comment goes here...',
  279. 'expires' => time() + 86400
  280. )),
  281. new Cookie(array(
  282. 'name' => 'googoo',
  283. 'value' => 'gaga',
  284. 'domain' => '.example.com',
  285. 'path' => '/test/acme/',
  286. 'max_age' => 1500,
  287. 'version' => 2
  288. ))
  289. );
  290. foreach ($bag as $cookie) {
  291. $this->jar->add($cookie);
  292. }
  293. $request = new Request('GET', $url);
  294. $results = $this->jar->getMatchingCookies($request);
  295. $this->assertEquals(count($cookies), count($results));
  296. foreach ($cookies as $i) {
  297. $this->assertContains($bag[$i], $results);
  298. }
  299. }
  300. /**
  301. * @expectedException \Guzzle\Plugin\Cookie\Exception\InvalidCookieException
  302. * @expectedExceptionMessage The cookie name must not contain invalid characters: abc:@123
  303. */
  304. public function testThrowsExceptionWithStrictMode()
  305. {
  306. $a = new ArrayCookieJar();
  307. $a->setStrictMode(true);
  308. $a->add(new Cookie(array(
  309. 'name' => 'abc:@123',
  310. 'value' => 'foo',
  311. 'domain' => 'bar'
  312. )));
  313. }
  314. }