Skip to content

Commit efed7df

Browse files
authored
Merge pull request #49 from nibra/auth
Provide access_token in header instead of URI. Fixes #48
2 parents 9f8d936 + b996b80 commit efed7df

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
composer.phar
33
composer.lock
44
phpunit.xml
5+
build/

Tests/GithubObjectTest.php

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
namespace Joomla\Github\Tests;
88

9+
use Joomla\Github\AbstractGithubObject;
910
use Joomla\Github\Tests\Stub\GitHubTestCase;
1011
use Joomla\Github\Tests\Stub\ObjectMock;
12+
use Joomla\Http\Http;
13+
use Joomla\Http\Transport\Curl;
1114

1215
/**
1316
* Test class for Joomla\Github\Object.
@@ -17,11 +20,17 @@
1720
class GithubObjectTest extends GitHubTestCase
1821
{
1922
/**
20-
* @var ObjectMock Object under test.
23+
* @var AbstractGithubObject Object under test.
2124
* @since 1.0
2225
*/
2326
protected $object;
2427

28+
/**
29+
* @var Http The HTTP client
30+
* @since __DEPLOY_VERSION__
31+
*/
32+
protected $client;
33+
2534
/**
2635
* Sets up the fixture, for example, opens a network connection.
2736
* This method is called before a test is executed.
@@ -34,6 +43,8 @@ protected function setUp()
3443
{
3544
parent::setUp();
3645

46+
$transport = new Curl(array());
47+
$this->client = new Http(array(), $transport);
3748
$this->object = new ObjectMock($this->options, $this->client);
3849
}
3950

@@ -47,10 +58,34 @@ protected function setUp()
4758
public function fetchUrlData()
4859
{
4960
return array(
50-
'Standard github - no pagination data' => array('https://api.github.com', '/gists', 0, 0, 'https://api.github.com/gists'),
51-
'Enterprise github - no pagination data' => array('https://mygithub.com', '/gists', 0, 0, 'https://mygithub.com/gists'),
52-
'Standard github - page 3' => array('https://api.github.com', '/gists', 3, 0, 'https://api.github.com/gists?page=3'),
53-
'Enterprise github - page 3, 50 per page' => array('https://mygithub.com', '/gists', 3, 50, 'https://mygithub.com/gists?page=3&per_page=50'),
61+
'Standard github - no pagination data' => array(
62+
'https://api.github.com',
63+
'/gists',
64+
0,
65+
0,
66+
'https://api.github.com/gists'
67+
),
68+
'Enterprise github - no pagination data' => array(
69+
'https://mygithub.com',
70+
'/gists',
71+
0,
72+
0,
73+
'https://mygithub.com/gists'
74+
),
75+
'Standard github - page 3' => array(
76+
'https://api.github.com',
77+
'/gists',
78+
3,
79+
0,
80+
'https://api.github.com/gists?page=3'
81+
),
82+
'Enterprise github - page 3, 50 per page' => array(
83+
'https://mygithub.com',
84+
'/gists',
85+
3,
86+
50,
87+
'https://mygithub.com/gists?page=3&per_page=50'
88+
),
5489
);
5590
}
5691

@@ -72,9 +107,10 @@ public function testFetchUrl($apiUrl, $path, $page, $limit, $expected)
72107
{
73108
$this->options->set('api.url', $apiUrl);
74109

75-
$this->assertThat(
110+
self::assertEquals(
111+
$expected,
76112
$this->object->fetchUrl($path, $page, $limit),
77-
$this->equalTo($expected)
113+
'URL is not as expected.'
78114
);
79115
}
80116

@@ -92,9 +128,10 @@ public function testFetchUrlBasicAuth()
92128
$this->options->set('api.username', 'MyTestUser');
93129
$this->options->set('api.password', 'MyTestPass');
94130

95-
$this->assertThat(
131+
self::assertEquals(
132+
'https://MyTestUser:MyTestPass@api.github.com/gists',
96133
$this->object->fetchUrl('/gists', 0, 0),
97-
$this->equalTo('https://MyTestUser:MyTestPass@api.github.com/gists')
134+
'URL is not as expected.'
98135
);
99136
}
100137

@@ -109,9 +146,16 @@ public function testFetchUrlToken()
109146

110147
$this->options->set('gh.token', 'MyTestToken');
111148

112-
$this->assertThat(
149+
self::assertEquals(
150+
'https://api.github.com/gists',
113151
$this->object->fetchUrl('/gists', 0, 0),
114-
$this->equalTo('https://api.github.com/gists?access_token=MyTestToken')
152+
'URL is not as expected.'
153+
);
154+
155+
self::assertEquals(
156+
array('Authorization' => 'token MyTestToken'),
157+
$this->client->getOption('headers'),
158+
'Token should be propagated as a header.'
115159
);
116160
}
117161
}

Tests/bootstrap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
require_once 'vendor/autoload.php';
3+
4+
// Turn off E_DEPRECATED caused by outdated PHPUnit
5+
$errorReporting = error_reporting();
6+
error_reporting($errorReporting & ~E_DEPRECATED);

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php" colors="false">
2+
<phpunit bootstrap="Tests/bootstrap.php" colors="false">
33
<testsuites>
44
<testsuite name="Unit">
55
<directory>Tests</directory>

src/AbstractGithubObject.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class AbstractGithubObject
3232
/**
3333
* The HTTP client object to use in sending HTTP requests.
3434
*
35-
* @var Http
35+
* @var BaseHttp
3636
* @since 1.0
3737
*/
3838
protected $client;
@@ -89,7 +89,19 @@ abstract class AbstractGithubObject
8989
public function __construct(Registry $options = null, BaseHttp $client = null)
9090
{
9191
$this->options = $options ?: new Registry;
92-
$this->client = $client ?: new Http($this->options);
92+
$this->client = $client ?: new BaseHttp($this->options);
93+
94+
// Make sure the user agent string is defined.
95+
if (!isset($this->options['userAgent']))
96+
{
97+
$this->options['userAgent'] = 'JGitHub/2.0';
98+
}
99+
100+
// Set the default timeout to 120 seconds.
101+
if (!isset($this->options['timeout']))
102+
{
103+
$this->options['timeout'] = 120;
104+
}
93105

94106
$this->package = \get_class($this);
95107
$this->package = substr($this->package, strrpos($this->package, '\\') + 1);
@@ -116,8 +128,14 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
116128

117129
if ($this->options->get('gh.token', false))
118130
{
119-
// Use oAuth authentication - @todo set in request header ?
120-
$uri->setVar('access_token', $this->options->get('gh.token'));
131+
// Use oAuth authentication
132+
$headers = $this->client->getOption('headers', array());
133+
134+
if (!isset($headers['Authorization']))
135+
{
136+
$headers['Authorization'] = 'token ' . $this->options->get('gh.token');
137+
$this->client->setOption('headers', $headers);
138+
}
121139
}
122140
else
123141
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy