common.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. require dirname(__FILE__).'/../OAuth.php';
  3. /**
  4. * A simple utils class for methods needed
  5. * during some of the tests
  6. */
  7. class OAuthTestUtils {
  8. private static function reset_request_vars() {
  9. $_SERVER = array();
  10. $_POST = array();
  11. $_GET = array();
  12. }
  13. /**
  14. * Populates $_{SERVER,GET,POST} and whatever environment-variables needed to test everything..
  15. *
  16. * @param string $method GET or POST
  17. * @param string $uri What URI is the request to (eg http://example.com/foo?bar=baz)
  18. * @param string $post_data What should the post-data be
  19. * @param string $auth_header What to set the Authorization header to
  20. */
  21. public static function build_request( $method, $uri, $post_data = '', $auth_header = '' ) {
  22. self::reset_request_vars();
  23. $method = strtoupper($method);
  24. $parts = parse_url($uri);
  25. $scheme = $parts['scheme'];
  26. $port = isset( $parts['port'] ) && $parts['port'] ? $parts['port'] : ( $scheme === 'https' ? '443' : '80' );
  27. $host = $parts['host'];
  28. $path = isset( $parts['path'] ) ? $parts['path'] : NULL;
  29. $query = isset( $parts['query'] ) ? $parts['query'] : NULL;
  30. if( $scheme == 'https') {
  31. $_SERVER['HTTPS'] = 'on';
  32. }
  33. $_SERVER['REQUEST_METHOD'] = $method;
  34. $_SERVER['HTTP_HOST'] = $host;
  35. $_SERVER['SERVER_NAME'] = $host;
  36. $_SERVER['SERVER_PORT'] = $port;
  37. $_SERVER['SCRIPT_NAME'] = $path;
  38. $_SERVER['REQUEST_URI'] = $path . '?' . $query;
  39. $_SERVER['QUERY_STRING'] = $query.'';
  40. parse_str($query, $_GET);
  41. if( $method == 'POST' ) {
  42. $_SERVER['HTTP_CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  43. $_POST = parse_str($post_data);
  44. OAuthRequest::$POST_INPUT = 'data:application/x-www-form-urlencoded,'.$post_data;
  45. }
  46. if( $auth_header != '' ) {
  47. $_SERVER['HTTP_AUTHORIZATION'] = $auth_header;
  48. }
  49. }
  50. }