From 2006daa20302b33608f00488c069d831dc9fdad5 Mon Sep 17 00:00:00 2001 From: Sebastian Zahn Date: Wed, 12 Oct 2016 14:56:38 +0200 Subject: [PATCH] Bugfix to prevent calling the API without an Authentication --- src/MessageBird/Common/HttpClient.php | 12 +++++++++--- tests/integration/HttpClientTest.php | 13 +++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/MessageBird/Common/HttpClient.php b/src/MessageBird/Common/HttpClient.php index 1a7e0656..79a7c983 100644 --- a/src/MessageBird/Common/HttpClient.php +++ b/src/MessageBird/Common/HttpClient.php @@ -75,11 +75,11 @@ public function __construct($endpoint, $timeout = 10, $connectionTimeout = 2) } /** - * @param $string + * @param string $userAgent */ - public function addUserAgentString($string) + public function addUserAgentString($userAgent) { - $this->userAgent[] = $string; + $this->userAgent[] = $userAgent; } /** @@ -116,12 +116,18 @@ public function getRequestUrl($resourceName, $query) * @param string|null $body * * @return array + * + * @throws Exceptions\AuthenticateException * @throws Exceptions\HttpException */ public function performHttpRequest($method, $resourceName, $query = null, $body = null) { $curl = curl_init(); + if ($this->Authentication === null) { + throw new Exceptions\AuthenticateException('Can not perform API Request without Authentication'); + } + $headers = array ( 'User-agent: ' . implode(' ', $this->userAgent), 'Accepts: application/json', diff --git a/tests/integration/HttpClientTest.php b/tests/integration/HttpClientTest.php index 186fd702..161e1a8d 100644 --- a/tests/integration/HttpClientTest.php +++ b/tests/integration/HttpClientTest.php @@ -32,4 +32,17 @@ public function testHttpClientInvalidConnectionTimeout() { new HttpClient(Client::ENDPOINT, 10, new \stdClass()); } + + /** + * Test that requests can only be made when there is an Authentication set + * + * @test + * @expectedException \MessageBird\Exceptions\AuthenticateException + * @expectedExceptionMessageRegExp #Can not perform API Request without Authentication# + */ + public function testHttpClientWithoutAuthenticationException() + { + $client = new HttpClient(Client::ENDPOINT); + $client->performHttpRequest('foo', 'bar'); + } }