Skip to content

Commit 1fcb441

Browse files
committed
Api: allows absolute HTTPS URLs with different hostname [Closes #18]
1 parent a9be108 commit 1fcb441

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

src/Github/Api.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,16 @@ public function request(Http\Request $request)
216216
*/
217217
public function createRequest($method, $urlPath, array $parameters = [], array $headers = [], $content = NULL)
218218
{
219-
if (stripos($urlPath, $this->url) === 0) {
219+
if (stripos($urlPath, $this->url) === 0) { # Allows non-HTTPS URLs
220+
$baseUrl = $this->url;
220221
$urlPath = substr($urlPath, strlen($this->url));
222+
223+
} elseif (preg_match('#^(https://[^/]+)(/.*)?$#', $urlPath, $m)) {
224+
$baseUrl = $m[1];
225+
$urlPath = isset($m[2]) ? $m[2] : '';
226+
227+
} else {
228+
$baseUrl = $this->url;
221229
}
222230

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

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

231239
if ($content !== NULL && (is_array($content) || is_object($content))) {
232240
$headers['Content-Type'] = 'application/json; charset=utf-8';

tests/Github/Api.expandColonParameters.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ test(function() {
5959
Assert::same('/default', $api->expandColonParameters('/:var', [], ['var' => 'default']));
6060
Assert::same('/set', $api->expandColonParameters('/:var', ['var' => 'set'], ['var' => 'default']));
6161
});
62+
63+
64+
# Expanding in absolute URL
65+
test(function () {
66+
$api = new Milo\Github\Api;
67+
$request = $api->createRequest('', 'https://host.:name.:tld/path/:name', ['name' => 'milo', 'tld' => 'cz']);
68+
Assert::same('https://host.:name.:tld/path/milo?tld=cz', $request->getUrl());
69+
});

tests/Github/Api.expandUriTemplate.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,11 @@ test(function() use ($api) {
469469
Assert::same($expected, $api->expandUriTemplate($template, $parameters));
470470
}
471471
});
472+
473+
474+
# Expanding in absolute URL
475+
test(function () {
476+
$api = new Milo\Github\Api;
477+
$request = $api->createRequest('', 'https://host.{name}.{tld}/path/{name}', ['name' => 'milo', 'tld' => 'cz']);
478+
Assert::same('https://host.{name}.{tld}/path/milo', $request->getUrl());
479+
});

tests/Github/Api.phpt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ test(function() {
6767
Assert::same(['content-type' => 'application/json; charset=utf-8'], $request->getHeaders());
6868
Assert::same('{"foo":"bar"}', $request->getContent());
6969

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

9999

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

112112
$request = $api->createRequest('', 'uRl://TeSt/path', [], [], NULL);
113113
Assert::same('url://test/path', $request->getUrl());
114+
115+
# Absolute HTTPS URL with different host
116+
$request = $api->createRequest('', 'https://example.com', [], [], NULL);
117+
Assert::same('https://example.com/', $request->getUrl());
118+
$request = $api->createRequest('', 'https://example.com/path', [], [], NULL);
119+
Assert::same('https://example.com/path', $request->getUrl());
120+
121+
# Absolute non-HTTPS URL with different host is not allowed (should be?)
122+
$request = $api->createRequest('', 'http://example.com', [], [], NULL);
123+
Assert::same('url://test/http://example.com', $request->getUrl());
114124
});
115125

116126

0 commit comments

Comments
 (0)