Cookies.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. class RequestsTest_Cookies extends PHPUnit_Framework_TestCase {
  3. public function testBasicCookie() {
  4. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue');
  5. $this->assertEquals('requests-testcookie', $cookie->name);
  6. $this->assertEquals('testvalue', $cookie->value);
  7. $this->assertEquals('testvalue', (string) $cookie);
  8. $this->assertEquals('requests-testcookie=testvalue', $cookie->formatForHeader());
  9. $this->assertEquals('requests-testcookie=testvalue', $cookie->formatForSetCookie());
  10. }
  11. public function testCookieWithAttributes() {
  12. $attributes = array(
  13. 'httponly',
  14. 'path' => '/'
  15. );
  16. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  17. $this->assertEquals('requests-testcookie=testvalue', $cookie->formatForHeader());
  18. $this->assertEquals('requests-testcookie=testvalue; httponly; path=/', $cookie->formatForSetCookie());
  19. }
  20. public function testEmptyCookieName() {
  21. $cookie = Requests_Cookie::parse('test');
  22. $this->assertEquals('', $cookie->name);
  23. $this->assertEquals('test', $cookie->value);
  24. }
  25. public function testEmptyAttributes() {
  26. $cookie = Requests_Cookie::parse('foo=bar; HttpOnly');
  27. $this->assertTrue($cookie->attributes['httponly']);
  28. }
  29. public function testCookieJarSetter() {
  30. $jar1 = new Requests_Cookie_Jar();
  31. $jar1['requests-testcookie'] = 'testvalue';
  32. $jar2 = new Requests_Cookie_Jar(array(
  33. 'requests-testcookie' => 'testvalue',
  34. ));
  35. $this->assertEquals($jar1, $jar2);
  36. }
  37. public function testCookieJarUnsetter() {
  38. $jar = new Requests_Cookie_Jar();
  39. $jar['requests-testcookie'] = 'testvalue';
  40. $this->assertEquals('testvalue', $jar['requests-testcookie']);
  41. unset($jar['requests-testcookie']);
  42. $this->assertEmpty($jar['requests-testcookie']);
  43. $this->assertFalse(isset($jar['requests-testcookie']));
  44. }
  45. /**
  46. * @expectedException Requests_Exception
  47. */
  48. public function testCookieJarAsList() {
  49. $cookies = new Requests_Cookie_Jar();
  50. $cookies[] = 'requests-testcookie1=testvalue1';
  51. }
  52. public function testCookieJarIterator() {
  53. $cookies = array(
  54. 'requests-testcookie1' => 'testvalue1',
  55. 'requests-testcookie2' => 'testvalue2',
  56. );
  57. $jar = new Requests_Cookie_Jar($cookies);
  58. foreach ($jar as $key => $value) {
  59. $this->assertEquals($cookies[$key], $value);
  60. }
  61. }
  62. public function testReceivingCookies() {
  63. $options = array(
  64. 'follow_redirects' => false,
  65. );
  66. $url = httpbin('/cookies/set?requests-testcookie=testvalue');
  67. $response = Requests::get($url, array(), $options);
  68. $cookie = $response->cookies['requests-testcookie'];
  69. $this->assertNotEmpty( $cookie );
  70. $this->assertEquals( 'testvalue', $cookie->value );
  71. }
  72. public function testPersistenceOnRedirect() {
  73. $options = array(
  74. 'follow_redirects' => true,
  75. );
  76. $url = httpbin('/cookies/set?requests-testcookie=testvalue');
  77. $response = Requests::get($url, array(), $options);
  78. $cookie = $response->cookies['requests-testcookie'];
  79. $this->assertNotEmpty( $cookie );
  80. $this->assertEquals( 'testvalue', $cookie->value );
  81. }
  82. protected function setCookieRequest($cookies) {
  83. $options = array(
  84. 'cookies' => $cookies,
  85. );
  86. $response = Requests::get(httpbin('/cookies/set'), array(), $options);
  87. $data = json_decode($response->body, true);
  88. $this->assertInternalType('array', $data);
  89. $this->assertArrayHasKey('cookies', $data);
  90. return $data['cookies'];
  91. }
  92. public function testSendingCookie() {
  93. $cookies = array(
  94. 'requests-testcookie1' => 'testvalue1',
  95. );
  96. $data = $this->setCookieRequest($cookies);
  97. $this->assertArrayHasKey('requests-testcookie1', $data);
  98. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  99. }
  100. public function testSendingCookieWithJar() {
  101. $cookies = new Requests_Cookie_Jar(array(
  102. 'requests-testcookie1' => 'testvalue1',
  103. ));
  104. $data = $this->setCookieRequest($cookies);
  105. $this->assertArrayHasKey('requests-testcookie1', $data);
  106. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  107. }
  108. public function testSendingMultipleCookies() {
  109. $cookies = array(
  110. 'requests-testcookie1' => 'testvalue1',
  111. 'requests-testcookie2' => 'testvalue2',
  112. );
  113. $data = $this->setCookieRequest($cookies);
  114. $this->assertArrayHasKey('requests-testcookie1', $data);
  115. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  116. $this->assertArrayHasKey('requests-testcookie2', $data);
  117. $this->assertEquals('testvalue2', $data['requests-testcookie2']);
  118. }
  119. public function testSendingMultipleCookiesWithJar() {
  120. $cookies = new Requests_Cookie_Jar(array(
  121. 'requests-testcookie1' => 'testvalue1',
  122. 'requests-testcookie2' => 'testvalue2',
  123. ));
  124. $data = $this->setCookieRequest($cookies);
  125. $this->assertArrayHasKey('requests-testcookie1', $data);
  126. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  127. $this->assertArrayHasKey('requests-testcookie2', $data);
  128. $this->assertEquals('testvalue2', $data['requests-testcookie2']);
  129. }
  130. public function testSendingPrebakedCookie() {
  131. $cookies = new Requests_Cookie_Jar(array(
  132. new Requests_Cookie('requests-testcookie', 'testvalue'),
  133. ));
  134. $data = $this->setCookieRequest($cookies);
  135. $this->assertArrayHasKey('requests-testcookie', $data);
  136. $this->assertEquals('testvalue', $data['requests-testcookie']);
  137. }
  138. public function domainMatchProvider() {
  139. return array(
  140. array('example.com', 'example.com', true, true),
  141. array('example.com', 'www.example.com', false, true),
  142. array('example.com', 'example.net', false, false),
  143. // Leading period
  144. array('.example.com', 'example.com', true, true),
  145. array('.example.com', 'www.example.com', false, true),
  146. array('.example.com', 'example.net', false, false),
  147. // Prefix, but not subdomain
  148. array('example.com', 'notexample.com', false, false),
  149. array('example.com', 'notexample.net', false, false),
  150. // Reject IP address prefixes
  151. array('127.0.0.1', '127.0.0.1', true, true),
  152. array('127.0.0.1', 'abc.127.0.0.1', false, false),
  153. array('127.0.0.1', 'example.com', false, false),
  154. // Check that we're checking the actual length
  155. array('127.com', 'test.127.com', false, true),
  156. );
  157. }
  158. /**
  159. * @dataProvider domainMatchProvider
  160. */
  161. public function testDomainExactMatch($original, $check, $matches, $domain_matches) {
  162. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  163. $attributes['domain'] = $original;
  164. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  165. $this->assertEquals($matches, $cookie->domainMatches($check));
  166. }
  167. /**
  168. * @dataProvider domainMatchProvider
  169. */
  170. public function testDomainMatch($original, $check, $matches, $domain_matches) {
  171. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  172. $attributes['domain'] = $original;
  173. $flags = array(
  174. 'host-only' => false
  175. );
  176. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
  177. $this->assertEquals($domain_matches, $cookie->domainMatches($check));
  178. }
  179. public function pathMatchProvider() {
  180. return array(
  181. array('/', '/', true),
  182. array('/', '/test', true),
  183. array('/', '/test/', true),
  184. array('/test', '/', false),
  185. array('/test', '/test', true),
  186. array('/test', '/testing', false),
  187. array('/test', '/test/', true),
  188. array('/test', '/test/ing', true),
  189. array('/test', '/test/ing/', true),
  190. array('/test/', '/test/', true),
  191. array('/test/', '/', false),
  192. );
  193. }
  194. /**
  195. * @dataProvider pathMatchProvider
  196. */
  197. public function testPathMatch($original, $check, $matches) {
  198. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  199. $attributes['path'] = $original;
  200. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  201. $this->assertEquals($matches, $cookie->pathMatches($check));
  202. }
  203. public function urlMatchProvider() {
  204. return array(
  205. // Domain handling
  206. array( 'example.com', '/', 'http://example.com/', true, true ),
  207. array( 'example.com', '/', 'http://www.example.com/', false, true ),
  208. array( 'example.com', '/', 'http://example.net/', false, false ),
  209. array( 'example.com', '/', 'http://www.example.net/', false, false ),
  210. // /test
  211. array( 'example.com', '/test', 'http://example.com/', false, false ),
  212. array( 'example.com', '/test', 'http://www.example.com/', false, false ),
  213. array( 'example.com', '/test', 'http://example.com/test', true, true ),
  214. array( 'example.com', '/test', 'http://www.example.com/test', false, true ),
  215. array( 'example.com', '/test', 'http://example.com/testing', false, false ),
  216. array( 'example.com', '/test', 'http://www.example.com/testing', false, false ),
  217. array( 'example.com', '/test', 'http://example.com/test/', true, true ),
  218. array( 'example.com', '/test', 'http://www.example.com/test/', false, true ),
  219. // /test/
  220. array( 'example.com', '/test/', 'http://example.com/', false, false ),
  221. array( 'example.com', '/test/', 'http://www.example.com/', false, false ),
  222. );
  223. }
  224. /**
  225. * @depends testDomainExactMatch
  226. * @depends testPathMatch
  227. * @dataProvider urlMatchProvider
  228. */
  229. public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matches) {
  230. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  231. $attributes['domain'] = $domain;
  232. $attributes['path'] = $path;
  233. $check = new Requests_IRI($check);
  234. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  235. $this->assertEquals($matches, $cookie->uriMatches($check));
  236. }
  237. /**
  238. * @depends testDomainMatch
  239. * @depends testPathMatch
  240. * @dataProvider urlMatchProvider
  241. */
  242. public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) {
  243. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  244. $attributes['domain'] = $domain;
  245. $attributes['path'] = $path;
  246. $flags = array(
  247. 'host-only' => false
  248. );
  249. $check = new Requests_IRI($check);
  250. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
  251. $this->assertEquals($domain_matches, $cookie->uriMatches($check));
  252. }
  253. public function testUrlMatchSecure() {
  254. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  255. $attributes['domain'] = 'example.com';
  256. $attributes['path'] = '/';
  257. $attributes['secure'] = true;
  258. $flags = array(
  259. 'host-only' => false,
  260. );
  261. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
  262. $this->assertTrue($cookie->uriMatches(new Requests_IRI('https://example.com/')));
  263. $this->assertFalse($cookie->uriMatches(new Requests_IRI('http://example.com/')));
  264. // Double-check host-only
  265. $this->assertTrue($cookie->uriMatches(new Requests_IRI('https://www.example.com/')));
  266. $this->assertFalse($cookie->uriMatches(new Requests_IRI('http://www.example.com/')));
  267. }
  268. /**
  269. * Manually set cookies without a domain/path set should always be valid
  270. *
  271. * Cookies parsed from headers internally in Requests will always have a
  272. * domain/path set, but those created manually will not. Manual cookies
  273. * should be regarded as "global" cookies (that is, set for `.`)
  274. */
  275. public function testUrlMatchManuallySet() {
  276. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue');
  277. $this->assertTrue($cookie->domainMatches('example.com'));
  278. $this->assertTrue($cookie->domainMatches('example.net'));
  279. $this->assertTrue($cookie->pathMatches('/'));
  280. $this->assertTrue($cookie->pathMatches('/test'));
  281. $this->assertTrue($cookie->pathMatches('/test/'));
  282. $this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.com/')));
  283. $this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.com/test')));
  284. $this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.com/test/')));
  285. $this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.net/')));
  286. $this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.net/test')));
  287. $this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.net/test/')));
  288. }
  289. }