Skip to content

Commit

Permalink
Api: allows absolute HTTPS URLs with different hostname [Closes #18]
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed Oct 27, 2016
1 parent a9be108 commit 1fcb441
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Github/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,16 @@ public function request(Http\Request $request)
*/
public function createRequest($method, $urlPath, array $parameters = [], array $headers = [], $content = NULL)
{
if (stripos($urlPath, $this->url) === 0) {
if (stripos($urlPath, $this->url) === 0) { # Allows non-HTTPS URLs
$baseUrl = $this->url;
$urlPath = substr($urlPath, strlen($this->url));

} elseif (preg_match('#^(https://[^/]+)(/.*)?$#', $urlPath, $m)) {
$baseUrl = $m[1];
$urlPath = isset($m[2]) ? $m[2] : '';

} else {
$baseUrl = $this->url;
}

if (strpos($urlPath, '{') === FALSE) {
Expand All @@ -226,7 +234,7 @@ public function createRequest($method, $urlPath, array $parameters = [], array $
$urlPath = $this->expandUriTemplate($urlPath, $parameters, $this->defaultParameters);
}

$url = rtrim($this->url, '/') . '/' . ltrim($urlPath, '/');
$url = rtrim($baseUrl, '/') . '/' . ltrim($urlPath, '/');

if ($content !== NULL && (is_array($content) || is_object($content))) {
$headers['Content-Type'] = 'application/json; charset=utf-8';
Expand Down
8 changes: 8 additions & 0 deletions tests/Github/Api.expandColonParameters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ test(function() {
Assert::same('/default', $api->expandColonParameters('/:var', [], ['var' => 'default']));
Assert::same('/set', $api->expandColonParameters('/:var', ['var' => 'set'], ['var' => 'default']));
});


# Expanding in absolute URL
test(function () {
$api = new Milo\Github\Api;
$request = $api->createRequest('', 'https://host.:name.:tld/path/:name', ['name' => 'milo', 'tld' => 'cz']);
Assert::same('https://host.:name.:tld/path/milo?tld=cz', $request->getUrl());
});
8 changes: 8 additions & 0 deletions tests/Github/Api.expandUriTemplate.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,11 @@ test(function() use ($api) {
Assert::same($expected, $api->expandUriTemplate($template, $parameters));
}
});


# Expanding in absolute URL
test(function () {
$api = new Milo\Github\Api;
$request = $api->createRequest('', 'https://host.{name}.{tld}/path/{name}', ['name' => 'milo', 'tld' => 'cz']);
Assert::same('https://host.{name}.{tld}/path/milo', $request->getUrl());
});
14 changes: 12 additions & 2 deletions tests/Github/Api.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test(function() {
Assert::same(['content-type' => 'application/json; charset=utf-8'], $request->getHeaders());
Assert::same('{"foo":"bar"}', $request->getContent());

## Object to JSON
# Object to JSON
$request = $api->createRequest('', '', [], [], (object) ['foo' => 'bar']);
Assert::same(['content-type' => 'application/json; charset=utf-8'], $request->getHeaders());
Assert::same('{"foo":"bar"}', $request->getContent());
Expand Down Expand Up @@ -97,7 +97,7 @@ test(function() {
});


# Api called with full URL
# Api called with absolute URL
test(function() {
$client = new MockIClient;
$api = new Milo\Github\Api($client);
Expand All @@ -111,6 +111,16 @@ test(function() {

$request = $api->createRequest('', 'uRl://TeSt/path', [], [], NULL);
Assert::same('url://test/path', $request->getUrl());

# Absolute HTTPS URL with different host
$request = $api->createRequest('', 'https://example.com', [], [], NULL);
Assert::same('https://example.com/', $request->getUrl());
$request = $api->createRequest('', 'https://example.com/path', [], [], NULL);
Assert::same('https://example.com/path', $request->getUrl());

# Absolute non-HTTPS URL with different host is not allowed (should be?)
$request = $api->createRequest('', 'http://example.com', [], [], NULL);
Assert::same('url://test/http://example.com', $request->getUrl());
});


Expand Down

0 comments on commit 1fcb441

Please sign in to comment.