Skip to content

Commit

Permalink
Add a put method to the WC_Helper_API (woocommerce#26262)
Browse files Browse the repository at this point in the history
* Merge wc api authorization headers with given headers

* Add put method to WC_Helper_API

* Add unit test coverage around WC_Helper_API request methods

* Add tests for WC_Helper_API url method
  • Loading branch information
joshuatf authored May 6, 2020
1 parent 906d4da commit 43ab840
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
16 changes: 15 additions & 1 deletion includes/admin/helper/class-wc-helper-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ private static function _authenticate( &$url, &$args ) {
$args['headers'] = array();
}

$args['headers'] = array(
$headers = array(
'Authorization' => 'Bearer ' . $auth['access_token'],
'X-Woo-Signature' => $signature,
);
$args['headers'] = wp_parse_args( $headers, $args['headers'] );

$url = add_query_arg(
array(
Expand Down Expand Up @@ -139,6 +140,19 @@ public static function post( $endpoint, $args = array() ) {
return self::request( $endpoint, $args );
}

/**
* Wrapper for self::request().
*
* @param string $endpoint The helper API endpoint to request.
* @param array $args Arguments passed to wp_remote_request().
*
* @return array The response object from wp_safe_remote_request().
*/
public static function put( $endpoint, $args = array() ) {
$args['method'] = 'PUT';
return self::request( $endpoint, $args );
}

/**
* Using the API base, form a request URL from a given endpoint.
*
Expand Down
109 changes: 109 additions & 0 deletions tests/unit-tests/helper/class-wc-helper-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Class WC_Helper_API unit tests.
*
* @package WooCommerce\Tests\Importer
*/

/**
* Test class for WC_Helper_API.
*/
class WC_Tests_Helper_API extends WC_Unit_Test_Case {

/**
* Set up mock responses for all API calls.
*/
public function setUp() {
parent::setUp();

// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
$this->http_responder = array( $this, 'mock_http_responses' );
}

/**
* Test that the url method returns the correct WooCommerce.com path.
*
* @return void
*/
public function test_api_url() {
$url = WC_Helper_API::url( '/test-path' );
$this->assertEquals( 'https://woocommerce.com/wp-json/helper/1.0/test-path', $url );
}

/**
* Test a GET request through the WC_Helper_API.
*
* @return void
*/
public function test_get_request() {
$request = WC_Helper_API::get(
'test-get'
);

$this->assertEquals( '200', $request['response']['code'] );
}

/**
* Test a POST request through the WC_Helper_API.
*
* @return void
*/
public function test_post_request() {
$request = WC_Helper_API::post(
'test-post'
);

$this->assertEquals( '200', $request['response']['code'] );
}

/**
* Test a PUT request through the WC_Helper_API.
*
* @return void
*/
public function test_put_request() {
$request = WC_Helper_API::put(
'test-put'
);

$this->assertEquals( '200', $request['response']['code'] );
}

/**
* Provides a mocked response for various paths and request methods.
*
* This function is called by WP_HTTP_TestCase::http_request_listner().
*
* @param array $request Request arguments.
* @param string $url URL of the request.
*
* @return array|false mocked response or false to let WP perform a regular request.
*/
protected function mock_http_responses( $request, $url ) {
$mocked_response = false;

if ( 'GET' === $request['method'] && WC_Helper_API::url( 'test-get' ) === $url ) {
$mocked_response = array(
'body' => 'Mocked response',
'response' => array( 'code' => 200 ),
);
}

if ( 'POST' === $request['method'] && WC_Helper_API::url( 'test-post' ) === $url ) {
$mocked_response = array(
'body' => 'Mocked response',
'response' => array( 'code' => 200 ),
);
}

if ( 'PUT' === $request['method'] && WC_Helper_API::url( 'test-put' ) === $url ) {
$mocked_response = array(
'body' => 'Mocked response',
'response' => array( 'code' => 200 ),
);
}

return $mocked_response;
}

}

0 comments on commit 43ab840

Please sign in to comment.