Cookies.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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->format_for_header());
  9. $this->assertEquals('requests-testcookie=testvalue', $cookie->format_for_set_cookie());
  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->format_for_header());
  18. $this->assertEquals('requests-testcookie=testvalue; httponly; path=/', $cookie->format_for_set_cookie());
  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. /**
  101. * @depends testSendingCookie
  102. */
  103. public function testCookieExpiration() {
  104. $options = array(
  105. 'follow_redirects' => true,
  106. );
  107. $url = httpbin('/cookies/set/testcookie/testvalue');
  108. $url .= '?expiry=1';
  109. $response = Requests::get($url, array(), $options);
  110. $response->throw_for_status();
  111. $data = json_decode($response->body, true);
  112. $this->assertEmpty($data['cookies']);
  113. }
  114. public function testSendingCookieWithJar() {
  115. $cookies = new Requests_Cookie_Jar(array(
  116. 'requests-testcookie1' => 'testvalue1',
  117. ));
  118. $data = $this->setCookieRequest($cookies);
  119. $this->assertArrayHasKey('requests-testcookie1', $data);
  120. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  121. }
  122. public function testSendingMultipleCookies() {
  123. $cookies = array(
  124. 'requests-testcookie1' => 'testvalue1',
  125. 'requests-testcookie2' => 'testvalue2',
  126. );
  127. $data = $this->setCookieRequest($cookies);
  128. $this->assertArrayHasKey('requests-testcookie1', $data);
  129. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  130. $this->assertArrayHasKey('requests-testcookie2', $data);
  131. $this->assertEquals('testvalue2', $data['requests-testcookie2']);
  132. }
  133. public function testSendingMultipleCookiesWithJar() {
  134. $cookies = new Requests_Cookie_Jar(array(
  135. 'requests-testcookie1' => 'testvalue1',
  136. 'requests-testcookie2' => 'testvalue2',
  137. ));
  138. $data = $this->setCookieRequest($cookies);
  139. $this->assertArrayHasKey('requests-testcookie1', $data);
  140. $this->assertEquals('testvalue1', $data['requests-testcookie1']);
  141. $this->assertArrayHasKey('requests-testcookie2', $data);
  142. $this->assertEquals('testvalue2', $data['requests-testcookie2']);
  143. }
  144. public function testSendingPrebakedCookie() {
  145. $cookies = new Requests_Cookie_Jar(array(
  146. new Requests_Cookie('requests-testcookie', 'testvalue'),
  147. ));
  148. $data = $this->setCookieRequest($cookies);
  149. $this->assertArrayHasKey('requests-testcookie', $data);
  150. $this->assertEquals('testvalue', $data['requests-testcookie']);
  151. }
  152. public function domainMatchProvider() {
  153. return array(
  154. array('example.com', 'example.com', true, true),
  155. array('example.com', 'www.example.com', false, true),
  156. array('example.com', 'example.net', false, false),
  157. // Leading period
  158. array('.example.com', 'example.com', true, true),
  159. array('.example.com', 'www.example.com', false, true),
  160. array('.example.com', 'example.net', false, false),
  161. // Prefix, but not subdomain
  162. array('example.com', 'notexample.com', false, false),
  163. array('example.com', 'notexample.net', false, false),
  164. // Reject IP address prefixes
  165. array('127.0.0.1', '127.0.0.1', true, true),
  166. array('127.0.0.1', 'abc.127.0.0.1', false, false),
  167. array('127.0.0.1', 'example.com', false, false),
  168. // Check that we're checking the actual length
  169. array('127.com', 'test.127.com', false, true),
  170. );
  171. }
  172. /**
  173. * @dataProvider domainMatchProvider
  174. */
  175. public function testDomainExactMatch($original, $check, $matches, $domain_matches) {
  176. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  177. $attributes['domain'] = $original;
  178. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  179. $this->assertEquals($matches, $cookie->domain_matches($check));
  180. }
  181. /**
  182. * @dataProvider domainMatchProvider
  183. */
  184. public function testDomainMatch($original, $check, $matches, $domain_matches) {
  185. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  186. $attributes['domain'] = $original;
  187. $flags = array(
  188. 'host-only' => false
  189. );
  190. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
  191. $this->assertEquals($domain_matches, $cookie->domain_matches($check));
  192. }
  193. public function pathMatchProvider() {
  194. return array(
  195. array('/', '', true),
  196. array('/', '/', true),
  197. array('/', '/test', true),
  198. array('/', '/test/', true),
  199. array('/test', '/', false),
  200. array('/test', '/test', true),
  201. array('/test', '/testing', false),
  202. array('/test', '/test/', true),
  203. array('/test', '/test/ing', true),
  204. array('/test', '/test/ing/', true),
  205. array('/test/', '/test/', true),
  206. array('/test/', '/', false),
  207. );
  208. }
  209. /**
  210. * @dataProvider pathMatchProvider
  211. */
  212. public function testPathMatch($original, $check, $matches) {
  213. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  214. $attributes['path'] = $original;
  215. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  216. $this->assertEquals($matches, $cookie->path_matches($check));
  217. }
  218. public function urlMatchProvider() {
  219. return array(
  220. // Domain handling
  221. array( 'example.com', '/', 'http://example.com/', true, true ),
  222. array( 'example.com', '/', 'http://www.example.com/', false, true ),
  223. array( 'example.com', '/', 'http://example.net/', false, false ),
  224. array( 'example.com', '/', 'http://www.example.net/', false, false ),
  225. // /test
  226. array( 'example.com', '/test', 'http://example.com/', false, false ),
  227. array( 'example.com', '/test', 'http://www.example.com/', false, false ),
  228. array( 'example.com', '/test', 'http://example.com/test', true, true ),
  229. array( 'example.com', '/test', 'http://www.example.com/test', false, true ),
  230. array( 'example.com', '/test', 'http://example.com/testing', false, false ),
  231. array( 'example.com', '/test', 'http://www.example.com/testing', false, false ),
  232. array( 'example.com', '/test', 'http://example.com/test/', true, true ),
  233. array( 'example.com', '/test', 'http://www.example.com/test/', false, true ),
  234. // /test/
  235. array( 'example.com', '/test/', 'http://example.com/', false, false ),
  236. array( 'example.com', '/test/', 'http://www.example.com/', false, false ),
  237. );
  238. }
  239. /**
  240. * @depends testDomainExactMatch
  241. * @depends testPathMatch
  242. * @dataProvider urlMatchProvider
  243. */
  244. public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matches) {
  245. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  246. $attributes['domain'] = $domain;
  247. $attributes['path'] = $path;
  248. $check = new Requests_IRI($check);
  249. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
  250. $this->assertEquals($matches, $cookie->uri_matches($check));
  251. }
  252. /**
  253. * @depends testDomainMatch
  254. * @depends testPathMatch
  255. * @dataProvider urlMatchProvider
  256. */
  257. public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) {
  258. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  259. $attributes['domain'] = $domain;
  260. $attributes['path'] = $path;
  261. $flags = array(
  262. 'host-only' => false
  263. );
  264. $check = new Requests_IRI($check);
  265. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
  266. $this->assertEquals($domain_matches, $cookie->uri_matches($check));
  267. }
  268. public function testUrlMatchSecure() {
  269. $attributes = new Requests_Utility_CaseInsensitiveDictionary();
  270. $attributes['domain'] = 'example.com';
  271. $attributes['path'] = '/';
  272. $attributes['secure'] = true;
  273. $flags = array(
  274. 'host-only' => false,
  275. );
  276. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
  277. $this->assertTrue($cookie->uri_matches(new Requests_IRI('https://example.com/')));
  278. $this->assertFalse($cookie->uri_matches(new Requests_IRI('http://example.com/')));
  279. // Double-check host-only
  280. $this->assertTrue($cookie->uri_matches(new Requests_IRI('https://www.example.com/')));
  281. $this->assertFalse($cookie->uri_matches(new Requests_IRI('http://www.example.com/')));
  282. }
  283. /**
  284. * Manually set cookies without a domain/path set should always be valid
  285. *
  286. * Cookies parsed from headers internally in Requests will always have a
  287. * domain/path set, but those created manually will not. Manual cookies
  288. * should be regarded as "global" cookies (that is, set for `.`)
  289. */
  290. public function testUrlMatchManuallySet() {
  291. $cookie = new Requests_Cookie('requests-testcookie', 'testvalue');
  292. $this->assertTrue($cookie->domain_matches('example.com'));
  293. $this->assertTrue($cookie->domain_matches('example.net'));
  294. $this->assertTrue($cookie->path_matches('/'));
  295. $this->assertTrue($cookie->path_matches('/test'));
  296. $this->assertTrue($cookie->path_matches('/test/'));
  297. $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/')));
  298. $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test')));
  299. $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test/')));
  300. $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/')));
  301. $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test')));
  302. $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test/')));
  303. }
  304. public static function parseResultProvider() {
  305. return array(
  306. // Basic parsing
  307. array(
  308. 'foo=bar',
  309. array( 'name' => 'foo', 'value' => 'bar' ),
  310. ),
  311. array(
  312. 'bar',
  313. array( 'name' => '', 'value' => 'bar' ),
  314. ),
  315. // Expiration
  316. // RFC 822, updated by RFC 1123
  317. array(
  318. 'foo=bar; Expires=Thu, 5-Dec-2013 04:50:12 GMT',
  319. array( 'expired' => true ),
  320. array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2013 ) ),
  321. ),
  322. array(
  323. 'foo=bar; Expires=Fri, 5-Dec-2014 04:50:12 GMT',
  324. array( 'expired' => false ),
  325. array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2014 ) ),
  326. ),
  327. // RFC 850, obsoleted by RFC 1036
  328. array(
  329. 'foo=bar; Expires=Thursday, 5-Dec-2013 04:50:12 GMT',
  330. array( 'expired' => true ),
  331. array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2013 ) ),
  332. ),
  333. array(
  334. 'foo=bar; Expires=Friday, 5-Dec-2014 04:50:12 GMT',
  335. array( 'expired' => false ),
  336. array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2014 ) ),
  337. ),
  338. // asctime()
  339. array(
  340. 'foo=bar; Expires=Thu Dec 5 04:50:12 2013',
  341. array( 'expired' => true ),
  342. array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2013 ) ),
  343. ),
  344. array(
  345. 'foo=bar; Expires=Fri Dec 5 04:50:12 2014',
  346. array( 'expired' => false ),
  347. array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2014 ) ),
  348. ),
  349. array(
  350. // Invalid
  351. 'foo=bar; Expires=never',
  352. array(),
  353. array( 'expires' => null ),
  354. ),
  355. // Max-Age
  356. array(
  357. 'foo=bar; Max-Age=10',
  358. array( 'expired' => false ),
  359. array( 'max-age' => gmmktime( 0, 0, 10, 1, 1, 2014 ) ),
  360. ),
  361. array(
  362. 'foo=bar; Max-Age=3660',
  363. array( 'expired' => false ),
  364. array( 'max-age' => gmmktime( 1, 1, 0, 1, 1, 2014 ) ),
  365. ),
  366. array(
  367. 'foo=bar; Max-Age=0',
  368. array( 'expired' => true ),
  369. array( 'max-age' => 0 ),
  370. ),
  371. array(
  372. 'foo=bar; Max-Age=-1000',
  373. array( 'expired' => true ),
  374. array( 'max-age' => 0 ),
  375. ),
  376. array(
  377. // Invalid (non-digit character)
  378. 'foo=bar; Max-Age=1e6',
  379. array( 'expired' => false ),
  380. array( 'max-age' => null ),
  381. )
  382. );
  383. }
  384. protected function check_parsed_cookie($cookie, $expected, $expected_attributes, $expected_flags = array()) {
  385. if (isset($expected['name'])) {
  386. $this->assertEquals($expected['name'], $cookie->name);
  387. }
  388. if (isset($expected['value'])) {
  389. $this->assertEquals($expected['value'], $cookie->value);
  390. }
  391. if (isset($expected['expired'])) {
  392. $this->assertEquals($expected['expired'], $cookie->is_expired());
  393. }
  394. if (isset($expected_attributes)) {
  395. foreach ($expected_attributes as $attr_key => $attr_val) {
  396. $this->assertEquals($attr_val, $cookie->attributes[$attr_key], "$attr_key should match supplied");
  397. }
  398. }
  399. if (isset($expected_flags)) {
  400. foreach ($expected_flags as $flag_key => $flag_val) {
  401. $this->assertEquals($flag_val, $cookie->flags[$flag_key], "$flag_key should match supplied");
  402. }
  403. }
  404. }
  405. /**
  406. * @dataProvider parseResultProvider
  407. */
  408. public function testParsingHeader($header, $expected, $expected_attributes = array(), $expected_flags = array()) {
  409. // Set the reference time to 2014-01-01 00:00:00
  410. $reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
  411. $cookie = Requests_Cookie::parse($header, null, $reference_time);
  412. $this->check_parsed_cookie($cookie, $expected, $expected_attributes);
  413. }
  414. /**
  415. * Double-normalizes the cookie data to ensure we catch any issues there
  416. *
  417. * @dataProvider parseResultProvider
  418. */
  419. public function testParsingHeaderDouble($header, $expected, $expected_attributes = array(), $expected_flags = array()) {
  420. // Set the reference time to 2014-01-01 00:00:00
  421. $reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
  422. $cookie = Requests_Cookie::parse($header, null, $reference_time);
  423. // Normalize the value again
  424. $cookie->normalize();
  425. $this->check_parsed_cookie($cookie, $expected, $expected_attributes, $expected_flags);
  426. }
  427. /**
  428. * @dataProvider parseResultProvider
  429. */
  430. public function testParsingHeaderObject($header, $expected, $expected_attributes = array(), $expected_flags = array()) {
  431. $headers = new Requests_Response_Headers();
  432. $headers['Set-Cookie'] = $header;
  433. // Set the reference time to 2014-01-01 00:00:00
  434. $reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
  435. $parsed = Requests_Cookie::parse_from_headers($headers, null, $reference_time);
  436. $this->assertCount(1, $parsed);
  437. $cookie = reset($parsed);
  438. $this->check_parsed_cookie($cookie, $expected, $expected_attributes);
  439. }
  440. public function parseFromHeadersProvider() {
  441. return array(
  442. # Varying origin path
  443. array(
  444. 'name=value',
  445. 'http://example.com/',
  446. array(),
  447. array( 'path' => '/' ),
  448. array( 'host-only' => true ),
  449. ),
  450. array(
  451. 'name=value',
  452. 'http://example.com/test',
  453. array(),
  454. array( 'path' => '/' ),
  455. array( 'host-only' => true ),
  456. ),
  457. array(
  458. 'name=value',
  459. 'http://example.com/test/',
  460. array(),
  461. array( 'path' => '/test' ),
  462. array( 'host-only' => true ),
  463. ),
  464. array(
  465. 'name=value',
  466. 'http://example.com/test/abc',
  467. array(),
  468. array( 'path' => '/test' ),
  469. array( 'host-only' => true ),
  470. ),
  471. array(
  472. 'name=value',
  473. 'http://example.com/test/abc/',
  474. array(),
  475. array( 'path' => '/test/abc' ),
  476. array( 'host-only' => true ),
  477. ),
  478. # With specified path
  479. array(
  480. 'name=value; path=/',
  481. 'http://example.com/',
  482. array(),
  483. array( 'path' => '/' ),
  484. array( 'host-only' => true ),
  485. ),
  486. array(
  487. 'name=value; path=/test',
  488. 'http://example.com/',
  489. array(),
  490. array( 'path' => '/test' ),
  491. array( 'host-only' => true ),
  492. ),
  493. array(
  494. 'name=value; path=/test/',
  495. 'http://example.com/',
  496. array(),
  497. array( 'path' => '/test/' ),
  498. array( 'host-only' => true ),
  499. ),
  500. # Invalid path
  501. array(
  502. 'name=value; path=yolo',
  503. 'http://example.com/',
  504. array(),
  505. array( 'path' => '/' ),
  506. array( 'host-only' => true ),
  507. ),
  508. array(
  509. 'name=value; path=yolo',
  510. 'http://example.com/test/',
  511. array(),
  512. array( 'path' => '/test' ),
  513. array( 'host-only' => true ),
  514. ),
  515. # Cross-origin cookies, reject!
  516. array(
  517. 'name=value; domain=example.org',
  518. 'http://example.com/',
  519. array( 'invalid' => false ),
  520. ),
  521. # Subdomain cookies
  522. array(
  523. 'name=value; domain=test.example.com',
  524. 'http://test.example.com/',
  525. array(),
  526. array( 'domain' => 'test.example.com' ),
  527. array( 'host-only' => false )
  528. ),
  529. array(
  530. 'name=value; domain=example.com',
  531. 'http://test.example.com/',
  532. array(),
  533. array( 'domain' => 'example.com' ),
  534. array( 'host-only' => false )
  535. ),
  536. );
  537. }
  538. /**
  539. * @dataProvider parseFromHeadersProvider
  540. */
  541. public function testParsingHeaderWithOrigin($header, $origin, $expected, $expected_attributes = array(), $expected_flags = array()) {
  542. $origin = new Requests_IRI($origin);
  543. $headers = new Requests_Response_Headers();
  544. $headers['Set-Cookie'] = $header;
  545. // Set the reference time to 2014-01-01 00:00:00
  546. $reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
  547. $parsed = Requests_Cookie::parse_from_headers($headers, $origin, $reference_time);
  548. if (isset($expected['invalid'])) {
  549. $this->assertCount(0, $parsed);
  550. return;
  551. }
  552. $this->assertCount(1, $parsed);
  553. $cookie = reset($parsed);
  554. $this->check_parsed_cookie($cookie, $expected, $expected_attributes, $expected_flags);
  555. }
  556. }