This cleans up a bit the OCSController/Middleware. Since the 2 versions of OCS differ a bit. Moved a lot of stuff internal since it is of no concern to the outside.tags/v11.0RC2
@@ -23,14 +23,15 @@ | |||
namespace OC\AppFramework\Middleware; | |||
use OC\AppFramework\Http; | |||
use OC\AppFramework\OCS\BaseResponse; | |||
use OC\AppFramework\OCS\V1Response; | |||
use OC\AppFramework\OCS\V2Response; | |||
use OCP\API; | |||
use OCP\AppFramework\Controller; | |||
use OCP\AppFramework\Http\DataResponse; | |||
use OCP\AppFramework\Http\JSONResponse; | |||
use OCP\AppFramework\Http\OCSResponse; | |||
use OCP\AppFramework\Http\Response; | |||
use OCP\AppFramework\OCS\OCSException; | |||
use OCP\AppFramework\OCS\OCSForbiddenException; | |||
use OCP\AppFramework\OCS\OCSNotFoundException; | |||
use OCP\AppFramework\OCSController; | |||
use OCP\IRequest; | |||
use OCP\AppFramework\Middleware; | |||
@@ -40,6 +41,9 @@ class OCSMiddleware extends Middleware { | |||
/** @var IRequest */ | |||
private $request; | |||
/** @var int */ | |||
private $ocsVersion; | |||
/** | |||
* @param IRequest $request | |||
*/ | |||
@@ -47,50 +51,36 @@ class OCSMiddleware extends Middleware { | |||
$this->request = $request; | |||
} | |||
/** | |||
* @param \OCP\AppFramework\Controller $controller | |||
* @param string $methodName | |||
*/ | |||
public function beforeController($controller, $methodName) { | |||
if ($controller instanceof OCSController) { | |||
if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) { | |||
$this->ocsVersion = 2; | |||
} else { | |||
$this->ocsVersion = 1; | |||
} | |||
$controller->setOCSVersion($this->ocsVersion); | |||
} | |||
} | |||
/** | |||
* @param \OCP\AppFramework\Controller $controller | |||
* @param string $methodName | |||
* @param \Exception $exception | |||
* @throws \Exception | |||
* @return OCSResponse | |||
* @return BaseResponse | |||
*/ | |||
public function afterException($controller, $methodName, \Exception $exception) { | |||
if ($controller instanceof OCSController && $exception instanceof OCSException) { | |||
$format = $this->getFormat($controller); | |||
$code = $exception->getCode(); | |||
if ($code === 0) { | |||
$code = API::RESPOND_UNKNOWN_ERROR; | |||
} | |||
// Build the response | |||
$response = new OCSResponse($format, $code, $exception->getMessage()); | |||
// Forbidden always sets 401 (even on v1.php) | |||
if ($exception instanceof OCSForbiddenException || $code === API::RESPOND_UNAUTHORISED) { | |||
$response->setStatus(Http::STATUS_UNAUTHORIZED); | |||
} | |||
// On v2.php we set actual HTTP error codes | |||
if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) { | |||
if ($code === API::RESPOND_NOT_FOUND) { | |||
$response->setStatus(Http::STATUS_NOT_FOUND); | |||
} else if ($code === API::RESPOND_SERVER_ERROR) { | |||
$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); | |||
} else if ($code === API::RESPOND_UNKNOWN_ERROR) { | |||
$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); | |||
} else if ($code === API::RESPOND_UNAUTHORISED) { | |||
// Already set | |||
} | |||
// 4xx and 5xx codes are forwarded as is. | |||
else if ($code >= 400 && $code < 600) { | |||
$response->setStatus($code); | |||
} else { | |||
// All other codes get a bad request | |||
$response->setStatus(Http::STATUS_BAD_REQUEST); | |||
} | |||
} | |||
return $response; | |||
return $this->buildNewResponse($controller, $code, $exception->getMessage()); | |||
} | |||
throw $exception; | |||
@@ -107,24 +97,43 @@ class OCSMiddleware extends Middleware { | |||
* If a different middleware has detected that a request unauthorized or forbidden | |||
* we need to catch the response and convert it to a proper OCS response. | |||
*/ | |||
if ($controller instanceof OCSController && !($response instanceof OCSResponse)) { | |||
if ($controller instanceof OCSController && !($response instanceof BaseResponse)) { | |||
if ($response->getStatus() === Http::STATUS_UNAUTHORIZED || | |||
$response->getStatus() === Http::STATUS_FORBIDDEN) { | |||
$format = $this->getFormat($controller); | |||
$message = ''; | |||
if ($response instanceof JSONResponse) { | |||
/** @var DataResponse $response */ | |||
$message = $response->getData()['message']; | |||
} | |||
$response = new OCSResponse($format, \OCP\API::RESPOND_UNAUTHORISED, $message); | |||
$response->setStatus(Http::STATUS_UNAUTHORIZED); | |||
return $this->buildNewResponse($controller, API::RESPOND_UNAUTHORISED, $message); | |||
} | |||
} | |||
return $response; | |||
} | |||
/** | |||
* @param Controller $controller | |||
* @param int $code | |||
* @param string $message | |||
* @return V1Response|V2Response | |||
*/ | |||
private function buildNewResponse($controller, $code, $message) { | |||
$format = $this->getFormat($controller); | |||
$data = new DataResponse(); | |||
$data->setStatus($code); | |||
if ($this->ocsVersion === 1) { | |||
$response = new V1Response($data, $format, $message); | |||
} else { | |||
$response = new V2Response($data, $format, $message); | |||
} | |||
return $response; | |||
} | |||
/** | |||
* @param \OCP\AppFramework\Controller $controller | |||
* @return string |
@@ -0,0 +1,85 @@ | |||
<?php | |||
/** | |||
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @author Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
* | |||
* This program is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License as | |||
* published by the Free Software Foundation, either version 3 of the | |||
* License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
* | |||
*/ | |||
namespace OC\AppFramework\OCS; | |||
use OCP\AppFramework\Http\DataResponse; | |||
use OCP\AppFramework\Http\Response; | |||
abstract class BaseResponse extends Response { | |||
/** @var array */ | |||
protected $data; | |||
/** @var string */ | |||
protected $format; | |||
/** @var string */ | |||
protected $statusMessage; | |||
/** @var int */ | |||
protected $itemsCount; | |||
/** @var int */ | |||
protected $itemsPerPage; | |||
/** | |||
* BaseResponse constructor. | |||
* | |||
* @param DataResponse|null $dataResponse | |||
* @param string $format | |||
* @param string|null $statusMessage | |||
* @param int|null $itemsCount | |||
* @param int|null $itemsPerPage | |||
*/ | |||
public function __construct(DataResponse $dataResponse, | |||
$format = 'xml', | |||
$statusMessage = null, | |||
$itemsCount = null, | |||
$itemsPerPage = null) { | |||
$this->format = $format; | |||
$this->statusMessage = $statusMessage; | |||
$this->itemsCount = $itemsCount; | |||
$this->itemsPerPage = $itemsPerPage; | |||
$this->data = $dataResponse->getData(); | |||
$this->setHeaders($dataResponse->getHeaders()); | |||
$this->setStatus($dataResponse->getStatus()); | |||
$this->setETag($dataResponse->getETag()); | |||
$this->setLastModified($dataResponse->getLastModified()); | |||
$this->setCookies($dataResponse->getCookies()); | |||
$this->setContentSecurityPolicy($dataResponse->getContentSecurityPolicy()); | |||
} | |||
/** | |||
* @param string[] $meta | |||
* @return string | |||
*/ | |||
protected function renderResult($meta) { | |||
// TODO rewrite functions | |||
return \OC_API::renderResult($this->format, $meta, $this->data); | |||
} | |||
public function getOCSStatus() { | |||
return parent::getStatus(); | |||
} | |||
} |
@@ -0,0 +1,78 @@ | |||
<?php | |||
/** | |||
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @author Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
* | |||
* This program is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License as | |||
* published by the Free Software Foundation, either version 3 of the | |||
* License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
* | |||
*/ | |||
namespace OC\AppFramework\OCS; | |||
use OCP\API; | |||
use OCP\AppFramework\Http; | |||
class V1Response extends BaseResponse { | |||
/** | |||
* The V1 endpoint has very limited http status codes basically everything | |||
* is status 200 except 401 | |||
* | |||
* @return int | |||
*/ | |||
public function getStatus() { | |||
$status = parent::getStatus(); | |||
if ($status === Http::STATUS_FORBIDDEN || $status === API::RESPOND_UNAUTHORISED) { | |||
return Http::STATUS_UNAUTHORIZED; | |||
} | |||
return Http::STATUS_OK; | |||
} | |||
/** | |||
* In v1 all OK is 100 | |||
* | |||
* @return int | |||
*/ | |||
public function getOCSStatus() { | |||
$status = parent::getOCSStatus(); | |||
if ($status === Http::STATUS_OK) { | |||
return 100; | |||
} | |||
return $status; | |||
} | |||
/** | |||
* Construct the meta part of the response | |||
* And then late the base class render | |||
* | |||
* @return string | |||
*/ | |||
public function render() { | |||
$meta = [ | |||
'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', | |||
'statuscode' => $this->getOCSStatus(), | |||
'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage, | |||
]; | |||
$meta['totalitems'] = $this->itemsCount !== null ? (string)$this->itemsCount : ''; | |||
$meta['itemsperpage'] = $this->itemsPerPage !== null ? (string)$this->itemsPerPage: ''; | |||
return $this->renderResult($meta); | |||
} | |||
} |
@@ -0,0 +1,76 @@ | |||
<?php | |||
/** | |||
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @author Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
* | |||
* This program is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU Affero General Public License as | |||
* published by the Free Software Foundation, either version 3 of the | |||
* License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU Affero General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Affero General Public License | |||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
* | |||
*/ | |||
namespace OC\AppFramework\OCS; | |||
use OCP\AppFramework\Http; | |||
use OCP\API; | |||
class V2Response extends BaseResponse { | |||
/** | |||
* The V2 endpoint just passes on status codes. | |||
* Of course we have to map the OCS specific codes to proper HTTP status codes | |||
* | |||
* @return int | |||
*/ | |||
public function getStatus() { | |||
$status = parent::getStatus(); | |||
if ($status === API::RESPOND_UNAUTHORISED) { | |||
return Http::STATUS_UNAUTHORIZED; | |||
} else if ($status === API::RESPOND_NOT_FOUND) { | |||
return Http::STATUS_NOT_FOUND; | |||
} else if ($status === API::RESPOND_SERVER_ERROR || $status === API::RESPOND_UNKNOWN_ERROR) { | |||
return Http::STATUS_INTERNAL_SERVER_ERROR; | |||
} else if ($status < 200 || $status > 600) { | |||
return Http::STATUS_BAD_REQUEST; | |||
} | |||
return $status; | |||
} | |||
/** | |||
* Construct the meta part of the response | |||
* And then late the base class render | |||
* | |||
* @return string | |||
*/ | |||
public function render() { | |||
$status = parent::getStatus(); | |||
$meta = [ | |||
'status' => $status >= 200 && $status < 300 ? 'ok' : 'failure', | |||
'statuscode' => $this->getOCSStatus(), | |||
'message' => $status >= 200 && $status < 300 ? 'OK' : $this->statusMessage, | |||
]; | |||
if ($this->itemsCount !== null) { | |||
$meta['totalitems'] = $this->itemsCount; | |||
} | |||
if ($this->itemsPerPage !== null) { | |||
$meta['itemsperpage'] = $this->itemsPerPage; | |||
} | |||
return $this->renderResult($meta); | |||
} | |||
} |
@@ -42,6 +42,9 @@ use OCP\IRequest; | |||
*/ | |||
abstract class OCSController extends ApiController { | |||
/** @var int */ | |||
private $ocsVersion; | |||
/** | |||
* constructor of the controller | |||
* @param string $appName the name of the app | |||
@@ -71,6 +74,15 @@ abstract class OCSController extends ApiController { | |||
}); | |||
} | |||
/** | |||
* @param int $version | |||
* @since 9.2.0 | |||
* @internal | |||
*/ | |||
public function setOCSVersion($version) { | |||
$this->ocsVersion = $version; | |||
} | |||
/** | |||
* Since the OCS endpoints default to XML we need to find out the format | |||
* again | |||
@@ -90,22 +102,16 @@ abstract class OCSController extends ApiController { | |||
* @param string $format json or xml | |||
* @param DataResponse $data the data which should be transformed | |||
* @since 8.1.0 | |||
* @return OCSResponse | |||
* @return \OC\AppFramework\OCS\BaseResponse | |||
*/ | |||
private function buildOCSResponse($format, DataResponse $data) { | |||
$params = [ | |||
'statuscode' => 100, | |||
'message' => 'OK', | |||
'data' => $data->getData(), | |||
'itemscount' => '', | |||
'itemsperpage' => '' | |||
]; | |||
if ($this->ocsVersion === 1) { | |||
$response = new \OC\AppFramework\OCS\V1Response($data, $format); | |||
} else { | |||
$response = new \OC\AppFramework\OCS\V2Response($data, $format); | |||
} | |||
return new OCSResponse( | |||
$format, $params['statuscode'], | |||
$params['message'], $params['data'], | |||
$params['itemscount'], $params['itemsperpage'] | |||
); | |||
return $response; | |||
} | |||
} |
@@ -27,6 +27,8 @@ namespace Test\AppFramework\Controller; | |||
use OC\AppFramework\Http\Request; | |||
use OCP\AppFramework\Http\DataResponse; | |||
use OCP\AppFramework\OCSController; | |||
use OCP\IConfig; | |||
use OCP\Security\ISecureRandom; | |||
class ChildOCSController extends OCSController {} | |||
@@ -40,10 +42,10 @@ class OCSControllerTest extends \Test\TestCase { | |||
'HTTP_ORIGIN' => 'test', | |||
], | |||
], | |||
$this->getMockBuilder('\OCP\Security\ISecureRandom') | |||
$this->getMockBuilder(ISecureRandom::class) | |||
->disableOriginalConstructor() | |||
->getMock(), | |||
$this->getMockBuilder('\OCP\IConfig') | |||
$this->getMockBuilder(IConfig::class) | |||
->disableOriginalConstructor() | |||
->getMock() | |||
); | |||
@@ -65,13 +67,15 @@ class OCSControllerTest extends \Test\TestCase { | |||
public function testXML() { | |||
$controller = new ChildOCSController('app', new Request( | |||
[], | |||
$this->getMockBuilder('\OCP\Security\ISecureRandom') | |||
$this->getMockBuilder(ISecureRandom::class) | |||
->disableOriginalConstructor() | |||
->getMock(), | |||
$this->getMockBuilder('\OCP\IConfig') | |||
$this->getMockBuilder(IConfig::class) | |||
->disableOriginalConstructor() | |||
->getMock() | |||
)); | |||
$controller->setOCSVersion(1); | |||
$expected = "<?xml version=\"1.0\"?>\n" . | |||
"<ocs>\n" . | |||
" <meta>\n" . | |||
@@ -95,13 +99,14 @@ class OCSControllerTest extends \Test\TestCase { | |||
public function testJSON() { | |||
$controller = new ChildOCSController('app', new Request( | |||
[], | |||
$this->getMockBuilder('\OCP\Security\ISecureRandom') | |||
$this->getMockBuilder(ISecureRandom::class) | |||
->disableOriginalConstructor() | |||
->getMock(), | |||
$this->getMockBuilder('\OCP\IConfig') | |||
$this->getMockBuilder(IConfig::class) | |||
->disableOriginalConstructor() | |||
->getMock() | |||
)); | |||
$controller->setOCSVersion(1); | |||
$expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' . | |||
'"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}'; | |||
$params = new DataResponse(['test' => 'hi']); | |||
@@ -110,5 +115,51 @@ class OCSControllerTest extends \Test\TestCase { | |||
$this->assertEquals($expected, $out); | |||
} | |||
public function testXMLV2() { | |||
$controller = new ChildOCSController('app', new Request( | |||
[], | |||
$this->getMockBuilder(ISecureRandom::class) | |||
->disableOriginalConstructor() | |||
->getMock(), | |||
$this->getMockBuilder(IConfig::class) | |||
->disableOriginalConstructor() | |||
->getMock() | |||
)); | |||
$controller->setOCSVersion(2); | |||
$expected = "<?xml version=\"1.0\"?>\n" . | |||
"<ocs>\n" . | |||
" <meta>\n" . | |||
" <status>ok</status>\n" . | |||
" <statuscode>200</statuscode>\n" . | |||
" <message>OK</message>\n" . | |||
" </meta>\n" . | |||
" <data>\n" . | |||
" <test>hi</test>\n" . | |||
" </data>\n" . | |||
"</ocs>\n"; | |||
$params = new DataResponse(['test' => 'hi']); | |||
$out = $controller->buildResponse($params, 'xml')->render(); | |||
$this->assertEquals($expected, $out); | |||
} | |||
public function testJSONV2() { | |||
$controller = new ChildOCSController('app', new Request( | |||
[], | |||
$this->getMockBuilder(ISecureRandom::class) | |||
->disableOriginalConstructor() | |||
->getMock(), | |||
$this->getMockBuilder(IConfig::class) | |||
->disableOriginalConstructor() | |||
->getMock() | |||
)); | |||
$controller->setOCSVersion(2); | |||
$expected = '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":{"test":"hi"}}}'; | |||
$params = new DataResponse(['test' => 'hi']); | |||
$out = $controller->buildResponse($params, 'json')->render(); | |||
$this->assertEquals($expected, $out); | |||
} | |||
} |
@@ -21,12 +21,16 @@ | |||
*/ | |||
namespace Test\AppFramework\Middleware; | |||
use OC\AppFramework\OCS\BaseResponse; | |||
use OC\AppFramework\OCS\V1Response; | |||
use OC\AppFramework\OCS\V2Response; | |||
use OCP\AppFramework\Controller; | |||
use OCP\AppFramework\Http; | |||
use OCP\AppFramework\OCS\OCSBadRequestException; | |||
use OCP\AppFramework\OCS\OCSException; | |||
use OCP\AppFramework\OCS\OCSForbiddenException; | |||
use OCP\AppFramework\OCS\OCSNotFoundException; | |||
use OCP\AppFramework\OCSController; | |||
use OCP\IRequest; | |||
use OC\AppFramework\Middleware\OCSMiddleware; | |||
@@ -41,16 +45,15 @@ class OCSMiddlewareTest extends \Test\TestCase { | |||
protected function setUp() { | |||
parent::setUp(); | |||
$this->request = $this->getMockBuilder('OCP\IRequest') | |||
$this->request = $this->getMockBuilder(IRequest::class) | |||
->getMock(); | |||
} | |||
public function dataAfterException() { | |||
$OCSController = $this->getMockBuilder('OCP\AppFramework\OCSController') | |||
$OCSController = $this->getMockBuilder(OCSController::class) | |||
->disableOriginalConstructor() | |||
->getMock(); | |||
$controller = $this->getMockBuilder('OCP\AppFramework\Controller') | |||
$controller = $this->getMockBuilder(Controller::class) | |||
->disableOriginalConstructor() | |||
->getMock(); | |||
@@ -93,25 +96,26 @@ class OCSMiddlewareTest extends \Test\TestCase { | |||
->method('getScriptName') | |||
->willReturn('/ocs/v1.php'); | |||
$OCSMiddleware = new OCSMiddleware($this->request); | |||
$OCSMiddleware->beforeController($controller, 'method'); | |||
try { | |||
$result = $OCSMiddleware->afterException($controller, 'method', $exception); | |||
$this->assertFalse($forward); | |||
$this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result); | |||
$this->assertInstanceOf(V1Response::class, $result); | |||
$this->assertSame($message, $this->invokePrivate($result, 'message')); | |||
$this->assertSame($message, $this->invokePrivate($result, 'statusMessage')); | |||
if ($exception->getCode() === 0) { | |||
$this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $this->invokePrivate($result, 'statuscode')); | |||
$this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus()); | |||
} else { | |||
$this->assertSame($code, $this->invokePrivate($result, 'statuscode')); | |||
$this->assertSame($code, $result->getOCSStatus()); | |||
} | |||
if ($exception instanceof OCSForbiddenException) { | |||
$this->assertSame(Http::STATUS_UNAUTHORIZED, $result->getStatus()); | |||
} else { | |||
$this->assertSame(200, $result->getStatus()); | |||
$this->assertSame(Http::STATUS_OK, $result->getStatus()); | |||
} | |||
} catch (\Exception $e) { | |||
$this->assertTrue($forward); | |||
@@ -133,18 +137,19 @@ class OCSMiddlewareTest extends \Test\TestCase { | |||
->method('getScriptName') | |||
->willReturn('/ocs/v2.php'); | |||
$OCSMiddleware = new OCSMiddleware($this->request); | |||
$OCSMiddleware->beforeController($controller, 'method'); | |||
try { | |||
$result = $OCSMiddleware->afterException($controller, 'method', $exception); | |||
$this->assertFalse($forward); | |||
$this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result); | |||
$this->assertInstanceOf(V2Response::class, $result); | |||
$this->assertSame($message, $this->invokePrivate($result, 'message')); | |||
$this->assertSame($message, $this->invokePrivate($result, 'statusMessage')); | |||
if ($exception->getCode() === 0) { | |||
$this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $this->invokePrivate($result, 'statuscode')); | |||
$this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus()); | |||
} else { | |||
$this->assertSame($code, $this->invokePrivate($result, 'statuscode')); | |||
$this->assertSame($code, $result->getOCSStatus()); | |||
} | |||
$this->assertSame($code, $result->getStatus()); | |||
} catch (\Exception $e) { | |||
@@ -167,18 +172,19 @@ class OCSMiddlewareTest extends \Test\TestCase { | |||
->method('getScriptName') | |||
->willReturn('/mysubfolder/ocs/v2.php'); | |||
$OCSMiddleware = new OCSMiddleware($this->request); | |||
$OCSMiddleware->beforeController($controller, 'method'); | |||
try { | |||
$result = $OCSMiddleware->afterException($controller, 'method', $exception); | |||
$this->assertFalse($forward); | |||
$this->assertInstanceOf('OCP\AppFramework\Http\OCSResponse', $result); | |||
$this->assertInstanceOf(V2Response::class, $result); | |||
$this->assertSame($message, $this->invokePrivate($result, 'message')); | |||
$this->assertSame($message, $this->invokePrivate($result, 'statusMessage')); | |||
if ($exception->getCode() === 0) { | |||
$this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $this->invokePrivate($result, 'statuscode')); | |||
$this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus()); | |||
} else { | |||
$this->assertSame($code, $this->invokePrivate($result, 'statuscode')); | |||
$this->assertSame($code, $result->getOCSStatus()); | |||
} | |||
$this->assertSame($code, $result->getStatus()); | |||
} catch (\Exception $e) { | |||
@@ -188,10 +194,10 @@ class OCSMiddlewareTest extends \Test\TestCase { | |||
} | |||
public function dataAfterController() { | |||
$OCSController = $this->getMockBuilder('OCP\AppFramework\OCSController') | |||
$OCSController = $this->getMockBuilder(OCSController::class) | |||
->disableOriginalConstructor() | |||
->getMock(); | |||
$controller = $this->getMockBuilder('OCP\AppFramework\Controller') | |||
$controller = $this->getMockBuilder(Controller::class) | |||
->disableOriginalConstructor() | |||
->getMock(); | |||
@@ -225,10 +231,10 @@ class OCSMiddlewareTest extends \Test\TestCase { | |||
if ($converted === false) { | |||
$this->assertSame($response, $newResponse); | |||
} else { | |||
$this->assertInstanceOf('\OCP\AppFramework\Http\OCSResponse', $newResponse); | |||
$this->assertInstanceOf(BaseResponse::class, $newResponse); | |||
/** @var Http\OCSResponse $newResponse */ | |||
$this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'message')); | |||
$this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $this->invokePrivate($newResponse, 'statuscode')); | |||
$this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'statusMessage')); | |||
$this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $newResponse->getOCSStatus()); | |||
$this->assertSame(Http::STATUS_UNAUTHORIZED, $newResponse->getStatus()); | |||
} | |||
} |