From cc5c2d85bf69987795b6db5f9e2f3154552533f2 Mon Sep 17 00:00:00 2001 From: Lucas Giovanny Date: Fri, 26 Aug 2022 10:06:17 +0100 Subject: [PATCH 1/2] Add a protocol by default on config endpoint --- src/Kitar/Dynamodb/Connection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Kitar/Dynamodb/Connection.php b/src/Kitar/Dynamodb/Connection.php index 6be55ef..860d51c 100644 --- a/src/Kitar/Dynamodb/Connection.php +++ b/src/Kitar/Dynamodb/Connection.php @@ -79,6 +79,10 @@ protected function createClient(array $config) 'endpoint' => $config['endpoint'] ?? null, ]; + if(preg_match('#^https?://#i', $dynamoConfig['endpoint']) === 0){ + $dynamoConfig['endpoint'] = "https://" . $dynamoConfig['endpoint']; + } + if ($key = $config['access_key'] ?? null) { $config['key'] = $key; unset($config['access_key']); From baef8cf7c5cf5ab746255f2a9dc6652b69b86e47 Mon Sep 17 00:00:00 2001 From: Satoshi Kita Date: Fri, 26 Aug 2022 21:01:15 +0900 Subject: [PATCH 2/2] add tests --- src/Kitar/Dynamodb/Connection.php | 2 +- tests/ConnectionTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Kitar/Dynamodb/Connection.php b/src/Kitar/Dynamodb/Connection.php index 860d51c..fa09132 100644 --- a/src/Kitar/Dynamodb/Connection.php +++ b/src/Kitar/Dynamodb/Connection.php @@ -79,7 +79,7 @@ protected function createClient(array $config) 'endpoint' => $config['endpoint'] ?? null, ]; - if(preg_match('#^https?://#i', $dynamoConfig['endpoint']) === 0){ + if (! empty($dynamoConfig['endpoint']) && preg_match('#^https?://#i', $dynamoConfig['endpoint']) === 0) { $dynamoConfig['endpoint'] = "https://" . $dynamoConfig['endpoint']; } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index e093ab7..682dd83 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -96,4 +96,30 @@ public function it_can_forward_call_to_dynamodb_client() 'TableName' => 'User' ]); } + + /** @test */ + public function it_prepends_default_protocol_if_not_given() + { + $connection = new Connection(['endpoint' => 'examples.com']); + $this->assertEquals($connection->getClient()->getEndpoint()->getScheme(), 'https'); + $this->assertEquals($connection->getClient()->getEndpoint()->getHost(), 'examples.com'); + $this->assertEquals($this->connection->getClient()->getEndpoint()->getScheme(), 'https'); + $this->assertEquals($this->connection->getClient()->getEndpoint()->getHost(), 'dynamodb.us-east-1.amazonaws.com'); + } + + /** @test */ + public function it_dont_prepends_default_protocol_if_http_given() + { + $connection = new Connection(['endpoint' => 'http://examples.com']); + $this->assertEquals($connection->getClient()->getEndpoint()->getScheme(), 'http'); + $this->assertEquals($connection->getClient()->getEndpoint()->getHost(), 'examples.com'); + } + + /** @test */ + public function it_dont_prepends_default_protocol_if_https_given() + { + $connection = new Connection(['endpoint' => 'https://examples.com']); + $this->assertEquals($connection->getClient()->getEndpoint()->getScheme(), 'https'); + $this->assertEquals($connection->getClient()->getEndpoint()->getHost(), 'examples.com'); + } }