diff --git a/Tests/GithubObjectTest.php b/Tests/GithubObjectTest.php index 4d75ff4b..3106dcaf 100755 --- a/Tests/GithubObjectTest.php +++ b/Tests/GithubObjectTest.php @@ -143,15 +143,24 @@ public function testFetchUrlBasicAuth() public function testFetchUrlToken() { $this->options->set('api.url', 'https://api.github.com'); - $this->options->set('gh.token', 'MyTestToken'); + $this->object = new ObjectMock($this->options, $this->client); + + $clientBeforeFetchUrl = clone $this->client; + self::assertEquals( 'https://api.github.com/gists', (string) $this->object->fetchUrl('/gists', 0, 0), 'URL is not as expected.' ); + self::assertEquals( + $clientBeforeFetchUrl, + $this->client, + 'HTTP client should not have changed' + ); + self::assertEquals( array('Authorization' => 'token MyTestToken'), $this->client->getOption('headers'), diff --git a/src/AbstractGithubObject.php b/src/AbstractGithubObject.php index 0b22dd33..dee5fee7 100644 --- a/src/AbstractGithubObject.php +++ b/src/AbstractGithubObject.php @@ -103,6 +103,11 @@ public function __construct(Registry $options = null, BaseHttp $client = null) $this->options['timeout'] = 120; } + if ($this->options->get('gh.token', false)) + { + $this->client->setOption('headers', array('Authorization' => 'token ' . $this->options->get('gh.token'))); + } + $this->package = \get_class($this); $this->package = substr($this->package, strrpos($this->package, '\\') + 1); } @@ -126,18 +131,7 @@ protected function fetchUrl($path, $page = 0, $limit = 0) // Get a new Uri object focusing the api url and given path. $uri = new Uri($this->options->get('api.url') . $path); - if ($this->options->get('gh.token', false)) - { - // Use oAuth authentication - $headers = $this->client->getOption('headers', array()); - - if (!isset($headers['Authorization'])) - { - $headers['Authorization'] = 'token ' . $this->options->get('gh.token'); - $this->client->setOption('headers', $headers); - } - } - else + if (!$this->options->get('gh.token', false)) { // Use basic authentication if ($this->options->get('api.username', false))
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: