Requests.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 testRawAccess() {
  49. $transport = new RawTransport();
  50. $transport->data =
  51. "HTTP/1.0 200 OK\r\n".
  52. "Host: localhost\r\n\r\n".
  53. "Test";
  54. $options = array(
  55. 'transport' => $transport
  56. );
  57. $response = Requests::get('http://example.com/', array(), $options);
  58. $this->assertEquals($transport->data, $response->raw);
  59. }
  60. /**
  61. * Headers with only \n delimiting should be treated as if they're \r\n
  62. */
  63. public function testHeaderOnlyLF() {
  64. $transport = new RawTransport();
  65. $transport->data = "HTTP/1.0 200 OK\r\nTest: value\nAnother-Test: value\r\n\r\n";
  66. $options = array(
  67. 'transport' => $transport
  68. );
  69. $response = Requests::get('http://example.com/', array(), $options);
  70. $this->assertEquals('value', $response->headers['test']);
  71. $this->assertEquals('value', $response->headers['another-test']);
  72. }
  73. /**
  74. * Check that invalid protocols are not accepted
  75. *
  76. * We do not support HTTP/0.9. If this is really an issue for you, file a
  77. * new issue, and update your server/proxy to support a proper protocol.
  78. *
  79. * @expectedException Requests_Exception
  80. */
  81. public function testInvalidProtocolVersion() {
  82. $transport = new RawTransport();
  83. $transport->data = "HTTP/0.9 200 OK\r\n\r\n<p>Test";
  84. $options = array(
  85. 'transport' => $transport
  86. );
  87. $response = Requests::get('http://example.com/', array(), $options);
  88. }
  89. /**
  90. * HTTP/0.9 also appears to use a single CRLF instead of two
  91. *
  92. * @expectedException Requests_Exception
  93. */
  94. public function testSingleCRLFSeparator() {
  95. $transport = new RawTransport();
  96. $transport->data = "HTTP/0.9 200 OK\r\n<p>Test";
  97. $options = array(
  98. 'transport' => $transport
  99. );
  100. $response = Requests::get('http://example.com/', array(), $options);
  101. }
  102. /**
  103. * @expectedException Requests_Exception
  104. */
  105. public function testInvalidStatus() {
  106. $transport = new RawTransport();
  107. $transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";
  108. $options = array(
  109. 'transport' => $transport
  110. );
  111. $response = Requests::get('http://example.com/', array(), $options);
  112. }
  113. public function test30xWithoutLocation() {
  114. $transport = new MockTransport();
  115. $transport->code = 302;
  116. $options = array(
  117. 'transport' => $transport
  118. );
  119. $response = Requests::get('http://example.com/', array(), $options);
  120. $this->assertEquals(302, $response->status_code);
  121. $this->assertEquals(0, $response->redirects);
  122. }
  123. /**
  124. * @expectedException Requests_Exception
  125. */
  126. public function testTimeoutException() {
  127. $options = array('timeout' => 0.5);
  128. $response = Requests::get(httpbin('/delay/3'), array(), $options);
  129. }
  130. }