Skip to content

Commit 5378799

Browse files
authored
Follow up to #268 (#271)
* 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`.
1 parent e23ac4a commit 5378799

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

includes/class-api-rewrite.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class API_Rewrite {
4343
* The Constructor.
4444
*
4545
* @param string $redirected_host The host to redirect to.
46-
* @param boolean $disable_ssl Disable SSL.
46+
* @param boolean $disable_ssl Disable SSL.
47+
* @param string $api_key The API key to use with the host.
4748
*/
4849
public function __construct( $redirected_host, $disable_ssl, $api_key ) {
4950
if ( 'debug' === $redirected_host ) {

tests/phpunit/tests/API_Rewrite/APIRewrite_ConstructTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class APIRewrite_ConstructTest extends WP_UnitTestCase {
2020
* @string $method The method to hook.
2121
*/
2222
public function test_should_add_hooks( $hook, $method ) {
23-
$api_rewrite = new AspireUpdate\API_Rewrite( 'debug', false );
23+
$api_rewrite = new AspireUpdate\API_Rewrite( 'debug', false, '' );
2424
$this->assertIsInt( has_action( $hook, [ $api_rewrite, $method ] ) );
2525
}
2626

@@ -50,7 +50,8 @@ public function data_hooks_and_methods() {
5050
public function test_should_set_properties( $property_name, $passed_value, $expected_value ) {
5151
$api_rewrite = new AspireUpdate\API_Rewrite(
5252
'redirected_host' === $property_name ? $passed_value : 'debug',
53-
'disable_ssl' === $property_name ? $passed_value : false
53+
'disable_ssl' === $property_name ? $passed_value : false,
54+
''
5455
);
5556

5657
if ( '%DEFAULT_HOST%' === $expected_value ) {

tests/phpunit/tests/API_Rewrite/APIRewrite_PreHttpRequestTest.php

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function test_should_not_perform_request_when_redirected_host_is_an_empty
1818
$request = new MockAction();
1919
add_filter( 'pre_http_request', [ $request, 'filter' ] );
2020

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

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

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

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

@@ -55,7 +55,7 @@ static function ( $response, $parsed_args ) use ( &$actual ) {
5555
2
5656
);
5757

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

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

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

113-
$this->assertSame( 'my.api.org', $actual );
113+
$this->assertMatchesRegularExpression( '/my\.api\.org\?cache_buster=[0-9]+/', $actual );
114+
}
115+
116+
/**
117+
* Test that the API Key is added to the Authorization header.
118+
*/
119+
public function test_should_add_api_key_to_authorization_header_when_present() {
120+
$actual = [];
121+
122+
add_filter(
123+
'pre_http_request',
124+
static function ( $response, $parsed_args ) use ( &$actual ) {
125+
$actual = $parsed_args;
126+
return $response;
127+
},
128+
10,
129+
2
130+
);
131+
132+
$api_key = 'MY_API_KEY';
133+
$api_rewrite = new AspireUpdate\API_Rewrite( 'my.api.org', true, $api_key );
134+
$api_rewrite->pre_http_request( [], [], $this->get_default_host() );
135+
136+
$this->assertIsArray(
137+
$actual,
138+
'Parsed arguments is not an array.'
139+
);
140+
141+
$this->assertArrayHasKey(
142+
'headers',
143+
$actual,
144+
'The "headers" key is not present.'
145+
);
146+
147+
$this->assertIsArray(
148+
$actual['headers'],
149+
'The "headers" value is not an array.'
150+
);
151+
152+
$this->assertArrayHasKey(
153+
'Authorization',
154+
$actual['headers'],
155+
'There is no authorization header.'
156+
);
157+
158+
$this->assertIsString(
159+
$actual['headers']['Authorization'],
160+
'The authorization header is not a string.'
161+
);
162+
163+
$this->assertSame(
164+
"Bearer $api_key",
165+
$actual['headers']['Authorization'],
166+
'The authorization header is wrong.'
167+
);
114168
}
115169

116170
/**

0 commit comments

Comments
 (0)