diff options
Diffstat (limited to 'OAuth/tests/phpunit/Lib/OAuthTestUtils.php')
-rw-r--r-- | OAuth/tests/phpunit/Lib/OAuthTestUtils.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/OAuth/tests/phpunit/Lib/OAuthTestUtils.php b/OAuth/tests/phpunit/Lib/OAuthTestUtils.php new file mode 100644 index 00000000..5f362543 --- /dev/null +++ b/OAuth/tests/phpunit/Lib/OAuthTestUtils.php @@ -0,0 +1,62 @@ +<?php + +namespace MediaWiki\Extensions\OAuth\Tests\Lib; + +use MediaWiki\Extensions\OAuth\Lib\OAuthRequest; + +/** + * A simple utils class for methods needed + * during some of the tests + */ +class OAuthTestUtils { + private static function reset_request_vars() { + $_SERVER = array(); + $_POST = array(); + $_GET = array(); + } + + /** + * Populates $_{SERVER,GET,POST} and whatever environment-variables needed to test everything.. + * + * @param string $method GET or POST + * @param string $uri What URI is the request to (eg http://example.com/foo?bar=baz) + * @param string $post_data What should the post-data be + * @param string $auth_header What to set the Authorization header to + */ + public static function build_request( $method, $uri, $post_data = '', $auth_header = '' ) { + self::reset_request_vars(); + + $method = strtoupper($method); + + $parts = parse_url($uri); + + $scheme = $parts['scheme']; + $port = isset( $parts['port'] ) && $parts['port'] ? $parts['port'] : ( $scheme === 'https' ? '443' : '80' ); + $host = $parts['host']; + $path = isset( $parts['path'] ) ? $parts['path'] : NULL; + $query = isset( $parts['query'] ) ? $parts['query'] : NULL; + + if( $scheme == 'https') { + $_SERVER['HTTPS'] = 'on'; + } + + $_SERVER['REQUEST_METHOD'] = $method; + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SERVER_NAME'] = $host; + $_SERVER['SERVER_PORT'] = $port; + $_SERVER['SCRIPT_NAME'] = $path; + $_SERVER['REQUEST_URI'] = $path . '?' . $query; + $_SERVER['QUERY_STRING'] = $query.''; + parse_str($query, $_GET); + + if( $method == 'POST' ) { + $_SERVER['HTTP_CONTENT_TYPE'] = 'application/x-www-form-urlencoded'; + parse_str($post_data, $_POST); + OAuthRequest::$POST_INPUT = 'data:application/x-www-form-urlencoded,'.$post_data; + } + + if( $auth_header != '' ) { + $_SERVER['HTTP_AUTHORIZATION'] = $auth_header; + } + } +} |