From 77d48049f031b62e91fd87584c74e3dc871eab34 Mon Sep 17 00:00:00 2001 From: costdev Date: Sat, 30 Nov 2024 11:10:53 +0000 Subject: [PATCH] Add tests for `API_Rewrite::pre_http_request()`. --- .../APIRewrite_PreHttpRequestTest.php | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php diff --git a/tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php b/tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php new file mode 100644 index 0000000..4d7805d --- /dev/null +++ b/tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php @@ -0,0 +1,131 @@ +pre_http_request( [], [], '' ); + + $this->assertSame( 0, $request->get_call_count() ); + } + + /** + * Test that no request is performed when the default host and redirected host are the same. + */ + public function test_should_not_perform_request_when_default_host_and_redirected_host_are_the_same() { + $request = new MockAction(); + add_filter( 'pre_http_request', [ $request, 'filter' ] ); + + $default_host = $this->get_default_host(); + $api_rewrite = new AspireUpdate\API_Rewrite( $default_host, false ); + + $api_rewrite->pre_http_request( [], [], $default_host ); + + $this->assertSame( 0, $request->get_call_count() ); + } + + /** + * Test that the request's original SSL verification is respected when SSL is not forcibly disabled. + */ + public function test_should_respect_the_original_ssl_verification_when_ssl_is_not_forcibly_disabled() { + $actual = ''; + + add_filter( + 'pre_http_request', + static function ( $response, $parsed_args ) use ( &$actual ) { + $actual = $parsed_args['sslverify']; + return $response; + }, + PHP_INT_MAX, + 2 + ); + + $api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', false ); + $api_rewrite->pre_http_request( + [], + [ 'sslverify' => 'original_sslverify_value' ], + $this->get_default_host() + ); + + $this->assertSame( 'original_sslverify_value', $actual ); + } + + /** + * Test that disabling SSL is respected. + */ + public function test_should_respect_disabling_ssl() { + $actual = ''; + + add_filter( + 'pre_http_request', + static function ( $response, $parsed_args ) use ( &$actual ) { + $actual = $parsed_args['sslverify']; + return $response; + }, + PHP_INT_MAX, + 2 + ); + + $api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true ); + $api_rewrite->pre_http_request( + [], + [ 'sslverify' => true ], + $this->get_default_host() + ); + + $this->assertFalse( $actual ); + } + + /** + * Test that the default host is replaced with the redirected host. + */ + public function test_should_replace_default_host_with_redirected_host() { + $actual = ''; + + add_filter( + 'pre_http_request', + static function ( $response, $parsed_args, $url ) use ( &$actual ) { + $actual = $url; + return $response; + }, + PHP_INT_MAX, + 3 + ); + + $api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true ); + $api_rewrite->pre_http_request( [], [], $this->get_default_host() ); + + $this->assertSame( 'my.api.org', $actual ); + } + + /** + * Gets the default host. + * + * @return string The default host. + */ + private function get_default_host() { + static $default_host; + + if ( ! $default_host ) { + $reflection = new ReflectionClass( 'AspireUpdate\API_Rewrite' ); + $default_host = $reflection->getDefaultProperties()['default_host']; + } + + return $default_host; + } +}