Requests.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. class RequestsTest_Requests extends PHPUnit_Framework_TestCase {
  3. /**
  4. * @expectedException Requests_Exception
  5. */
  6. public function testInvalidProtocol() {
  7. $request = Requests::request('ftp://128.0.0.1/');
  8. }
  9. public function testDefaultTransport() {
  10. $request = Requests::get(httpbin('/get'));
  11. $this->assertEquals(200, $request->status_code);
  12. }
  13. /**
  14. * Standard response header parsing
  15. */
  16. public function testHeaderParsing() {
  17. $transport = new RawTransport();
  18. $transport->data =
  19. "HTTP/1.0 200 OK\r\n".
  20. "Host: localhost\r\n".
  21. "Host: ambiguous\r\n".
  22. "Nospace:here\r\n".
  23. "Muchspace: there \r\n".
  24. "Empty:\r\n".
  25. "Empty2: \r\n".
  26. "Folded: one\r\n".
  27. "\ttwo\r\n".
  28. " three\r\n\r\n".
  29. "stop\r\n";
  30. $options = array(
  31. 'transport' => $transport
  32. );
  33. $response = Requests::get('http://example.com/', array(), $options);
  34. $expected = new Requests_Response_Headers();
  35. $expected['host'] = 'localhost,ambiguous';
  36. $expected['nospace'] = 'here';
  37. $expected['muchspace'] = 'there';
  38. $expected['empty'] = '';
  39. $expected['empty2'] = '';
  40. $expected['folded'] = 'one two three';
  41. foreach ($expected as $key => $value) {
  42. $this->assertEquals($value, $response->headers[$key]);
  43. }
  44. foreach ($response->headers as $key => $value) {
  45. $this->assertEquals($value, $expected[$key]);
  46. }
  47. }
  48. public function testProtocolVersionParsing() {
  49. $transport = new RawTransport();
  50. $transport->data =
  51. "HTTP/1.0 200 OK\r\n".
  52. "Host: localhost\r\n\r\n";
  53. $options = array(
  54. 'transport' => $transport
  55. );
  56. $response = Requests::get('http://example.com/', array(), $options);
  57. $this->assertEquals(1.0, $response->protocol_version);
  58. }
  59. public function testRawAccess() {
  60. $transport = new RawTransport();
  61. $transport->data =
  62. "HTTP/1.0 200 OK\r\n".
  63. "Host: localhost\r\n\r\n".
  64. "Test";
  65. $options = array(
  66. 'transport' => $transport
  67. );
  68. $response = Requests::get('http://example.com/', array(), $options);
  69. $this->assertEquals($transport->data, $response->raw);
  70. }
  71. /**
  72. * Headers with only \n delimiting should be treated as if they're \r\n
  73. */
  74. public function testHeaderOnlyLF() {
  75. $transport = new RawTransport();
  76. $transport->data = "HTTP/1.0 200 OK\r\nTest: value\nAnother-Test: value\r\n\r\n";
  77. $options = array(
  78. 'transport' => $transport
  79. );
  80. $response = Requests::get('http://example.com/', array(), $options);
  81. $this->assertEquals('value', $response->headers['test']);
  82. $this->assertEquals('value', $response->headers['another-test']);
  83. }
  84. /**
  85. * Check that invalid protocols are not accepted
  86. *
  87. * We do not support HTTP/0.9. If this is really an issue for you, file a
  88. * new issue, and update your server/proxy to support a proper protocol.
  89. *
  90. * @expectedException Requests_Exception
  91. */
  92. public function testInvalidProtocolVersion() {
  93. $transport = new RawTransport();
  94. $transport->data = "HTTP/0.9 200 OK\r\n\r\n<p>Test";
  95. $options = array(
  96. 'transport' => $transport
  97. );
  98. $response = Requests::get('http://example.com/', array(), $options);
  99. }
  100. /**
  101. * HTTP/0.9 also appears to use a single CRLF instead of two
  102. *
  103. * @expectedException Requests_Exception
  104. */
  105. public function testSingleCRLFSeparator() {
  106. $transport = new RawTransport();
  107. $transport->data = "HTTP/0.9 200 OK\r\n<p>Test";
  108. $options = array(
  109. 'transport' => $transport
  110. );
  111. $response = Requests::get('http://example.com/', array(), $options);
  112. }
  113. /**
  114. * @expectedException Requests_Exception
  115. */
  116. public function testInvalidStatus() {
  117. $transport = new RawTransport();
  118. $transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";
  119. $options = array(
  120. 'transport' => $transport
  121. );
  122. $response = Requests::get('http://example.com/', array(), $options);
  123. }
  124. public function test30xWithoutLocation() {
  125. $transport = new MockTransport();
  126. $transport->code = 302;
  127. $options = array(
  128. 'transport' => $transport
  129. );
  130. $response = Requests::get('http://example.com/', array(), $options);
  131. $this->assertEquals(302, $response->status_code);
  132. $this->assertEquals(0, $response->redirects);
  133. }
  134. /**
  135. * @expectedException Requests_Exception
  136. */
  137. public function testTimeoutException() {
  138. $options = array('timeout' => 0.5);
  139. $response = Requests::get(httpbin('/delay/3'), array(), $options);
  140. }
  141. }