Skip to content

Commit

Permalink
Ensure that custom endpoints have a defined scheme and host
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew committed Aug 10, 2015
1 parent 112c054 commit 037bddb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/ClientResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ClientResolver
'type' => 'value',
'valid' => ['string'],
'doc' => 'The full URI of the webservice. This is only required when connecting to a custom endpoint (e.g., a local version of S3).',
'fn' => [__CLASS__, '_apply_endpoint'],
],
'region' => [
'type' => 'value',
Expand Down Expand Up @@ -301,7 +302,7 @@ private function invalidType($name, $provided)
. "provided for \"{$name}\". Expected {$expected}, but got "
. describe_type($provided) . "\n\n"
. $this->getArgMessage($name);
throw new \InvalidArgumentException($msg);
throw new IAE($msg);
}

/**
Expand Down Expand Up @@ -476,6 +477,18 @@ public static function _apply_user_agent($value, array &$args, HandlerList $list
});
}

public static function _apply_endpoint($value, array &$args, HandlerList $list)
{
$parts = parse_url($value);
if (empty($parts['scheme']) || empty($parts['host'])) {
throw new IAE(
'Endpoints must be full URIs and include a scheme and host'
);
}

$args['endpoint'] = $value;
}

public static function _default_endpoint_provider()
{
return EndpointProvider::defaultProvider();
Expand Down
22 changes: 22 additions & 0 deletions tests/ClientResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,26 @@ public function testUserAgentAlwaysStartsWithSdkAgentString()
ClientResolver::_apply_user_agent([], $args, $list);
call_user_func($list->resolve(), $command, $request);
}

public function malformedEndpointProvider()
{
return [
['www.amazon.com'], // missing protocol
['https://'], // missing host
];
}

/**
* @dataProvider malformedEndpointProvider
* @param $endpoint
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Endpoints must be full URIs and include a scheme and host
*/
public function testRejectsMalformedEndpoints($endpoint)
{
$list = new HandlerList();
$args = [];
ClientResolver::_apply_endpoint($endpoint, $args, $list);
}
}

0 comments on commit 037bddb

Please sign in to comment.