Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #53

Open
wants to merge 2 commits into
base: 1.x-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Tests/GithubObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
18 changes: 6 additions & 12 deletions src/AbstractGithubObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public function __construct(Registry $options = null, BaseHttp $client = null)
$this->options['timeout'] = 120;
}

if ($this->options->get('gh.token', false))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous path only set the Authorization if it was not set... now its being set irrespective of that.

{
$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);
}
Expand All @@ -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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this change not override all other headers that where set by the user.

{
// 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))
Expand Down