From 20439e646f4c06212582e94931cf2f09488944a0 Mon Sep 17 00:00:00 2001 From: wing328 Date: Wed, 20 May 2015 18:25:35 +0800 Subject: [PATCH] add test case and prefix support --- .../src/main/resources/php/APIClient.mustache | 25 +++++++++++-- .../php/SwaggerClient-php/lib/APIClient.php | 37 ++++++++++++++----- .../SwaggerClient-php/tests/PetApiTest.php | 30 +++++++++++++++ 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache index e1de6566b17..573b3183f51 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -67,6 +67,19 @@ class APIClient { $this->curl_timeout = $seconds; } + /** + * Get API key (with prefix if set) + * @param string key name + * @return string + */ + public function getApiKeyWithPrefix($apiKey) { + if (Configuration::$apiKeyPrefix[$apiKey]) { + return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey]; + } else { + return Configuration::$apiKey[$apiKey]; + } + } + /** * update hearder and query param based on authentication setting * @@ -85,15 +98,16 @@ class APIClient { switch($auth) { {{#authMethods}} case '{{name}}': - {{#isApiKey}}{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = Configuration::$apiKey['{{keyParamName}}'];{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = Configuration::$apiKey['{{keyParamName}}'];{{/isKeyInQuery}}{{#isBasic}}$headerParams['Authorization'] = base64_encode(Configuraiton::$username.":"Configuration::$password){{/isBasic}}{{/isApiKey}} - {{#isOAuth}}#TODO support oauth{{/isOAuth}} - break; + {{#isApiKey}}{{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $this->getApiKeyWithPrefix('{{keyParamName}}');{{/isKeyInQuery}}{{#isBasic}}$headerParams['Authorization'] = base64_encode(Configuraiton::$username.":"Configuration::$password){{/isBasic}}{{/isApiKey}} + {{#isOAuth}}//TODO support oauth{{/isOAuth}} + break; {{/authMethods}} + default: + //TODO show warning about security definition not found } } } - /** * @param string $resourcePath path to method endpoint * @param string $method method to call @@ -107,6 +121,9 @@ class APIClient { $headers = array(); + # determine authentication setting + $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); + # Allow API key from $headerParams to override default $added_api_key = False; if ($headerParams != null) { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php index d6b74f610e2..fed11b95450 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php @@ -67,6 +67,19 @@ class APIClient { $this->curl_timeout = $seconds; } + /** + * Get API key (with prefix if set) + * @param string key name + * @return string + */ + public function getApiKeyWithPrefix($apiKey) { + if (Configuration::$apiKeyPrefix[$apiKey]) { + return Configuration::$apiKeyPrefix[$apiKey]." ".Configuration::$apiKey[$apiKey]; + } else { + return Configuration::$apiKey[$apiKey]; + } + } + /** * update hearder and query param based on authentication setting * @@ -85,25 +98,26 @@ class APIClient { switch($auth) { case 'api_key': - $headerParams['api_key'] = Configuration::$apiKey['api_key']; - - break; + $headerParams['api_key'] = $this->getApiKeyWithPrefix('api_key'); + + break; case 'api_secret': - $queryParams['api_secret'] = Configuration::$apiKey['api_secret']; - - break; + $queryParams['api_secret'] = $this->getApiKeyWithPrefix('api_secret'); + + break; case 'petstore_auth': - - #TODO support oauth - break; + + //TODO support oauth + break; + default: + //TODO show warning about security definition not found } } } - /** * @param string $resourcePath path to method endpoint * @param string $method method to call @@ -117,6 +131,9 @@ class APIClient { $headers = array(); + # determine authentication setting + $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); + # Allow API key from $headerParams to override default $added_api_key = False; if ($headerParams != null) { diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index d2d398ae140..fe5ed3a9ae2 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -31,6 +31,36 @@ class PetApiTest extends \PHPUnit_Framework_TestCase $add_response = $pet_api->addPet($new_pet); } + public function testConfiguration() + { + $api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); + SwaggerClient\Configuration::$apiKey['api_key'] = '123456'; + $headerParams = array('test1' => 'value1'); + $queryParams = array('test2' => 'value2'); + $authSettings = array('api_key', 'unknown'); + + # test prefix + SwaggerClient\Configuration::$apiKeyPrefix['api_key'] = 'PREFIX'; + $this->assertSame('PREFIX', SwaggerClient\Configuration::$apiKeyPrefix['api_key']); + + # update parameters based on auth setting + $api_client->updateParamsForAuth($headerParams, $queryParams, $authSettings); + + # test api key + $this->assertSame($headerParams['test1'], 'value1'); + $this->assertSame($headerParams['api_key'], 'PREFIX 123456'); + $this->assertSame($queryParams['test2'], 'value2'); + + # test http basic auth + SwaggerClient\Configuration::$username = 'test_username'; + SwaggerClient\Configuration::$password = 'test_password'; + $this->assertSame('test_username', SwaggerClient\Configuration::$username); + $this->assertSame('test_password', SwaggerClient\Configuration::$password); + + + + } + // test getPetById with a Pet object (id 10005) public function testGetPetById() {