Skip to content

Commit

Permalink
throw exception when response has not the requested language
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 30, 2015
1 parent 3bd0e27 commit f536ae6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
47 changes: 47 additions & 0 deletions src/V2/Localization/Exception/InvalidLanguageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace GW2Treasures\GW2Api\V2\Localization\Exception;

use GuzzleHttp\Message\ResponseInterface;
use GW2Treasures\GW2Api\Exception\ApiException;

class InvalidLanguageException extends ApiException {

/** @var string $requestLanguage */
protected $requestLanguage;

/** @var string $responseLanguage */
protected $responseLanguage;

/**
* @param string $message
* @param string $requestLanguage
* @param string $responseLanguage
* @param ResponseInterface $response
*/
public function __construct( $message, $requestLanguage, $responseLanguage, ResponseInterface $response ) {
$this->requestLanguage = $requestLanguage;
$this->responseLanguage = $responseLanguage;

parent::__construct( $message, $response );
}

/**
* Get the requested language.
*
* @return string
*/
public function getRequestLanguage() {
return $this->requestLanguage;
}

/**
* Get the language the api responded with.
*
* @return string
*/
public function getResponseLanguage() {
return $this->responseLanguage;
}

}
12 changes: 12 additions & 0 deletions src/V2/Localization/LocalizationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace GW2Treasures\GW2Api\V2\Localization;

use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\ResponseInterface;
use GW2Treasures\GW2Api\V2\ApiHandler;
use GW2Treasures\GW2Api\V2\Localization\Exception\InvalidLanguageException;

class LocalizationHandler extends ApiHandler {
function __construct( ILocalizedEndpoint $endpoint ) {
Expand All @@ -27,4 +29,14 @@ protected function getEndpoint() {
public function onRequest( RequestInterface $request ) {
$request->getQuery()->add( 'lang', $this->getEndpoint()->getLang() );
}

public function onResponse( ResponseInterface $response, RequestInterface $request ) {
$requestLanguage = $request->getQuery()->get('lang');
$responseLanguage = $response->getHeader( 'Content-Language' );

if( $requestLanguage !== $responseLanguage ) {
$message = 'Invalid language (expected: ' . $requestLanguage . '; actual: ' . $responseLanguage . ')';
throw new InvalidLanguageException( $message, $requestLanguage, $responseLanguage, $response );
}
}
}
24 changes: 21 additions & 3 deletions tests/LocalizedEndpointTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use GW2Treasures\GW2Api\V2\Localization\Exception\InvalidLanguageException;
use Stubs\LocalizedEndpointStub;

class LocalizedEndpointTest extends TestCase {
Expand All @@ -20,8 +21,8 @@ public function testBasic() {
}

public function testRepeated() {
$this->mockResponse('[]');
$this->mockResponse('[]');
$this->mockResponse('[]', 'de');
$this->mockResponse('[]', 'de');

$endpoint_de = $this->getLocalizedEndpoint()->lang('de');

Expand All @@ -36,7 +37,7 @@ public function testRepeated() {
}

public function testNested() {
$this->mockResponse('[]');
$this->mockResponse('[]', 'fr');

$this->getLocalizedEndpoint()->lang('es')->lang('fr')->test();

Expand All @@ -46,4 +47,21 @@ public function testNested() {
$this->assertEquals( 'fr', $request->getQuery()->get('lang'),
'LocalizedEndpoint sets correct query parameter value on nested request' );
}

public function testInvalidLanguage() {
$this->mockResponse('[]', 'en');

try {
$this->getLocalizedEndpoint()->lang('invalid')->test();
} catch( InvalidLanguageException $ex ) {
$this->assertEquals( 'en', $ex->getResponseLanguage() );
$this->assertEquals( 'invalid', $ex->getRequestLanguage() );

$this->assertTrue( strstr( $ex->getMessage(), 'Invalid language' ) !== false );

return;
}

$this->fail();
}
}
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function api() {
/**
* @param Response|RequestException|string $response
*/
protected function mockResponse( $response ) {
protected function mockResponse( $response, $language = 'en' ) {
if( !isset( $this->mock )) {
$this->mock = new Mock();
$this->history = new History();
Expand All @@ -38,7 +38,7 @@ protected function mockResponse( $response ) {
}
if( is_string( $response )) {
$this->mock->addResponse(
new Response( 200, ['Content-Type' => 'application/json; charset=utf-8'], Stream::factory( $response ))
new Response( 200, ['Content-Type' => 'application/json; charset=utf-8', 'Content-Language' => $language], Stream::factory( $response ))
);
} elseif( $response instanceof RequestException ) {
$this->mock->addException( $response );
Expand Down

0 comments on commit f536ae6

Please sign in to comment.