Session.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class RequestsTest_Session extends PHPUnit_Framework_TestCase {
  3. public function testPropertyUsage() {
  4. $headers = array(
  5. 'X-TestHeader' => 'testing',
  6. 'X-TestHeader2' => 'requests-test'
  7. );
  8. $data = array(
  9. 'testdata' => 'value1',
  10. 'test2' => 'value2',
  11. 'test3' => array(
  12. 'foo' => 'bar',
  13. 'abc' => 'xyz'
  14. )
  15. );
  16. $options = array(
  17. 'testoption' => 'test',
  18. 'foo' => 'bar'
  19. );
  20. $session = new Requests_Session('http://example.com/', $headers, $data, $options);
  21. $this->assertEquals('http://example.com/', $session->url);
  22. $this->assertEquals($headers, $session->headers);
  23. $this->assertEquals($data, $session->data);
  24. $this->assertEquals($options['testoption'], $session->options['testoption']);
  25. // Test via property access
  26. $this->assertEquals($options['testoption'], $session->testoption);
  27. // Test setting new property
  28. $session->newoption = 'foobar';
  29. $options['newoption'] = 'foobar';
  30. $this->assertEquals($options['newoption'], $session->options['newoption']);
  31. // Test unsetting property
  32. unset($session->newoption);
  33. $this->assertFalse(isset($session->newoption));
  34. // Update property
  35. $session->testoption = 'foobar';
  36. $options['testoption'] = 'foobar';
  37. $this->assertEquals($options['testoption'], $session->testoption);
  38. // Test getting invalid property
  39. $this->assertNull($session->invalidoption);
  40. }
  41. public function testURLResolution() {
  42. $session = new Requests_Session(httpbin('/'));
  43. // Set the cookies up
  44. $response = $session->get('/get');
  45. $this->assertTrue($response->success);
  46. $this->assertEquals(httpbin('/get'), $response->url);
  47. $data = json_decode($response->body, true);
  48. $this->assertNotNull($data);
  49. $this->assertArrayHasKey('url', $data);
  50. $this->assertEquals(httpbin('/get'), $data['url']);
  51. }
  52. public function testSharedCookies() {
  53. $session = new Requests_Session(httpbin('/'));
  54. $options = array(
  55. 'follow_redirects' => false
  56. );
  57. $response = $session->get('/cookies/set?requests-testcookie=testvalue', array(), $options);
  58. $this->assertEquals(302, $response->status_code);
  59. // Check the cookies
  60. $response = $session->get('/cookies');
  61. $this->assertTrue($response->success);
  62. // Check the response
  63. $data = json_decode($response->body, true);
  64. $this->assertNotNull($data);
  65. $this->assertArrayHasKey('cookies', $data);
  66. $cookies = array(
  67. 'requests-testcookie' => 'testvalue'
  68. );
  69. $this->assertEquals($cookies, $data['cookies']);
  70. }
  71. }