add test case and prefix support

This commit is contained in:
wing328 2015-05-20 18:25:35 +08:00
parent 06c7a6a109
commit 20439e646f
3 changed files with 78 additions and 14 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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()
{