bootstrap.php 4.7 KB

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