Skip to content

Commit

Permalink
Merge pull request #56 from ThaDafinser/feature/udger
Browse files Browse the repository at this point in the history
fixing udger, wrong response codes
  • Loading branch information
ThaDafinser committed Feb 9, 2016
2 parents 731c836 + 03df879 commit 7367948
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
39 changes: 16 additions & 23 deletions src/Provider/Http/UdgerCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
*/
Expand Down
35 changes: 30 additions & 5 deletions tests/unit/Provider/Http/UdgerComTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testGetResultNoResultFoundExceptionEmptyUserAgent()
}

/**
* 400 - flag 4
* 200 - flag 4
*
* @expectedException \UserAgentParser\Exception\InvalidCredentialsException
*/
Expand All @@ -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');
Expand All @@ -111,7 +113,7 @@ public function testGetResultInvalidCredentialsException()
}

/**
* 400 - flag 4
* 200 - flag 6
*
* @expectedException \UserAgentParser\Exception\LimitationExceededException
*/
Expand All @@ -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');
Expand All @@ -134,7 +159,7 @@ public function testGetResultLimitationExceededException()
*
* @expectedException \UserAgentParser\Exception\RequestException
*/
public function testGetResultRequestException()
public function testGetResultRequestException2()
{
$responseQueue = [
new Response(500),
Expand Down

0 comments on commit 7367948

Please sign in to comment.