Permalink
Browse files

adding support for setting user agent

  • Loading branch information...
1 parent b845876 commit 19d5934207ce1a427e0e7236ffbe7e5051d8df10 Steven Maguire committed Jan 16, 2015
Showing with 112 additions and 11 deletions.
  1. +2 −1 .gitignore
  2. +2 −1 composer.json
  3. +10 −0 phpunit.xml
  4. +58 −9 src/Client/Server/Server.php
  5. +40 −0 tests/ServerTest.php
View
@@ -1,3 +1,4 @@
+/build
/vendor
/composer.lock
-.DS_Store
+.DS_Store
View
@@ -8,7 +8,8 @@
},
"require-dev": {
"phpunit/phpunit": "*",
- "mockery/mockery": "0.7.2"
+ "mockery/mockery": "0.7.2",
+ "satooshi/php-coveralls": "dev-master"
},
"keywords": [
"oauth",
View
@@ -5,6 +5,16 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
+ <logging>
+ <log type="coverage-html"
+ target="./build/coverage/html"
+ charset="UTF-8"
+ highlight="false"
+ lowUpperBound="35"
+ highLowerBound="70"/>
+ <log type="coverage-clover"
+ target="./build/coverage/log/coverage.xml"/>
+ </logging>
<testsuites>
<testsuite name="common">
<directory suffix="Test.php">tests</directory>
@@ -44,6 +44,13 @@
protected $cachedUserDetailsResponse;
/**
+ * Optional user agent
+ *
+ * @var string
+ */
+ protected $userAgent;
+
+ /**
* Create a new server instance.
*
* @param ClientCredentialsInterface|array $clientCredentials
@@ -74,10 +81,12 @@ public function getTemporaryCredentials()
$client = $this->createHttpClient();
+ $header = $this->temporaryCredentialsProtocolHeader($uri);
+ $authorization_header = array('Authorization' => $header);
+ $headers = $this->buildHttpClientHeaders($authorization_header);
+
try {
- $response = $client->post($uri, array(
- 'Authorization' => $this->temporaryCredentialsProtocolHeader($uri),
- ))->send();
+ $response = $client->post($uri, $headers)->send();
} catch (BadResponseException $e) {
return $this->handleTemporaryCredentialsBadResponse($e);
}
@@ -147,11 +156,11 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials,
$client = $this->createHttpClient();
$header = $this->protocolHeader('POST', $uri, $temporaryCredentials, $bodyParameters);
+ $authorization_header = array('Authorization' => $header);
+ $headers = $this->buildHttpClientHeaders($authorization_header);
try {
- $response = $client->post($uri, array(
- 'Authorization' => $header,
- ), $bodyParameters)->send();
+ $response = $client->post($uri, $headers, $bodyParameters)->send();
} catch (BadResponseException $e) {
return $this->handleTokenCredentialsBadResponse($e);
}
@@ -222,10 +231,12 @@ protected function fetchUserDetails(TokenCredentials $tokenCredentials, $force =
$client = $this->createHttpClient();
+ $header = $this->protocolHeader('GET', $url, $tokenCredentials);
+ $authorization_header = array('Authorization' => $header);
+ $headers = $this->buildHttpClientHeaders($authorization_header);
+
try {
- $response = $client->get($url, array(
- 'Authorization' => $this->protocolHeader('GET', $url, $tokenCredentials),
- ))->send();
+ $response = $client->get($url, $headers)->send();
} catch (BadResponseException $e) {
$response = $e->getResponse();
$body = $response->getBody();
@@ -288,6 +299,44 @@ public function createHttpClient()
}
/**
+ * Set the user agent value.
+ *
+ * @param string $userAgent
+ *
+ * @return Server
+ */
+ public function setUserAgent($userAgent = null)
+ {
+ $this->userAgent = $userAgent;
+ return $this;
+ }
+
+ /**
+ * Get Guzzle HTTP client default headers.
+ *
+ * @return array
+ */
+ protected function getHttpClientDefaultHeaders()
+ {
+ $default_headers = array();
+ if (!empty($this->userAgent)) {
+ $default_headers['User-Agent'] = $this->userAgent;
+ }
+ return $default_headers;
+ }
+
+ /**
+ * Build Guzzle HTTP client headers.
+ *
+ * @return array
+ */
+ protected function buildHttpClientHeaders($headers = array())
+ {
+ $default_headers = $this->getHttpClientDefaultHeaders();
+ return array_merge($headers, $default_headers);
+ }
+
+ /**
* Creates a client credentials instance from an array of credentials.
*
* @param array $clientCredentials
View
@@ -138,6 +138,7 @@ public function testGettingTokenCredentials()
$me = $this;
$client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($headers) use ($me) {
$me->assertTrue(isset($headers['Authorization']));
+ $me->assertFalse(isset($headers['User-Agent']));
// OAuth protocol specifies a strict number of
// headers should be sent, in the correct order.
@@ -159,6 +160,45 @@ public function testGettingTokenCredentials()
$this->assertEquals('tokencredentialssecret', $credentials->getSecret());
}
+ public function testGettingTokenCredentialsWithUserAgent()
+ {
+ $userAgent = 'FooBar';
+ $server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]');
+ $server->__construct($this->getMockClientCredentials());
+
+ $temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
+ $temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
+ $temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
+
+ $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
+
+ $me = $this;
+ $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($headers) use ($me, $userAgent) {
+ $me->assertTrue(isset($headers['Authorization']));
+ $me->assertTrue(isset($headers['User-Agent']));
+ $me->assertEquals($userAgent, $headers['User-Agent']);
+
+ // 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']);
+ $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
+
+ return true;
+ }), array('oauth_verifier' => 'myverifiercode'))->once()->andReturn($request = m::mock('stdClass'));
+
+ $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
+ $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
+
+ $credentials = $server->setUserAgent($userAgent)->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
+ $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
+ $this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
+ $this->assertEquals('tokencredentialssecret', $credentials->getSecret());
+
+ }
+
public function testGettingUserDetails()
{
$server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient,protocolHeader]');

0 comments on commit 19d5934

Please sign in to comment.