Session.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. class RequestsTest_Session extends PHPUnit_Framework_TestCase {
  3. public function testURLResolution() {
  4. $session = new Requests_Session(httpbin('/'));
  5. // Set the cookies up
  6. $response = $session->get('/get');
  7. $this->assertTrue($response->success);
  8. $this->assertEquals(httpbin('/get'), $response->url);
  9. $data = json_decode($response->body, true);
  10. $this->assertNotNull($data);
  11. $this->assertArrayHasKey('url', $data);
  12. $this->assertEquals(httpbin('/get'), $data['url']);
  13. }
  14. public function testBasicGET() {
  15. $session_headers = array(
  16. 'X-Requests-Session' => 'BasicGET',
  17. 'X-Requests-Request' => 'notset',
  18. );
  19. $session = new Requests_Session(httpbin('/'), $session_headers);
  20. $response = $session->get('/get', array('X-Requests-Request' => 'GET'));
  21. $response->throw_for_status(false);
  22. $this->assertEquals(200, $response->status_code);
  23. $data = json_decode($response->body, true);
  24. $this->assertArrayHasKey('X-Requests-Session', $data['headers']);
  25. $this->assertEquals('BasicGET', $data['headers']['X-Requests-Session']);
  26. $this->assertArrayHasKey('X-Requests-Request', $data['headers']);
  27. $this->assertEquals('GET', $data['headers']['X-Requests-Request']);
  28. }
  29. public function testBasicHEAD() {
  30. $session_headers = array(
  31. 'X-Requests-Session' => 'BasicHEAD',
  32. 'X-Requests-Request' => 'notset',
  33. );
  34. $session = new Requests_Session(httpbin('/'), $session_headers);
  35. $response = $session->head('/get', array('X-Requests-Request' => 'HEAD'));
  36. $response->throw_for_status(false);
  37. $this->assertEquals(200, $response->status_code);
  38. }
  39. public function testBasicDELETE() {
  40. $session_headers = array(
  41. 'X-Requests-Session' => 'BasicDELETE',
  42. 'X-Requests-Request' => 'notset',
  43. );
  44. $session = new Requests_Session(httpbin('/'), $session_headers);
  45. $response = $session->delete('/delete', array('X-Requests-Request' => 'DELETE'));
  46. $response->throw_for_status(false);
  47. $this->assertEquals(200, $response->status_code);
  48. $data = json_decode($response->body, true);
  49. $this->assertArrayHasKey('X-Requests-Session', $data['headers']);
  50. $this->assertEquals('BasicDELETE', $data['headers']['X-Requests-Session']);
  51. $this->assertArrayHasKey('X-Requests-Request', $data['headers']);
  52. $this->assertEquals('DELETE', $data['headers']['X-Requests-Request']);
  53. }
  54. public function testBasicPOST() {
  55. $session_headers = array(
  56. 'X-Requests-Session' => 'BasicPOST',
  57. 'X-Requests-Request' => 'notset',
  58. );
  59. $session = new Requests_Session(httpbin('/'), $session_headers);
  60. $response = $session->post('/post', array('X-Requests-Request' => 'POST'), array('postdata' => 'exists'));
  61. $response->throw_for_status(false);
  62. $this->assertEquals(200, $response->status_code);
  63. $data = json_decode($response->body, true);
  64. $this->assertArrayHasKey('X-Requests-Session', $data['headers']);
  65. $this->assertEquals('BasicPOST', $data['headers']['X-Requests-Session']);
  66. $this->assertArrayHasKey('X-Requests-Request', $data['headers']);
  67. $this->assertEquals('POST', $data['headers']['X-Requests-Request']);
  68. }
  69. public function testBasicPUT() {
  70. $session_headers = array(
  71. 'X-Requests-Session' => 'BasicPUT',
  72. 'X-Requests-Request' => 'notset',
  73. );
  74. $session = new Requests_Session(httpbin('/'), $session_headers);
  75. $response = $session->put('/put', array('X-Requests-Request' => 'PUT'), array('postdata' => 'exists'));
  76. $response->throw_for_status(false);
  77. $this->assertEquals(200, $response->status_code);
  78. $data = json_decode($response->body, true);
  79. $this->assertArrayHasKey('X-Requests-Session', $data['headers']);
  80. $this->assertEquals('BasicPUT', $data['headers']['X-Requests-Session']);
  81. $this->assertArrayHasKey('X-Requests-Request', $data['headers']);
  82. $this->assertEquals('PUT', $data['headers']['X-Requests-Request']);
  83. }
  84. public function testBasicPATCH() {
  85. $session_headers = array(
  86. 'X-Requests-Session' => 'BasicPATCH',
  87. 'X-Requests-Request' => 'notset',
  88. );
  89. $session = new Requests_Session(httpbin('/'), $session_headers);
  90. $response = $session->patch('/patch', array('X-Requests-Request' => 'PATCH'), array('postdata' => 'exists'));
  91. $response->throw_for_status(false);
  92. $this->assertEquals(200, $response->status_code);
  93. $data = json_decode($response->body, true);
  94. $this->assertArrayHasKey('X-Requests-Session', $data['headers']);
  95. $this->assertEquals('BasicPATCH', $data['headers']['X-Requests-Session']);
  96. $this->assertArrayHasKey('X-Requests-Request', $data['headers']);
  97. $this->assertEquals('PATCH', $data['headers']['X-Requests-Request']);
  98. }
  99. public function testMultiple() {
  100. $session = new Requests_Session(httpbin('/'), array('X-Requests-Session' => 'Multiple'));
  101. $requests = array(
  102. 'test1' => array(
  103. 'url' => httpbin('/get')
  104. ),
  105. 'test2' => array(
  106. 'url' => httpbin('/get')
  107. ),
  108. );
  109. $responses = $session->request_multiple($requests);
  110. // test1
  111. $this->assertNotEmpty($responses['test1']);
  112. $this->assertInstanceOf('Requests_Response', $responses['test1']);
  113. $this->assertEquals(200, $responses['test1']->status_code);
  114. $result = json_decode($responses['test1']->body, true);
  115. $this->assertEquals(httpbin('/get'), $result['url']);
  116. $this->assertEmpty($result['args']);
  117. // test2
  118. $this->assertNotEmpty($responses['test2']);
  119. $this->assertInstanceOf('Requests_Response', $responses['test2']);
  120. $this->assertEquals(200, $responses['test2']->status_code);
  121. $result = json_decode($responses['test2']->body, true);
  122. $this->assertEquals(httpbin('/get'), $result['url']);
  123. $this->assertEmpty($result['args']);
  124. }
  125. public function testPropertyUsage() {
  126. $headers = array(
  127. 'X-TestHeader' => 'testing',
  128. 'X-TestHeader2' => 'requests-test'
  129. );
  130. $data = array(
  131. 'testdata' => 'value1',
  132. 'test2' => 'value2',
  133. 'test3' => array(
  134. 'foo' => 'bar',
  135. 'abc' => 'xyz'
  136. )
  137. );
  138. $options = array(
  139. 'testoption' => 'test',
  140. 'foo' => 'bar'
  141. );
  142. $session = new Requests_Session('http://example.com/', $headers, $data, $options);
  143. $this->assertEquals('http://example.com/', $session->url);
  144. $this->assertEquals($headers, $session->headers);
  145. $this->assertEquals($data, $session->data);
  146. $this->assertEquals($options['testoption'], $session->options['testoption']);
  147. // Test via property access
  148. $this->assertEquals($options['testoption'], $session->testoption);
  149. // Test setting new property
  150. $session->newoption = 'foobar';
  151. $options['newoption'] = 'foobar';
  152. $this->assertEquals($options['newoption'], $session->options['newoption']);
  153. // Test unsetting property
  154. unset($session->newoption);
  155. $this->assertFalse(isset($session->newoption));
  156. // Update property
  157. $session->testoption = 'foobar';
  158. $options['testoption'] = 'foobar';
  159. $this->assertEquals($options['testoption'], $session->testoption);
  160. // Test getting invalid property
  161. $this->assertNull($session->invalidoption);
  162. }
  163. public function testSharedCookies() {
  164. $session = new Requests_Session(httpbin('/'));
  165. $options = array(
  166. 'follow_redirects' => false
  167. );
  168. $response = $session->get('/cookies/set?requests-testcookie=testvalue', array(), $options);
  169. $this->assertEquals(302, $response->status_code);
  170. // Check the cookies
  171. $response = $session->get('/cookies');
  172. $this->assertTrue($response->success);
  173. // Check the response
  174. $data = json_decode($response->body, true);
  175. $this->assertNotNull($data);
  176. $this->assertArrayHasKey('cookies', $data);
  177. $cookies = array(
  178. 'requests-testcookie' => 'testvalue'
  179. );
  180. $this->assertEquals($cookies, $data['cookies']);
  181. }
  182. }