From 7f85683cf76cbe775bb2ea612b5c45e8a281efe5 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 17 Aug 2016 16:15:23 +0200 Subject: [PATCH 1/3] Decouple from Guzzle --- README.md | 5 +- composer.json | 7 ++- src/Client/Server/Server.php | 69 ++++++++++------------ src/Client/Signature/HmacSha1Signature.php | 21 +++---- tests/ServerTest.php | 57 ++++++++++-------- tests/TrelloServerTest.php | 39 ++++++------ tests/XingServerTest.php | 40 +++++++------ 7 files changed, 126 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 550718d..2678ed0 100644 --- a/README.md +++ b/README.md @@ -87,9 +87,12 @@ so please help them out with a pull request if you notice this. Via Composer ```shell -$ composer require league/oauth1-client +$ composer require league/oauth1-client php-http/message php-http/guzzle6-adapter ``` +We are not coupled to any library sending HTTP messages. We use an abstraction called +HTTPlug that allows you to choose the client you want. Read more about it +[in the docs](http://docs.php-http.org/en/latest/httplug/users.html). ## Usage diff --git a/composer.json b/composer.json index c4e1fae..86a9983 100644 --- a/composer.json +++ b/composer.json @@ -4,9 +4,14 @@ "license": "MIT", "require": { "php": ">=5.5.0", - "guzzlehttp/guzzle": "^6.0" + "php-http/httplug": "^1.0", + "php-http/discovery": "^1.0", + "psr/http-message-implementation": "^1.0", + "php-http/client-implementation": "^1.0" }, "require-dev": { + "php-http/message": "^1.0", + "php-http/guzzle6-adapter": "^1.0", "phpunit/phpunit": "^4.0", "mockery/mockery": "^0.9", "squizlabs/php_codesniffer": "^2.0" diff --git a/src/Client/Server/Server.php b/src/Client/Server/Server.php index 417688c..e92c13d 100644 --- a/src/Client/Server/Server.php +++ b/src/Client/Server/Server.php @@ -2,8 +2,9 @@ namespace League\OAuth1\Client\Server; -use GuzzleHttp\Client as GuzzleHttpClient; -use GuzzleHttp\Exception\BadResponseException; +use Http\Client\HttpClient; +use Http\Discovery\HttpClientDiscovery; +use Http\Discovery\MessageFactoryDiscovery; use League\OAuth1\Client\Credentials\ClientCredentialsInterface; use League\OAuth1\Client\Credentials\ClientCredentials; use League\OAuth1\Client\Credentials\CredentialsInterface; @@ -12,6 +13,7 @@ use League\OAuth1\Client\Credentials\TokenCredentials; use League\OAuth1\Client\Signature\HmacSha1Signature; use League\OAuth1\Client\Signature\SignatureInterface; +use Psr\Http\Message\ResponseInterface; abstract class Server { @@ -79,18 +81,16 @@ public function getTemporaryCredentials() { $uri = $this->urlTemporaryCredentials(); - $client = $this->createHttpClient(); - $header = $this->temporaryCredentialsProtocolHeader($uri); $authorizationHeader = array('Authorization' => $header); $headers = $this->buildHttpClientHeaders($authorizationHeader); - try { - $response = $client->post($uri, [ - 'headers' => $headers, - ]); - } catch (BadResponseException $e) { - return $this->handleTemporaryCredentialsBadResponse($e); + $request = MessageFactoryDiscovery::find()->createRequest('POST', $uri, $headers); + $response = $this->createHttpClient()->sendRequest($request); + $statusCode = $response->getStatusCode(); + + if ($statusCode >= 400 && $statusCode < 600) { + $this->handleTemporaryCredentialsBadResponse($response); } return $this->createTemporaryCredentials((string) $response->getBody()); @@ -157,17 +157,15 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $uri = $this->urlTokenCredentials(); $bodyParameters = array('oauth_verifier' => $verifier); - $client = $this->createHttpClient(); - $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters); + $headers['Content-Type'] = 'application/x-www-form-urlencoded'; - try { - $response = $client->post($uri, [ - 'headers' => $headers, - 'form_params' => $bodyParameters, - ]); - } catch (BadResponseException $e) { - return $this->handleTokenCredentialsBadResponse($e); + $request = MessageFactoryDiscovery::find()->createRequest('POST', $uri, $headers, http_build_query($bodyParameters)); + $response = $this->createHttpClient()->sendRequest($request); + $statusCode = $response->getStatusCode(); + + if ($statusCode >= 400 && $statusCode < 600) { + $this->handleTokenCredentialsBadResponse($response); } return $this->createTokenCredentials((string) $response->getBody()); @@ -246,16 +244,12 @@ protected function fetchUserDetails(TokenCredentials $tokenCredentials, $force = if (!$this->cachedUserDetailsResponse || $force) { $url = $this->urlUserDetails(); - $client = $this->createHttpClient(); - $headers = $this->getHeaders($tokenCredentials, 'GET', $url); + $request = MessageFactoryDiscovery::find()->createRequest('GET', $url, $headers); + $response = $this->createHttpClient()->sendRequest($request); + $statusCode = $response->getStatusCode(); - try { - $response = $client->get($url, [ - 'headers' => $headers, - ]); - } catch (BadResponseException $e) { - $response = $e->getResponse(); + if ($statusCode >= 400 && $statusCode < 600) { $body = $response->getBody(); $statusCode = $response->getStatusCode(); @@ -263,6 +257,7 @@ protected function fetchUserDetails(TokenCredentials $tokenCredentials, $force = "Received error [$body] with status code [$statusCode] when retrieving token credentials." ); } + switch ($this->responseType) { case 'json': $this->cachedUserDetailsResponse = json_decode((string) $response->getBody(), true); @@ -305,13 +300,13 @@ public function getSignature() } /** - * Creates a Guzzle HTTP client for the given URL. + * Creates a HTTP client * - * @return GuzzleHttpClient + * @return HttpClient */ public function createHttpClient() { - return new GuzzleHttpClient(); + return HttpClientDiscovery::find(); } /** @@ -348,7 +343,7 @@ public function getHeaders(CredentialsInterface $credentials, $method, $url, arr } /** - * Get Guzzle HTTP client default headers. + * Get HTTP client default headers. * * @return array */ @@ -363,7 +358,7 @@ protected function getHttpClientDefaultHeaders() } /** - * Build Guzzle HTTP client headers. + * Build HTTP headers. * * @return array */ @@ -405,13 +400,12 @@ protected function createClientCredentials(array $clientCredentials) /** * Handle a bad response coming back when getting temporary credentials. * - * @param BadResponseException $e + * @param ResponseInterface $response * * @throws CredentialsException */ - protected function handleTemporaryCredentialsBadResponse(BadResponseException $e) + protected function handleTemporaryCredentialsBadResponse(ResponseInterface $response) { - $response = $e->getResponse(); $body = $response->getBody(); $statusCode = $response->getStatusCode(); @@ -449,13 +443,12 @@ protected function createTemporaryCredentials($body) /** * Handle a bad response coming back when getting token credentials. * - * @param BadResponseException $e + * @param ResponseInterface $response * * @throws CredentialsException */ - protected function handleTokenCredentialsBadResponse(BadResponseException $e) + protected function handleTokenCredentialsBadResponse(ResponseInterface $response) { - $response = $e->getResponse(); $body = $response->getBody(); $statusCode = $response->getStatusCode(); diff --git a/src/Client/Signature/HmacSha1Signature.php b/src/Client/Signature/HmacSha1Signature.php index 70fdfc6..fbdb205 100644 --- a/src/Client/Signature/HmacSha1Signature.php +++ b/src/Client/Signature/HmacSha1Signature.php @@ -2,8 +2,8 @@ namespace League\OAuth1\Client\Signature; -use GuzzleHttp\Psr7; -use GuzzleHttp\Psr7\Uri; +use Http\Discovery\UriFactoryDiscovery; +use Psr\Http\Message\UriInterface; class HmacSha1Signature extends Signature implements SignatureInterface { @@ -28,36 +28,31 @@ public function sign($uri, array $parameters = array(), $method = 'POST') } /** - * Create a Guzzle url for the given URI. + * Create a PSR URI for the given string URI. * * @param string $uri * - * @return Url + * @return UriInterface */ protected function createUrl($uri) { - return Psr7\uri_for($uri); + return UriFactoryDiscovery::find()->createUri($uri); } /** * Generate a base string for a HMAC-SHA1 signature * based on the given a url, method, and any parameters. * - * @param Url $url + * @param UriInterface $url * @param string $method * @param array $parameters * * @return string */ - protected function baseString(Uri $url, $method = 'POST', array $parameters = array()) + protected function baseString(UriInterface $url, $method = 'POST', array $parameters = array()) { $baseString = rawurlencode($method).'&'; - - $schemeHostPath = Uri::fromParts(array( - 'scheme' => $url->getScheme(), - 'host' => $url->getHost(), - 'path' => $url->getPath(), - )); + $schemeHostPath = $this->createUrl(sprintf('%s://%s%s', $url->getScheme(), $url->getHost(), $url->getPath())); $baseString .= rawurlencode($schemeHostPath).'&'; diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 75b93d1..42f7dc9 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -18,9 +18,11 @@ * @link http://cartalyst.com */ +use GuzzleHttp\Psr7\Response; use League\OAuth1\Client\Credentials\ClientCredentials; use Mockery as m; use PHPUnit_Framework_TestCase; +use Psr\Http\Message\RequestInterface; class ServerTest extends PHPUnit_Framework_TestCase { @@ -82,8 +84,11 @@ public function testGettingTemporaryCredentials() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('http://www.example.com/temporary', m::on(function($options) use ($me) { - $headers = $options['headers']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $uri = $request->getUri()->__toString(); + $me->assertSame($uri, 'http://www.example.com/temporary'); + $me->assertTrue(isset($headers['Authorization'])); @@ -92,12 +97,11 @@ public function testGettingTemporaryCredentials() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true')); $credentials = $server->getTemporaryCredentials(); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials); @@ -142,9 +146,10 @@ public function testGettingTokenCredentials() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($options) use ($me) { - $headers = $options['headers']; - $body = $options['form_params']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $uri = $request->getUri()->__toString(); + $body = $request->getBody()->__toString(); $me->assertTrue(isset($headers['Authorization'])); $me->assertFalse(isset($headers['User-Agent'])); @@ -154,14 +159,14 @@ public function testGettingTokenCredentials() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); - $me->assertSame($body, array('oauth_verifier' => 'myverifiercode')); + $me->assertSame($body, http_build_query(array('oauth_verifier' => 'myverifiercode'))); + $me->assertSame($uri, 'http://www.example.com/token'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret')); $credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode'); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials); @@ -181,27 +186,28 @@ public function testGettingTokenCredentialsWithUserAgent() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($options) use ($me, $userAgent) { - $headers = $options['headers']; - $body = $options['form_params']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me, $userAgent) { + $headers = $request->getHeaders(); + $uri = $request->getUri()->__toString(); + $body = $request->getBody()->__toString(); $me->assertTrue(isset($headers['Authorization'])); $me->assertTrue(isset($headers['User-Agent'])); - $me->assertEquals($userAgent, $headers['User-Agent']); + $me->assertEquals($userAgent, $headers['User-Agent'][0]); // OAuth protocol specifies a strict number of // headers should be sent, in the correct order. // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); - $me->assertSame($body, array('oauth_verifier' => 'myverifiercode')); + $me->assertSame($body, http_build_query(array('oauth_verifier' => 'myverifiercode'))); + $me->assertSame($uri, 'http://www.example.com/token'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret')); $credentials = $server->setUserAgent($userAgent)->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode'); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials); @@ -221,8 +227,10 @@ public function testGettingUserDetails() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('get')->with('http://www.example.com/user', m::on(function($options) use ($me) { - $headers = $options['headers']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $me->assertSame('http://www.example.com/user', $url); $me->assertTrue(isset($headers['Authorization'])); @@ -231,12 +239,11 @@ public function testGettingUserDetails() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->once()->andReturn(json_encode(array('foo' => 'bar', 'id' => 123, 'contact_email' => 'baz@qux.com', 'username' => 'fred'))); + }))->once()->andReturn(new Response(200, [], json_encode(array('foo' => 'bar', 'id' => 123, 'contact_email' => 'baz@qux.com', 'username' => 'fred')))); $user = $server->getUserDetails($temporaryCredentials); $this->assertInstanceOf('League\OAuth1\Client\Server\User', $user); diff --git a/tests/TrelloServerTest.php b/tests/TrelloServerTest.php index 1e3105a..bb1c511 100644 --- a/tests/TrelloServerTest.php +++ b/tests/TrelloServerTest.php @@ -1,9 +1,11 @@ shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetRequestToken', m::on(function($options) use ($me) { - $headers = $options['headers']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $me->assertSame('https://trello.com/1/OAuthGetRequestToken', $url); $me->assertTrue(isset($headers['Authorization'])); @@ -57,12 +61,11 @@ public function testGettingTemporaryCredentials() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true')); $credentials = $server->getTemporaryCredentials(); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials); @@ -200,9 +203,11 @@ public function testGettingTokenCredentials() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('https://trello.com/1/OAuthGetAccessToken', m::on(function($options) use ($me) { - $headers = $options['headers']; - $body = $options['form_params']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $body = $request->getBody()->__toString(); + $me->assertSame('https://trello.com/1/OAuthGetAccessToken', $url); $me->assertTrue(isset($headers['Authorization'])); @@ -211,14 +216,13 @@ public function testGettingTokenCredentials() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); - $me->assertSame($body, array('oauth_verifier' => 'myverifiercode')); + $me->assertSame($body, http_build_query(array('oauth_verifier' => 'myverifiercode'))); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret')); $credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode'); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials); @@ -237,8 +241,10 @@ public function testGettingUserDetails() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('get')->with('https://trello.com/1/members/me?key='.$this->getApplicationKey().'&token='.$this->getAccessToken(), m::on(function($options) use ($me) { - $headers = $options['headers']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $me->assertSame('https://trello.com/1/members/me?key='.$me->getApplicationKey().'&token='.$me->getAccessToken(), $url); $me->assertTrue(isset($headers['Authorization'])); @@ -247,12 +253,11 @@ public function testGettingUserDetails() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->once()->andReturn($this->getUserPayload()); + }))->once()->andReturn(new Response(200, [], $this->getUserPayload())); $user = $server ->setAccessToken($this->getAccessToken()) diff --git a/tests/XingServerTest.php b/tests/XingServerTest.php index d3dd0f0..bc5a471 100644 --- a/tests/XingServerTest.php +++ b/tests/XingServerTest.php @@ -1,9 +1,11 @@ shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('https://api.xing.com/v1/request_token', m::on(function ($options) use ($me) { - $headers = $options['headers']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $me->assertSame( 'https://api.xing.com/v1/request_token', $url); + $me->assertTrue(isset($headers['Authorization'])); // OAuth protocol specifies a strict number of @@ -56,12 +61,11 @@ public function testGettingTemporaryCredentials() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true')); $credentials = $server->getTemporaryCredentials(); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials); @@ -106,9 +110,11 @@ public function testGettingTokenCredentials() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('post')->with('https://api.xing.com/v1/access_token', m::on(function ($options) use ($me) { - $headers = $options['headers']; - $body = $options['form_params']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $body = $request->getBody()->__toString(); + $me->assertSame('https://api.xing.com/v1/access_token', $url); $me->assertTrue(isset($headers['Authorization'])); @@ -117,14 +123,13 @@ public function testGettingTokenCredentials() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); - $me->assertSame($body, array('oauth_verifier' => 'myverifiercode')); + $me->assertSame($body, http_build_query(array('oauth_verifier' => 'myverifiercode'))); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret'); + }))->once()->andReturn(new Response(200, [], 'oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret')); $credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode'); $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials); @@ -143,8 +148,10 @@ public function testGettingUserDetails() $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass')); $me = $this; - $client->shouldReceive('get')->with('https://api.xing.com/v1/users/me', m::on(function ($options) use ($me) { - $headers = $options['headers']; + $client->shouldReceive('sendRequest')->with(m::on(function(RequestInterface $request) use ($me) { + $headers = $request->getHeaders(); + $url = $request->getUri()->__toString(); + $me->assertSame('https://api.xing.com/v1/users/me', $url); $me->assertTrue(isset($headers['Authorization'])); @@ -153,12 +160,11 @@ public function testGettingUserDetails() // We'll validate that here. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/'; - $matches = preg_match($pattern, $headers['Authorization']); + $matches = preg_match($pattern, $headers['Authorization'][0]); $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.'); return true; - }))->once()->andReturn($response = m::mock('stdClass')); - $response->shouldReceive('getBody')->once()->andReturn($this->getUserPayload()); + }))->once()->andReturn(new Response(200, [], $this->getUserPayload())); $user = $server->getUserDetails($temporaryCredentials); $this->assertInstanceOf('League\OAuth1\Client\Server\User', $user); From 3aae877b003024482fde878e309b8fc5e303febe Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 17 Aug 2016 16:27:18 +0200 Subject: [PATCH 2/3] Code style fixes --- src/Client/Server/Server.php | 3 ++- src/Client/Signature/HmacSha1Signature.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Client/Server/Server.php b/src/Client/Server/Server.php index e92c13d..a7fd294 100644 --- a/src/Client/Server/Server.php +++ b/src/Client/Server/Server.php @@ -160,7 +160,8 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $headers = $this->getHeaders($temporaryCredentials, 'POST', $uri, $bodyParameters); $headers['Content-Type'] = 'application/x-www-form-urlencoded'; - $request = MessageFactoryDiscovery::find()->createRequest('POST', $uri, $headers, http_build_query($bodyParameters)); + $request = MessageFactoryDiscovery::find() + ->createRequest('POST', $uri, $headers, http_build_query($bodyParameters)); $response = $this->createHttpClient()->sendRequest($request); $statusCode = $response->getStatusCode(); diff --git a/src/Client/Signature/HmacSha1Signature.php b/src/Client/Signature/HmacSha1Signature.php index fbdb205..49ec290 100644 --- a/src/Client/Signature/HmacSha1Signature.php +++ b/src/Client/Signature/HmacSha1Signature.php @@ -52,7 +52,7 @@ protected function createUrl($uri) protected function baseString(UriInterface $url, $method = 'POST', array $parameters = array()) { $baseString = rawurlencode($method).'&'; - $schemeHostPath = $this->createUrl(sprintf('%s://%s%s', $url->getScheme(), $url->getHost(), $url->getPath())); + $schemeHostPath = $this->createUrl(sprintf('%s://%s%s', $url->getScheme(), $url->getHost(), $url->getPath())); $baseString .= rawurlencode($schemeHostPath).'&'; From 94470ebbab8fa14a990fcef1e3e2aa0c546b9fbf Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 17 Aug 2016 16:31:02 +0200 Subject: [PATCH 3/3] Update old travis config --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0644d45..f20c368 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: php php: - - 5.3 - - 5.4 - 5.5 - 5.6 - 7.0