diff --git a/src/V2/Localization/Exception/InvalidLanguageException.php b/src/V2/Localization/Exception/InvalidLanguageException.php new file mode 100644 index 0000000..53d621f --- /dev/null +++ b/src/V2/Localization/Exception/InvalidLanguageException.php @@ -0,0 +1,47 @@ +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; + } + +} diff --git a/src/V2/Localization/LocalizationHandler.php b/src/V2/Localization/LocalizationHandler.php index dcaf4ed..5edf0df 100644 --- a/src/V2/Localization/LocalizationHandler.php +++ b/src/V2/Localization/LocalizationHandler.php @@ -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 ) { @@ -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 ); + } + } } diff --git a/tests/LocalizedEndpointTest.php b/tests/LocalizedEndpointTest.php index 90262cc..73bb826 100644 --- a/tests/LocalizedEndpointTest.php +++ b/tests/LocalizedEndpointTest.php @@ -1,5 +1,6 @@ mockResponse('[]'); - $this->mockResponse('[]'); + $this->mockResponse('[]', 'de'); + $this->mockResponse('[]', 'de'); $endpoint_de = $this->getLocalizedEndpoint()->lang('de'); @@ -36,7 +37,7 @@ public function testRepeated() { } public function testNested() { - $this->mockResponse('[]'); + $this->mockResponse('[]', 'fr'); $this->getLocalizedEndpoint()->lang('es')->lang('fr')->test(); @@ -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(); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5b629c4..fdf2838 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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(); @@ -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 );