From 03df879cf7bd2d1887c9df0406de9ce23bb59a94 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Tue, 9 Feb 2016 11:37:07 +0100 Subject: [PATCH] fixing udger, wrong response codes --- src/Provider/Http/UdgerCom.php | 39 ++++++++++------------- tests/unit/Provider/Http/UdgerComTest.php | 35 +++++++++++++++++--- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/Provider/Http/UdgerCom.php b/src/Provider/Http/UdgerCom.php index 4b93ed2..4a195ce 100644 --- a/src/Provider/Http/UdgerCom.php +++ b/src/Provider/Http/UdgerCom.php @@ -107,29 +107,7 @@ protected function getResult($userAgent, array $headers) 'Content-Type' => 'application/x-www-form-urlencoded', ], $body); - try { - $response = $this->getResponse($request); - } catch (Exception\RequestException $ex) { - /* @var $prevEx \GuzzleHttp\Exception\ClientException */ - $prevEx = $ex->getPrevious(); - - if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 400) { - $content = $prevEx->getResponse() - ->getBody() - ->getContents(); - $content = json_decode($content); - - if (isset($content->flag) && $content->flag == 4) { - throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); - } - - if (isset($content->flag) && $content->flag == 6) { - throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName(), null, $ex); - } - } - - throw $ex; - } + $response = $this->getResponse($request); /* * no json returned? @@ -148,6 +126,21 @@ protected function getResult($userAgent, array $headers) throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); } + /* + * Errors + */ + if (isset($content->flag) && $content->flag == 4) { + throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName()); + } + + if (isset($content->flag) && $content->flag == 6) { + throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName()); + } + + if (isset($content->flag) && $content->flag > 3) { + throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); + } + /* * Missing data? */ diff --git a/tests/unit/Provider/Http/UdgerComTest.php b/tests/unit/Provider/Http/UdgerComTest.php index ca533f0..3ff5638 100644 --- a/tests/unit/Provider/Http/UdgerComTest.php +++ b/tests/unit/Provider/Http/UdgerComTest.php @@ -92,7 +92,7 @@ public function testGetResultNoResultFoundExceptionEmptyUserAgent() } /** - * 400 - flag 4 + * 200 - flag 4 * * @expectedException \UserAgentParser\Exception\InvalidCredentialsException */ @@ -102,7 +102,9 @@ public function testGetResultInvalidCredentialsException() $rawResult->flag = 4; $responseQueue = [ - new Response(400, [], json_encode($rawResult)), + new Response(200, [ + 'Content-Type' => 'application/json', + ], json_encode($rawResult)), ]; $provider = new UdgerCom($this->getClient($responseQueue), 'apiKey123'); @@ -111,7 +113,7 @@ public function testGetResultInvalidCredentialsException() } /** - * 400 - flag 4 + * 200 - flag 6 * * @expectedException \UserAgentParser\Exception\LimitationExceededException */ @@ -121,7 +123,30 @@ public function testGetResultLimitationExceededException() $rawResult->flag = 6; $responseQueue = [ - new Response(400, [], json_encode($rawResult)), + new Response(200, [ + 'Content-Type' => 'application/json', + ], json_encode($rawResult)), + ]; + + $provider = new UdgerCom($this->getClient($responseQueue), 'apiKey123'); + + $provider->parse('A real user agent...'); + } + + /** + * 200 - flag 99 + * + * @expectedException \UserAgentParser\Exception\RequestException + */ + public function testGetResultRequestException1() + { + $rawResult = new stdClass(); + $rawResult->flag = 99; + + $responseQueue = [ + new Response(200, [ + 'Content-Type' => 'application/json', + ], json_encode($rawResult)), ]; $provider = new UdgerCom($this->getClient($responseQueue), 'apiKey123'); @@ -134,7 +159,7 @@ public function testGetResultLimitationExceededException() * * @expectedException \UserAgentParser\Exception\RequestException */ - public function testGetResultRequestException() + public function testGetResultRequestException2() { $responseQueue = [ new Response(500),