bootstrap.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. date_default_timezone_set('UTC');
  3. $host = getenv('REQUESTS_TEST_HOST');
  4. if (empty($host)) {
  5. $host = 'httpbin.org';
  6. }
  7. define('REQUESTS_TEST_HOST', getenv('REQUESTS_TEST_HOST') ? getenv('REQUESTS_TEST_HOST') : 'httpbin.org');
  8. define('REQUESTS_TEST_HOST_HTTP', getenv('REQUESTS_TEST_HOST_HTTP') ? getenv('REQUESTS_TEST_HOST_HTTP') : REQUESTS_TEST_HOST);
  9. define('REQUESTS_TEST_HOST_HTTPS', getenv('REQUESTS_TEST_HOST_HTTPS') ? getenv('REQUESTS_TEST_HOST_HTTPS'): REQUESTS_TEST_HOST);
  10. include(dirname(dirname(__FILE__)) . '/library/Requests.php');
  11. Requests::register_autoloader();
  12. function autoload_tests($class) {
  13. if (strpos($class, 'RequestsTest_') !== 0) {
  14. return;
  15. }
  16. $class = substr($class, 13);
  17. $file = str_replace('_', '/', $class);
  18. if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
  19. require_once(dirname(__FILE__) . '/' . $file . '.php');
  20. }
  21. }
  22. spl_autoload_register('autoload_tests');
  23. function httpbin($suffix = '', $ssl = false) {
  24. $host = $ssl ? 'https://' . REQUESTS_TEST_HOST_HTTPS : 'http://' . REQUESTS_TEST_HOST_HTTP;
  25. return rtrim( $host, '/' ) . '/' . ltrim( $suffix, '/' );
  26. }
  27. class MockTransport implements Requests_Transport {
  28. public $code = 200;
  29. public $chunked = false;
  30. public $body = 'Test Body';
  31. public $raw_headers = '';
  32. private static $messages = array(
  33. 100 => '100 Continue',
  34. 101 => '101 Switching Protocols',
  35. 200 => '200 OK',
  36. 201 => '201 Created',
  37. 202 => '202 Accepted',
  38. 203 => '203 Non-Authoritative Information',
  39. 204 => '204 No Content',
  40. 205 => '205 Reset Content',
  41. 206 => '206 Partial Content',
  42. 300 => '300 Multiple Choices',
  43. 301 => '301 Moved Permanently',
  44. 302 => '302 Found',
  45. 303 => '303 See Other',
  46. 304 => '304 Not Modified',
  47. 305 => '305 Use Proxy',
  48. 306 => '306 (Unused)',
  49. 307 => '307 Temporary Redirect',
  50. 400 => '400 Bad Request',
  51. 401 => '401 Unauthorized',
  52. 402 => '402 Payment Required',
  53. 403 => '403 Forbidden',
  54. 404 => '404 Not Found',
  55. 405 => '405 Method Not Allowed',
  56. 406 => '406 Not Acceptable',
  57. 407 => '407 Proxy Authentication Required',
  58. 408 => '408 Request Timeout',
  59. 409 => '409 Conflict',
  60. 410 => '410 Gone',
  61. 411 => '411 Length Required',
  62. 412 => '412 Precondition Failed',
  63. 413 => '413 Request Entity Too Large',
  64. 414 => '414 Request-URI Too Long',
  65. 415 => '415 Unsupported Media Type',
  66. 416 => '416 Requested Range Not Satisfiable',
  67. 417 => '417 Expectation Failed',
  68. 500 => '500 Internal Server Error',
  69. 501 => '501 Not Implemented',
  70. 502 => '502 Bad Gateway',
  71. 503 => '503 Service Unavailable',
  72. 504 => '504 Gateway Timeout',
  73. 505 => '505 HTTP Version Not Supported',
  74. );
  75. public function request($url, $headers = array(), $data = array(), $options = array()) {
  76. $status = self::$messages[$this->code];
  77. $response = "HTTP/1.0 $status\r\n";
  78. $response .= "Content-Type: text/plain\r\n";
  79. if ($this->chunked) {
  80. $response .= "Transfer-Encoding: chunked\r\n";
  81. }
  82. $response .= $this->raw_headers;
  83. $response .= "Connection: close\r\n\r\n";
  84. $response .= $this->body;
  85. return $response;
  86. }
  87. public function request_multiple($requests, $options) {
  88. $responses = array();
  89. foreach ($requests as $id => $request) {
  90. $handler = new MockTransport();
  91. $handler->code = $request['options']['mock.code'];
  92. $handler->chunked = $request['options']['mock.chunked'];
  93. $handler->body = $request['options']['mock.body'];
  94. $handler->raw_headers = $request['options']['mock.raw_headers'];
  95. $responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
  96. if (!empty($options['mock.parse'])) {
  97. $request['options']['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$id], $request));
  98. $request['options']['hooks']->dispatch('multiple.request.complete', array(&$responses[$id], $id));
  99. }
  100. }
  101. return $responses;
  102. }
  103. public static function test() {
  104. return true;
  105. }
  106. }
  107. class RawTransport implements Requests_Transport {
  108. public $data = '';
  109. public function request($url, $headers = array(), $data = array(), $options = array()) {
  110. return $this->data;
  111. }
  112. public function request_multiple($requests, $options) {
  113. foreach ($requests as $id => &$request) {
  114. $handler = new RawTransport();
  115. $handler->data = $request['options']['raw.data'];
  116. $request = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
  117. }
  118. return $requests;
  119. }
  120. public static function test() {
  121. return true;
  122. }
  123. }