Merge headers of ocs results

This commit is contained in:
Thomas Müller 2015-08-10 21:33:50 +02:00
parent 6006a03ef1
commit 2c8eaacec7
3 changed files with 46 additions and 37 deletions

View File

@ -201,7 +201,8 @@ class OC_API {
$picked = reset($shipped['failed']); $picked = reset($shipped['failed']);
$code = $picked['response']->getStatusCode(); $code = $picked['response']->getStatusCode();
$meta = $picked['response']->getMeta(); $meta = $picked['response']->getMeta();
$response = new OC_OCS_Result($data, $code, $meta['message']); $headers = $picked['response']->getHeaders();
$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
return $response; return $response;
} elseif(!empty($shipped['succeeded'])) { } elseif(!empty($shipped['succeeded'])) {
$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
@ -214,13 +215,16 @@ class OC_API {
$picked = reset($thirdparty['failed']); $picked = reset($thirdparty['failed']);
$code = $picked['response']->getStatusCode(); $code = $picked['response']->getStatusCode();
$meta = $picked['response']->getMeta(); $meta = $picked['response']->getMeta();
$response = new OC_OCS_Result($data, $code, $meta['message']); $headers = $picked['response']->getHeaders();
$response = new OC_OCS_Result($data, $code, $meta['message'], $headers);
return $response; return $response;
} else { } else {
$responses = $thirdparty['succeeded']; $responses = $thirdparty['succeeded'];
} }
// Merge the successful responses // Merge the successful responses
$data = array(); $data = [];
$codes = [];
$header = [];
foreach($responses as $response) { foreach($responses as $response) {
if($response['shipped']) { if($response['shipped']) {
@ -228,8 +232,9 @@ class OC_API {
} else { } else {
$data = array_merge_recursive($data, $response['response']->getData()); $data = array_merge_recursive($data, $response['response']->getData());
} }
$codes[] = array('code' => $response['response']->getStatusCode(), $header = array_merge_recursive($header, $response['response']->getHeaders());
'meta' => $response['response']->getMeta()); $codes[] = ['code' => $response['response']->getStatusCode(),
'meta' => $response['response']->getMeta()];
} }
// Use any non 100 status codes // Use any non 100 status codes
@ -243,8 +248,7 @@ class OC_API {
} }
} }
$result = new OC_OCS_Result($data, $statusCode, $statusMessage); return new OC_OCS_Result($data, $statusCode, $statusMessage, $header);
return $result;
} }
/** /**

View File

@ -51,7 +51,7 @@ class OC_OCS_Result{
* @param int $code * @param int $code
* @param null|string $message * @param null|string $message
*/ */
public function __construct($data=null, $code=100, $message=null) { public function __construct($data = null, $code = 100, $message = null, $headers = []) {
if ($data === null) { if ($data === null) {
$this->data = array(); $this->data = array();
} elseif (!is_array($data)) { } elseif (!is_array($data)) {
@ -61,6 +61,7 @@ class OC_OCS_Result{
} }
$this->statusCode = $code; $this->statusCode = $code;
$this->message = $message; $this->message = $message;
$this->headers = $headers;
} }
/** /**

View File

@ -14,11 +14,13 @@ class Test_API extends \Test\TestCase {
* @param string $message * @param string $message
*/ */
function buildResponse($shipped, $data, $code, $message=null) { function buildResponse($shipped, $data, $code, $message=null) {
return array( $resp = new OC_OCS_Result($data, $code, $message);
$resp->addHeader('KEY', 'VALUE');
return [
'shipped' => $shipped, 'shipped' => $shipped,
'response' => new OC_OCS_Result($data, $code, $message), 'response' => $resp,
'app' => $this->getUniqueID('testapp_'), 'app' => $this->getUniqueID('testapp_'),
); ];
} }
// Validate details of the result // Validate details of the result
@ -35,11 +37,11 @@ class Test_API extends \Test\TestCase {
} }
function dataProviderTestOneResult() { function dataProviderTestOneResult() {
return array( return [
array(100, true), [100, true],
array(101, false), [101, false],
array(997, false), [997, false],
); ];
} }
/** /**
@ -50,47 +52,47 @@ class Test_API extends \Test\TestCase {
*/ */
public function testOneResult($statusCode, $succeeded) { public function testOneResult($statusCode, $succeeded) {
// Setup some data arrays // Setup some data arrays
$data1 = array( $data1 = [
'users' => array( 'users' => [
'tom' => array( 'tom' => [
'key' => 'value', 'key' => 'value',
), ],
'frank' => array( 'frank' => [
'key' => 'value', 'key' => 'value',
), ],
)); ]];
// Test merging one success result // Test merging one success result
$response = $this->buildResponse(true, $data1, $statusCode); $response = $this->buildResponse(true, $data1, $statusCode);
$result = OC_API::mergeResponses(array($response)); $result = OC_API::mergeResponses([$response]);
$this->assertEquals($response['response'], $result); $this->assertEquals($response['response'], $result);
$this->checkResult($result, $succeeded); $this->checkResult($result, $succeeded);
} }
function dataProviderTestMergeResponses() { function dataProviderTestMergeResponses() {
return array( return [
// Two shipped success results // Two shipped success results
array(true, 100, true, 100, true), [true, 100, true, 100, true],
// Two shipped results, one success and one failure // Two shipped results, one success and one failure
array(true, 100, true, 998, false), [true, 100, true, 998, false],
// Two shipped results, both failure // Two shipped results, both failure
array(true, 997, true, 998, false), [true, 997, true, 998, false],
// Two third party success results // Two third party success results
array(false, 100, false, 100, true), [false, 100, false, 100, true],
// Two third party results, one success and one failure // Two third party results, one success and one failure
array(false, 100, false, 998, false), [false, 100, false, 998, false],
// Two third party results, both failure // Two third party results, both failure
array(false, 997, false, 998, false), [false, 997, false, 998, false],
// One of each, both success // One of each, both success
array(false, 100, true, 100, true), [false, 100, true, 100, true],
array(true, 100, false, 100, true), [true, 100, false, 100, true],
// One of each, both failure // One of each, both failure
array(false, 997, true, 998, false), [false, 997, true, 998, false],
// One of each, shipped success // One of each, shipped success
array(false, 997, true, 100, true), [false, 997, true, 100, true],
// One of each, third party success // One of each, third party success
array(false, 100, true, 998, false), [false, 100, true, 998, false],
); ];
} }
/** /**
* @dataProvider dataProviderTestMergeResponses * @dataProvider dataProviderTestMergeResponses
@ -131,9 +133,11 @@ class Test_API extends \Test\TestCase {
$this->checkResult($result, $succeeded); $this->checkResult($result, $succeeded);
$resultData = $result->getData(); $resultData = $result->getData();
$resultMeta = $result->getMeta(); $resultMeta = $result->getMeta();
$resultHeaders = $result->getHeaders();
$resultStatusCode = $result->getStatusCode(); $resultStatusCode = $result->getStatusCode();
$this->assertArrayHasKey('jan', $resultData['users']); $this->assertArrayHasKey('jan', $resultData['users']);
$this->assertArrayHasKey('KEY', $resultHeaders);
// check if the returned status message matches the selected status code // check if the returned status message matches the selected status code
if ($resultStatusCode === 997) { if ($resultStatusCode === 997) {