Skip to content

Commit

Permalink
Merge pull request #56 from Nyholm/urlIssue
Browse files Browse the repository at this point in the history
Make sure we can have query string in the endpoint.
  • Loading branch information
Nyholm committed Jun 28, 2015
2 parents 195e93c + bb1a387 commit 6f187c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Http/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ public function getUrl($name, $path = '', $params = array())
}

if (!empty($params)) {
//it needs to be PHP_QUERY_RFC3986. We want to have %20 between scopes
// we cant run http_build_query($params, null, '&', PHP_QUERY_RFC3986); because it is not supported in php 5.3 or hhvm
$url .= '?';
foreach ($params as $key => $value) {
$url .= sprintf('%s=%s&', rawurlencode($key), rawurlencode($value));
// does it exist a query string?
$queryString = parse_url($url, PHP_URL_QUERY);
if (empty($queryString)) {
$url .= '?';
} else {
$url .= '&';
}
$url = rtrim($url, '&');

// it needs to be PHP_QUERY_RFC3986. We want to have %20 between scopes
$url .= http_build_query($params, null, '&', PHP_QUERY_RFC3986);
}

return $url;
Expand Down
12 changes: 12 additions & 0 deletions tests/Http/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ public function testGetUrl()
$this->assertNotEquals($notExpected, $gen->getUrl('api', 'foobar', array('bar' => 'baz a b')), 'Dont use PHP_QUERY_RFC1738');
}

public function testGetUrlWithParams()
{
$gen = new UrlGenerator();

$expected = 'https://api.linkedin.com/endpoint?bar=baz&format=json';
$this->assertEquals($expected, $gen->getUrl('api', 'endpoint?bar=baz', array('format' => 'json')));

$expected = 'https://api.linkedin.com/endpoint?bar=baz&bar=baz';
$this->assertEquals($expected, $gen->getUrl('api', 'endpoint?bar=baz', array('bar' => 'baz')));

}

public function testGetCurrentURL()
{
$gen = $this->getMock('Happyr\LinkedIn\Http\UrlGenerator', array('getHttpProtocol', 'getHttpHost', 'dropLinkedInParams'), array());
Expand Down

0 comments on commit 6f187c9

Please sign in to comment.