add http_parse_headers, update test case

This commit is contained in:
wing328
2015-12-06 23:58:25 +08:00
parent bb341832a5
commit b282d4fbea
9 changed files with 146 additions and 52 deletions

View File

@@ -216,7 +216,7 @@ class ApiClient
// Make the request
$response = curl_exec($curl);
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$http_header = substr($response, 0, $http_header_size);
$http_header = $this->http_parse_headers(substr($response, 0, $http_header_size));
$http_body = substr($response, $http_header_size);
$response_info = curl_getinfo($curl);
@@ -287,4 +287,52 @@ class ApiClient
return implode(',', $content_type);
}
}
/**
* Return an array of HTTP response headers
*
* @param string $raw_headers A string of raw HTTP response headers
*
* @return string[] Array of HTTP response heaers
*/
protected function http_parse_headers($raw_headers)
{
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
$headers = array();
$key = ''; // [+]
foreach(explode("\n", $raw_headers) as $i => $h)
{
$h = explode(':', $h, 2);
if (isset($h[1]))
{
if (!isset($headers[$h[0]]))
$headers[$h[0]] = trim($h[1]);
elseif (is_array($headers[$h[0]]))
{
// $tmp = array_merge($headers[$h[0]], array(trim($h[1]))); // [-]
// $headers[$h[0]] = $tmp; // [-]
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); // [+]
}
else
{
// $tmp = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [-]
// $headers[$h[0]] = $tmp; // [-]
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [+]
}
$key = $h[0]; // [+]
}
else // [+]
{ // [+]
if (substr($h[0], 0, 1) == "\t") // [+]
$headers[$key] .= "\r\n\t".trim($h[0]); // [+]
elseif (!$key) // [+]
$headers[0] = trim($h[0]);trim($h[0]); // [+]
} // [+]
}
return $headers;
}
}

View File

@@ -198,7 +198,7 @@ class ObjectSerializer
$deserialized = $data;
} elseif ($class === '\SplFileObject') {
// determine file name
if (preg_match('/Content-Disposition: inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader, $match)) {
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader['Content-Disposition'], $match)) {
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1];
} else {
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');

View File

@@ -208,7 +208,9 @@ use \{{invokerPackage}}\ObjectSerializer;
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader), $statusCode, $httpHeader);
{{/returnType}}{{#returnType}}
return array(null, $statusCode, $httpHeader);
{{/returnType}}
} catch (ApiException $e) {
switch ($e->getCode()) { {{#responses}}{{#dataType}}
@@ -220,9 +222,6 @@ use \{{invokerPackage}}\ObjectSerializer;
throw $e;
}
{{#returnType}}
return array(null, $statusCode, $httpHeader);
{{/returnType}}
}
{{/operation}}
}

View File

@@ -171,7 +171,6 @@ class PetApi
throw $e;
}
}
/**
@@ -254,7 +253,6 @@ class PetApi
throw $e;
}
}
/**
@@ -334,7 +332,9 @@ class PetApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Pet[]', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Pet[]', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -346,9 +346,6 @@ class PetApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -428,7 +425,9 @@ class PetApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Pet[]', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Pet[]', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -440,9 +439,6 @@ class PetApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -532,7 +528,9 @@ class PetApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Pet', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Pet', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -544,9 +542,6 @@ class PetApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -652,7 +647,6 @@ class PetApi
throw $e;
}
}
/**
@@ -747,7 +741,6 @@ class PetApi
throw $e;
}
}
/**
@@ -859,7 +852,6 @@ class PetApi
throw $e;
}
}
}

View File

@@ -165,7 +165,9 @@ class StoreApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, 'map[string,int]', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, 'map[string,int]', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -177,9 +179,6 @@ class StoreApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -255,7 +254,9 @@ class StoreApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Order', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Order', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -267,9 +268,6 @@ class StoreApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -352,7 +350,9 @@ class StoreApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Order', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\Order', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -364,9 +364,6 @@ class StoreApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -451,7 +448,6 @@ class StoreApi
throw $e;
}
}
}

View File

@@ -166,7 +166,6 @@ class UserApi
throw $e;
}
}
/**
@@ -244,7 +243,6 @@ class UserApi
throw $e;
}
}
/**
@@ -322,7 +320,6 @@ class UserApi
throw $e;
}
}
/**
@@ -402,7 +399,9 @@ class UserApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, 'string', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, 'string', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -414,9 +413,6 @@ class UserApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -488,7 +484,6 @@ class UserApi
throw $e;
}
}
/**
@@ -571,7 +566,9 @@ class UserApi
return array(null, $statusCode, $httpHeader);
}
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\User', $httpHeader));
return array($this->apiClient->getSerializer()->deserialize($response, '\Swagger\Client\Model\User', $httpHeader), $statusCode, $httpHeader);
return array(null, $statusCode, $httpHeader);
} catch (ApiException $e) {
switch ($e->getCode()) {
@@ -583,9 +580,6 @@ class UserApi
throw $e;
}
return array(null, $statusCode, $httpHeader);
}
/**
@@ -676,7 +670,6 @@ class UserApi
throw $e;
}
}
/**
@@ -761,7 +754,6 @@ class UserApi
throw $e;
}
}
}

View File

@@ -216,7 +216,7 @@ class ApiClient
// Make the request
$response = curl_exec($curl);
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$http_header = substr($response, 0, $http_header_size);
$http_header = $this->http_parse_headers(substr($response, 0, $http_header_size));
$http_body = substr($response, $http_header_size);
$response_info = curl_getinfo($curl);
@@ -287,4 +287,52 @@ class ApiClient
return implode(',', $content_type);
}
}
/**
* Return an array of HTTP response headers
*
* @param string $raw_headers A string of raw HTTP response headers
*
* @return string[] Array of HTTP response heaers
*/
protected function http_parse_headers($raw_headers)
{
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
$headers = array();
$key = ''; // [+]
foreach(explode("\n", $raw_headers) as $i => $h)
{
$h = explode(':', $h, 2);
if (isset($h[1]))
{
if (!isset($headers[$h[0]]))
$headers[$h[0]] = trim($h[1]);
elseif (is_array($headers[$h[0]]))
{
// $tmp = array_merge($headers[$h[0]], array(trim($h[1]))); // [-]
// $headers[$h[0]] = $tmp; // [-]
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); // [+]
}
else
{
// $tmp = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [-]
// $headers[$h[0]] = $tmp; // [-]
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); // [+]
}
$key = $h[0]; // [+]
}
else // [+]
{ // [+]
if (substr($h[0], 0, 1) == "\t") // [+]
$headers[$key] .= "\r\n\t".trim($h[0]); // [+]
elseif (!$key) // [+]
$headers[0] = trim($h[0]);trim($h[0]); // [+]
} // [+]
}
return $headers;
}
}

View File

@@ -198,7 +198,7 @@ class ObjectSerializer
$deserialized = $data;
} elseif ($class === '\SplFileObject') {
// determine file name
if (preg_match('/Content-Disposition: inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader, $match)) {
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeader['Content-Disposition'], $match)) {
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1];
} else {
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');

View File

@@ -105,6 +105,25 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
}
// test getPetById with a Pet object (id 10005)
public function testGetPetByIdWithHttpInfo()
{
// initialize the API client without host
$pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetAPI();
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
// return Pet (model)
list($response, $status_code, $response_headers) = $pet_api->getPetByIdWithHttpInfo($pet_id);
$this->assertSame($response->getId(), $pet_id);
$this->assertSame($response->getName(), 'PHP Unit Test');
$this->assertSame($response->getCategory()->getId(), $pet_id);
$this->assertSame($response->getCategory()->getName(), 'test php category');
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
$this->assertSame($status_code, 200);
$this->assertSame($response_headers['Content-Type'], 'application/json');
}
// test getPetByStatus and verify by the "id" of the response
public function testFindPetByStatus()
{