Merge pull request #1275 from nextcloud/OCS_forward_headers_cleanup

Internal OCS Controller cleanup
This commit is contained in:
Morris Jobke 2016-09-06 14:46:05 +02:00 committed by GitHub
commit b6bdf81d55
9 changed files with 396 additions and 79 deletions

View File

@ -262,6 +262,9 @@ return array(
'OC\\AppFramework\\Middleware\\Security\\Exceptions\\StrictCookieMissingException' => $baseDir . '/lib/private/AppFramework/Middleware/Security/Exceptions/StrictCookieMissingException.php', 'OC\\AppFramework\\Middleware\\Security\\Exceptions\\StrictCookieMissingException' => $baseDir . '/lib/private/AppFramework/Middleware/Security/Exceptions/StrictCookieMissingException.php',
'OC\\AppFramework\\Middleware\\Security\\SecurityMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php', 'OC\\AppFramework\\Middleware\\Security\\SecurityMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php',
'OC\\AppFramework\\Middleware\\SessionMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/SessionMiddleware.php', 'OC\\AppFramework\\Middleware\\SessionMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/SessionMiddleware.php',
'OC\\AppFramework\\OCS\\BaseResponse' => $baseDir . '/lib/private/AppFramework/OCS/BaseResponse.php',
'OC\\AppFramework\\OCS\\V1Response' => $baseDir . '/lib/private/AppFramework/OCS/V1Response.php',
'OC\\AppFramework\\OCS\\V2Response' => $baseDir . '/lib/private/AppFramework/OCS/V2Response.php',
'OC\\AppFramework\\Routing\\RouteActionHandler' => $baseDir . '/lib/private/AppFramework/Routing/RouteActionHandler.php', 'OC\\AppFramework\\Routing\\RouteActionHandler' => $baseDir . '/lib/private/AppFramework/Routing/RouteActionHandler.php',
'OC\\AppFramework\\Routing\\RouteConfig' => $baseDir . '/lib/private/AppFramework/Routing/RouteConfig.php', 'OC\\AppFramework\\Routing\\RouteConfig' => $baseDir . '/lib/private/AppFramework/Routing/RouteConfig.php',
'OC\\AppFramework\\Utility\\ControllerMethodReflector' => $baseDir . '/lib/private/AppFramework/Utility/ControllerMethodReflector.php', 'OC\\AppFramework\\Utility\\ControllerMethodReflector' => $baseDir . '/lib/private/AppFramework/Utility/ControllerMethodReflector.php',

View File

@ -292,6 +292,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\AppFramework\\Middleware\\Security\\Exceptions\\StrictCookieMissingException' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/Security/Exceptions/StrictCookieMissingException.php', 'OC\\AppFramework\\Middleware\\Security\\Exceptions\\StrictCookieMissingException' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/Security/Exceptions/StrictCookieMissingException.php',
'OC\\AppFramework\\Middleware\\Security\\SecurityMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php', 'OC\\AppFramework\\Middleware\\Security\\SecurityMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/Security/SecurityMiddleware.php',
'OC\\AppFramework\\Middleware\\SessionMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/SessionMiddleware.php', 'OC\\AppFramework\\Middleware\\SessionMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/SessionMiddleware.php',
'OC\\AppFramework\\OCS\\BaseResponse' => __DIR__ . '/../../..' . '/lib/private/AppFramework/OCS/BaseResponse.php',
'OC\\AppFramework\\OCS\\V1Response' => __DIR__ . '/../../..' . '/lib/private/AppFramework/OCS/V1Response.php',
'OC\\AppFramework\\OCS\\V2Response' => __DIR__ . '/../../..' . '/lib/private/AppFramework/OCS/V2Response.php',
'OC\\AppFramework\\Routing\\RouteActionHandler' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Routing/RouteActionHandler.php', 'OC\\AppFramework\\Routing\\RouteActionHandler' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Routing/RouteActionHandler.php',
'OC\\AppFramework\\Routing\\RouteConfig' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Routing/RouteConfig.php', 'OC\\AppFramework\\Routing\\RouteConfig' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Routing/RouteConfig.php',
'OC\\AppFramework\\Utility\\ControllerMethodReflector' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/ControllerMethodReflector.php', 'OC\\AppFramework\\Utility\\ControllerMethodReflector' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Utility/ControllerMethodReflector.php',

View File

@ -23,14 +23,15 @@
namespace OC\AppFramework\Middleware; namespace OC\AppFramework\Middleware;
use OC\AppFramework\Http; use OC\AppFramework\Http;
use OC\AppFramework\OCS\BaseResponse;
use OC\AppFramework\OCS\V1Response;
use OC\AppFramework\OCS\V2Response;
use OCP\API; use OCP\API;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\OCSResponse;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\IRequest; use OCP\IRequest;
use OCP\AppFramework\Middleware; use OCP\AppFramework\Middleware;
@ -40,6 +41,9 @@ class OCSMiddleware extends Middleware {
/** @var IRequest */ /** @var IRequest */
private $request; private $request;
/** @var int */
private $ocsVersion;
/** /**
* @param IRequest $request * @param IRequest $request
*/ */
@ -47,50 +51,36 @@ class OCSMiddleware extends Middleware {
$this->request = $request; $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 \OCP\AppFramework\Controller $controller
* @param string $methodName * @param string $methodName
* @param \Exception $exception * @param \Exception $exception
* @throws \Exception * @throws \Exception
* @return OCSResponse * @return BaseResponse
*/ */
public function afterException($controller, $methodName, \Exception $exception) { public function afterException($controller, $methodName, \Exception $exception) {
if ($controller instanceof OCSController && $exception instanceof OCSException) { if ($controller instanceof OCSController && $exception instanceof OCSException) {
$format = $this->getFormat($controller);
$code = $exception->getCode(); $code = $exception->getCode();
if ($code === 0) { if ($code === 0) {
$code = API::RESPOND_UNKNOWN_ERROR; $code = API::RESPOND_UNKNOWN_ERROR;
} }
// Build the response return $this->buildNewResponse($controller, $code, $exception->getMessage());
$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;
} }
throw $exception; throw $exception;
@ -107,24 +97,43 @@ class OCSMiddleware extends Middleware {
* If a different middleware has detected that a request unauthorized or forbidden * 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. * 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 || if ($response->getStatus() === Http::STATUS_UNAUTHORIZED ||
$response->getStatus() === Http::STATUS_FORBIDDEN) { $response->getStatus() === Http::STATUS_FORBIDDEN) {
$format = $this->getFormat($controller);
$message = ''; $message = '';
if ($response instanceof JSONResponse) { if ($response instanceof JSONResponse) {
/** @var DataResponse $response */ /** @var DataResponse $response */
$message = $response->getData()['message']; $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; 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 * @param \OCP\AppFramework\Controller $controller
* @return string * @return string

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -42,6 +42,9 @@ use OCP\IRequest;
*/ */
abstract class OCSController extends ApiController { abstract class OCSController extends ApiController {
/** @var int */
private $ocsVersion;
/** /**
* constructor of the controller * constructor of the controller
* @param string $appName the name of the app * @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 * Since the OCS endpoints default to XML we need to find out the format
* again * again
@ -90,22 +102,16 @@ abstract class OCSController extends ApiController {
* @param string $format json or xml * @param string $format json or xml
* @param DataResponse $data the data which should be transformed * @param DataResponse $data the data which should be transformed
* @since 8.1.0 * @since 8.1.0
* @return OCSResponse * @return \OC\AppFramework\OCS\BaseResponse
*/ */
private function buildOCSResponse($format, DataResponse $data) { private function buildOCSResponse($format, DataResponse $data) {
$params = [ if ($this->ocsVersion === 1) {
'statuscode' => 100, $response = new \OC\AppFramework\OCS\V1Response($data, $format);
'message' => 'OK', } else {
'data' => $data->getData(), $response = new \OC\AppFramework\OCS\V2Response($data, $format);
'itemscount' => '', }
'itemsperpage' => ''
];
return new OCSResponse( return $response;
$format, $params['statuscode'],
$params['message'], $params['data'],
$params['itemscount'], $params['itemsperpage']
);
} }
} }

View File

@ -27,6 +27,8 @@ namespace Test\AppFramework\Controller;
use OC\AppFramework\Http\Request; use OC\AppFramework\Http\Request;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
class ChildOCSController extends OCSController {} class ChildOCSController extends OCSController {}
@ -40,10 +42,10 @@ class OCSControllerTest extends \Test\TestCase {
'HTTP_ORIGIN' => 'test', 'HTTP_ORIGIN' => 'test',
], ],
], ],
$this->getMockBuilder('\OCP\Security\ISecureRandom') $this->getMockBuilder(ISecureRandom::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(), ->getMock(),
$this->getMockBuilder('\OCP\IConfig') $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
); );
@ -65,13 +67,15 @@ class OCSControllerTest extends \Test\TestCase {
public function testXML() { public function testXML() {
$controller = new ChildOCSController('app', new Request( $controller = new ChildOCSController('app', new Request(
[], [],
$this->getMockBuilder('\OCP\Security\ISecureRandom') $this->getMockBuilder(ISecureRandom::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(), ->getMock(),
$this->getMockBuilder('\OCP\IConfig') $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
)); ));
$controller->setOCSVersion(1);
$expected = "<?xml version=\"1.0\"?>\n" . $expected = "<?xml version=\"1.0\"?>\n" .
"<ocs>\n" . "<ocs>\n" .
" <meta>\n" . " <meta>\n" .
@ -95,13 +99,14 @@ class OCSControllerTest extends \Test\TestCase {
public function testJSON() { public function testJSON() {
$controller = new ChildOCSController('app', new Request( $controller = new ChildOCSController('app', new Request(
[], [],
$this->getMockBuilder('\OCP\Security\ISecureRandom') $this->getMockBuilder(ISecureRandom::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(), ->getMock(),
$this->getMockBuilder('\OCP\IConfig') $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
)); ));
$controller->setOCSVersion(1);
$expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' . $expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
'"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}'; '"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
$params = new DataResponse(['test' => 'hi']); $params = new DataResponse(['test' => 'hi']);
@ -110,5 +115,51 @@ class OCSControllerTest extends \Test\TestCase {
$this->assertEquals($expected, $out); $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);
}
} }

View File

@ -21,12 +21,16 @@
*/ */
namespace Test\AppFramework\Middleware; 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\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSException; use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\IRequest; use OCP\IRequest;
use OC\AppFramework\Middleware\OCSMiddleware; use OC\AppFramework\Middleware\OCSMiddleware;
@ -41,16 +45,15 @@ class OCSMiddlewareTest extends \Test\TestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->request = $this->getMockBuilder('OCP\IRequest') $this->request = $this->getMockBuilder(IRequest::class)
->getMock(); ->getMock();
} }
public function dataAfterException() { public function dataAfterException() {
$OCSController = $this->getMockBuilder('OCP\AppFramework\OCSController') $OCSController = $this->getMockBuilder(OCSController::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$controller = $this->getMockBuilder('OCP\AppFramework\Controller') $controller = $this->getMockBuilder(Controller::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -93,25 +96,26 @@ class OCSMiddlewareTest extends \Test\TestCase {
->method('getScriptName') ->method('getScriptName')
->willReturn('/ocs/v1.php'); ->willReturn('/ocs/v1.php');
$OCSMiddleware = new OCSMiddleware($this->request); $OCSMiddleware = new OCSMiddleware($this->request);
$OCSMiddleware->beforeController($controller, 'method');
try { try {
$result = $OCSMiddleware->afterException($controller, 'method', $exception); $result = $OCSMiddleware->afterException($controller, 'method', $exception);
$this->assertFalse($forward); $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) { 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 { } else {
$this->assertSame($code, $this->invokePrivate($result, 'statuscode')); $this->assertSame($code, $result->getOCSStatus());
} }
if ($exception instanceof OCSForbiddenException) { if ($exception instanceof OCSForbiddenException) {
$this->assertSame(Http::STATUS_UNAUTHORIZED, $result->getStatus()); $this->assertSame(Http::STATUS_UNAUTHORIZED, $result->getStatus());
} else { } else {
$this->assertSame(200, $result->getStatus()); $this->assertSame(Http::STATUS_OK, $result->getStatus());
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$this->assertTrue($forward); $this->assertTrue($forward);
@ -133,18 +137,19 @@ class OCSMiddlewareTest extends \Test\TestCase {
->method('getScriptName') ->method('getScriptName')
->willReturn('/ocs/v2.php'); ->willReturn('/ocs/v2.php');
$OCSMiddleware = new OCSMiddleware($this->request); $OCSMiddleware = new OCSMiddleware($this->request);
$OCSMiddleware->beforeController($controller, 'method');
try { try {
$result = $OCSMiddleware->afterException($controller, 'method', $exception); $result = $OCSMiddleware->afterException($controller, 'method', $exception);
$this->assertFalse($forward); $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) { 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 { } else {
$this->assertSame($code, $this->invokePrivate($result, 'statuscode')); $this->assertSame($code, $result->getOCSStatus());
} }
$this->assertSame($code, $result->getStatus()); $this->assertSame($code, $result->getStatus());
} catch (\Exception $e) { } catch (\Exception $e) {
@ -167,18 +172,19 @@ class OCSMiddlewareTest extends \Test\TestCase {
->method('getScriptName') ->method('getScriptName')
->willReturn('/mysubfolder/ocs/v2.php'); ->willReturn('/mysubfolder/ocs/v2.php');
$OCSMiddleware = new OCSMiddleware($this->request); $OCSMiddleware = new OCSMiddleware($this->request);
$OCSMiddleware->beforeController($controller, 'method');
try { try {
$result = $OCSMiddleware->afterException($controller, 'method', $exception); $result = $OCSMiddleware->afterException($controller, 'method', $exception);
$this->assertFalse($forward); $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) { 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 { } else {
$this->assertSame($code, $this->invokePrivate($result, 'statuscode')); $this->assertSame($code, $result->getOCSStatus());
} }
$this->assertSame($code, $result->getStatus()); $this->assertSame($code, $result->getStatus());
} catch (\Exception $e) { } catch (\Exception $e) {
@ -188,10 +194,10 @@ class OCSMiddlewareTest extends \Test\TestCase {
} }
public function dataAfterController() { public function dataAfterController() {
$OCSController = $this->getMockBuilder('OCP\AppFramework\OCSController') $OCSController = $this->getMockBuilder(OCSController::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$controller = $this->getMockBuilder('OCP\AppFramework\Controller') $controller = $this->getMockBuilder(Controller::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -225,10 +231,10 @@ class OCSMiddlewareTest extends \Test\TestCase {
if ($converted === false) { if ($converted === false) {
$this->assertSame($response, $newResponse); $this->assertSame($response, $newResponse);
} else { } else {
$this->assertInstanceOf('\OCP\AppFramework\Http\OCSResponse', $newResponse); $this->assertInstanceOf(BaseResponse::class, $newResponse);
/** @var Http\OCSResponse $newResponse */ /** @var Http\OCSResponse $newResponse */
$this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'message')); $this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'statusMessage'));
$this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $this->invokePrivate($newResponse, 'statuscode')); $this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $newResponse->getOCSStatus());
$this->assertSame(Http::STATUS_UNAUTHORIZED, $newResponse->getStatus()); $this->assertSame(Http::STATUS_UNAUTHORIZED, $newResponse->getStatus());
} }
} }