Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* API Rewrite: Document the `$api_key` parameter.

* Tests: Facilitate the `$api_key` parameter in `API_Rewrite` tests.

* Tests: Add an API key test for `API Rewrite`.
  • Loading branch information
costdev authored Jan 29, 2025
1 parent e23ac4a commit 5378799
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
3 changes: 2 additions & 1 deletion includes/class-api-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class API_Rewrite {
* The Constructor.
*
* @param string $redirected_host The host to redirect to.
* @param boolean $disable_ssl Disable SSL.
* @param boolean $disable_ssl Disable SSL.
* @param string $api_key The API key to use with the host.
*/
public function __construct( $redirected_host, $disable_ssl, $api_key ) {
if ( 'debug' === $redirected_host ) {
Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/tests/API_Rewrite/APIRewrite_ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class APIRewrite_ConstructTest extends WP_UnitTestCase {
* @string $method The method to hook.
*/
public function test_should_add_hooks( $hook, $method ) {
$api_rewrite = new AspireUpdate\API_Rewrite( 'debug', false );
$api_rewrite = new AspireUpdate\API_Rewrite( 'debug', false, '' );
$this->assertIsInt( has_action( $hook, [ $api_rewrite, $method ] ) );
}

Expand Down Expand Up @@ -50,7 +50,8 @@ public function data_hooks_and_methods() {
public function test_should_set_properties( $property_name, $passed_value, $expected_value ) {
$api_rewrite = new AspireUpdate\API_Rewrite(
'redirected_host' === $property_name ? $passed_value : 'debug',
'disable_ssl' === $property_name ? $passed_value : false
'disable_ssl' === $property_name ? $passed_value : false,
''
);

if ( '%DEFAULT_HOST%' === $expected_value ) {
Expand Down
66 changes: 60 additions & 6 deletions tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_should_not_perform_request_when_redirected_host_is_an_empty
$request = new MockAction();
add_filter( 'pre_http_request', [ $request, 'filter' ] );

$api_rewrite = new AspireUpdate\API_Rewrite( '', false );
$api_rewrite = new AspireUpdate\API_Rewrite( '', false, '' );
$api_rewrite->pre_http_request( [], [], '' );

$this->assertSame( 0, $request->get_call_count() );
Expand All @@ -32,7 +32,7 @@ public function test_should_not_perform_request_when_default_host_and_redirected
add_filter( 'pre_http_request', [ $request, 'filter' ] );

$default_host = $this->get_default_host();
$api_rewrite = new AspireUpdate\API_Rewrite( $default_host, false );
$api_rewrite = new AspireUpdate\API_Rewrite( $default_host, false, '' );

$api_rewrite->pre_http_request( [], [], $default_host );

Expand All @@ -55,7 +55,7 @@ static function ( $response, $parsed_args ) use ( &$actual ) {
2
);

$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', false );
$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', false, '' );
$api_rewrite->pre_http_request(
[],
[ 'sslverify' => 'original_sslverify_value' ],
Expand All @@ -81,7 +81,7 @@ static function ( $response, $parsed_args ) use ( &$actual ) {
2
);

$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true );
$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true, '' );
$api_rewrite->pre_http_request(
[],
[ 'sslverify' => true ],
Expand All @@ -107,10 +107,64 @@ static function ( $response, $parsed_args, $url ) use ( &$actual ) {
3
);

$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true );
$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 );
$this->assertMatchesRegularExpression( '/my\.api\.org\?cache_buster=[0-9]+/', $actual );
}

/**
* Test that the API Key is added to the Authorization header.
*/
public function test_should_add_api_key_to_authorization_header_when_present() {
$actual = [];

add_filter(
'pre_http_request',
static function ( $response, $parsed_args ) use ( &$actual ) {
$actual = $parsed_args;
return $response;
},
10,
2
);

$api_key = 'MY_API_KEY';
$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true, $api_key );
$api_rewrite->pre_http_request( [], [], $this->get_default_host() );

$this->assertIsArray(
$actual,
'Parsed arguments is not an array.'
);

$this->assertArrayHasKey(
'headers',
$actual,
'The "headers" key is not present.'
);

$this->assertIsArray(
$actual['headers'],
'The "headers" value is not an array.'
);

$this->assertArrayHasKey(
'Authorization',
$actual['headers'],
'There is no authorization header.'
);

$this->assertIsString(
$actual['headers']['Authorization'],
'The authorization header is not a string.'
);

$this->assertSame(
"Bearer $api_key",
$actual['headers']['Authorization'],
'The authorization header is wrong.'
);
}

/**
Expand Down

0 comments on commit 5378799

Please sign in to comment.