From ecd9a6a21d756bc9965ffccdfefba31a63bca5ec Mon Sep 17 00:00:00 2001 From: Matthew Radcliffe Date: Sun, 11 Oct 2015 00:41:18 -0400 Subject: [PATCH 1/3] Add test coverage for Guzzle get request to confirm my poor code. --- Tests/XeroClientTest.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Tests/XeroClientTest.php b/Tests/XeroClientTest.php index 2222dae..320a51c 100644 --- a/Tests/XeroClientTest.php +++ b/Tests/XeroClientTest.php @@ -3,6 +3,10 @@ namespace BlackOptic\Bundle\XeroBundle\Tests; use BlackOptic\Bundle\XeroBundle\XeroClient; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Exception\RequestException; class XeroClientTest extends TestBase { @@ -13,7 +17,7 @@ protected function setUp() parent::setUp(); $this->options = array( - 'base_url' => 'https://api.xero.com/api.xro/2.0', + 'base_url' => 'https://api.xero.com/api.xro/2.0/', 'consumer_key' => '', 'consumer_secret' => '', 'private_key' => '', @@ -37,4 +41,25 @@ public function testInstantiationWithKey() // Test set token methods. $client->setToken('a', 'b'); } + + public function testGetRequest() { + $mock = new MockHandler(array( + new Response(200, array('Content-Length' => 0)) + )); + $this->options['handler'] = HandlerStack::create($mock); + $this->options['private_key'] = $this->pemFile; + + $client = new XeroClient($this->options); + + try + { + $client->get('Accounts'); + } + catch (RequestException $e) + { + $this->assertNotEquals('401', $e->getCode()); + } + + $this->assertEquals('/api.xro/2.0/Accounts', $mock->getLastRequest()->getUri()->getPath()); + } } From 02f3ffaca22c8748dc22ac30a695a43a0d4ce316 Mon Sep 17 00:00:00 2001 From: Matthew Radcliffe Date: Sun, 11 Oct 2015 00:52:56 -0400 Subject: [PATCH 2/3] Fix #5 improper use of OAuth1 options. --- XeroClient.php | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/XeroClient.php b/XeroClient.php index 2ad18db..d1037e1 100644 --- a/XeroClient.php +++ b/XeroClient.php @@ -62,28 +62,22 @@ public function __construct($config = array()) throw new FileNotFoundException('Unable able to find file: ' . $config['private_key']); } - $privateKey = file_get_contents($config['private_key']); - - $config['signature_method'] = 'RSA-SHA1'; - $config['signature_callback'] = function ($baseString) use ($privateKey) { - $signature = ''; - $privateKeyId = openssl_pkey_get_private($privateKey); - openssl_sign($baseString, $signature, $privateKeyId); - openssl_free_key($privateKeyId); - return $signature; - }; - - if ($config['signature_callback'] === '') { - throw new \Exception('Could not create signature from key'); + // Do not obliterate a stack that may be passed into the client. + if (isset($config['handler']) && is_a($config['handler'], '\GuzzleHttp\HandlerStack')) { + $stack = $config['handler']; + } else { + $stack = HandlerStack::create(); } - $stack = HandlerStack::create(); // Create an oauth middleware and push it onto the handler stack. $middleware = new Oauth1([ 'consumer_key' => $config['consumer_key'], 'consumer_secret' => $config['consumer_secret'], 'token' => $config['token'], 'token_secret' => $config['token_secret'], + 'private_key_file' => $config['private_key'], + 'private_key_passphrase' => NULL, + 'signature_method' => Oauth1::SIGNATURE_METHOD_RSA, ]); $stack->push($middleware); From 4049086fc43fe0ce3b65e48e30fdeda0f3b020d2 Mon Sep 17 00:00:00 2001 From: Matthew Radcliffe Date: Sun, 11 Oct 2015 00:58:45 -0400 Subject: [PATCH 3/3] Add back trailing slash to base url. --- DependencyInjection/Configuration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index d31da52..6842b61 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -25,7 +25,7 @@ public function getConfigTreeBuilder() ->defaultValue(self::APPLICATION_TYPE_PRIVATE) ->end() ->scalarNode('base_url') - ->defaultValue('https://api.xero.com/api.xro/2.0') + ->defaultValue('https://api.xero.com/api.xro/2.0/') ->end() ->scalarNode('consumer_key') ->isRequired()