Skip to content

Commit

Permalink
Merge pull request #2008 from woocommerce/add/plugin-version-header
Browse files Browse the repository at this point in the history
Add client name and version as request headers
  • Loading branch information
martynmjones authored Jul 6, 2023
2 parents 67f8616 + b2303ba commit 566e407
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Internal/DependencyManagement/GoogleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ protected function register_guzzle() {
$handler_stack->remove( 'http_errors' );
$handler_stack->push( $this->error_handler(), 'http_errors' );
$handler_stack->push( $this->add_auth_header() );
$handler_stack->push( $this->add_plugin_version_header(), 'plugin_version_header' );

// Override endpoint URL if we are using http locally.
if ( 0 === strpos( $this->get_connect_server_url_root()->getValue(), 'http://' ) ) {
Expand Down Expand Up @@ -254,6 +255,23 @@ protected function add_auth_header(): callable {
};
}

/**
* Add client name and version headers to request
*
* @since x.x.x
*
* @return callable
*/
public function add_plugin_version_header(): callable {
return function( callable $handler ) {
return function( RequestInterface $request, array $options ) use ( $handler ) {
$request = $request->withHeader( 'x-client-name', $this->get_client_name() )
->withHeader( 'x-client-version', $this->get_version() );
return $handler( $request, $options );
};
};
}

/**
* @return callable
*/
Expand Down
9 changes: 9 additions & 0 deletions src/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ protected function get_main_filename(): string {
return 'google-listings-and-ads.php';
}

/**
* Get the client name for this plugin.
*
* @return string
*/
protected function get_client_name(): string {
return 'google-listings-and-ads';
}

/**
* Get the plugin slug.
*
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/API/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API;

use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\ContainerAwareUnitTest;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\GuzzleHttp\Client;
use Automattic\WooCommerce\GoogleListingsAndAds\Internal\DependencyManagement\GoogleServiceProvider;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\GuzzleHttp\Psr7\Request;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;

defined( 'ABSPATH' ) || exit;

/**
* Class ClientTest
*
* @since x.x.x
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API
*/
class ClientTest extends ContainerAwareUnitTest {
use PluginHelper;

/**
* Confirm that the client handler stack includes the `plugin_version_header`
*
* @return void
*/
public function test_plugin_version_header_in_handler_stack(): void {
// Get string representation of the handler stack and confirm if `plugin_version_header` is contained within it.
$this->assertStringContainsString( 'plugin_version_header', (string) $this->container->get( Client::class )->getConfig( 'handler' ) );
}

/**
* Confirm that `add_plugin_version_header` adds the correct headers to the request.
*
* @return void
*/
public function test_plugin_version_headers(): void {
$service = new GoogleServiceProvider();
$request = new Request( 'GET', 'https://testing.local' );

$service->add_plugin_version_header()(
function( $request, $options ) {
$this->assertEquals( $this->get_client_name(), $request->getHeader( 'x-client-name' )[0] );
$this->assertEquals( $this->get_version(), $request->getHeader( 'x-client-version' )[0] );
}
)( $request, [] );

}
}

0 comments on commit 566e407

Please sign in to comment.