nextcloud/tests/lib/AppFramework/Controller/ControllerTest.php

163 lines
4.5 KiB
PHP
Raw Normal View History

2013-08-17 11:16:48 +02:00
<?php
/**
* ownCloud - App Framework
*
* @author Bernhard Posselt
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
2013-08-17 11:16:48 +02:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*
*/
2016-05-18 18:40:34 +02:00
namespace Test\AppFramework\Controller;
2013-08-17 11:16:48 +02:00
use OC\AppFramework\DependencyInjection\DIContainer;
2013-08-17 11:16:48 +02:00
use OC\AppFramework\Http\Request;
2016-05-18 18:40:34 +02:00
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IRequestId;
2013-08-17 11:16:48 +02:00
class ChildController extends Controller {
public function __construct($appName, $request) {
parent::__construct($appName, $request);
$this->registerResponder('tom', function ($respone) {
return 'hi';
});
}
public function custom($in) {
$this->registerResponder('json', function ($response) {
return new JSONResponse([strlen($response)]);
});
return $in;
}
public function customDataResponse($in) {
$response = new DataResponse($in, 300);
$response->addHeader('test', 'something');
return $response;
}
};
2013-08-17 11:16:48 +02:00
class ControllerTest extends \Test\TestCase {
2013-08-17 11:16:48 +02:00
/**
* @var Controller
*/
private $controller;
2013-10-07 11:25:50 +02:00
private $app;
private $request;
2013-08-17 11:16:48 +02:00
protected function setUp(): void {
parent::setUp();
2013-08-17 11:16:48 +02:00
$request = new Request(
[
'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
'urlParams' => ['name' => 'Johnny Weissmüller'],
'files' => ['file' => 'filevalue'],
'env' => ['PATH' => 'daheim'],
'session' => ['sezession' => 'kein'],
2013-08-17 11:16:48 +02:00
'method' => 'hi',
],
$this->createMock(IRequestId::class),
$this->createMock(IConfig::class)
2013-08-17 11:16:48 +02:00
);
$this->app = $this->getMockBuilder(DIContainer::class)
->setMethods(['getAppName'])
->setConstructorArgs(['test'])
->getMock();
2013-10-07 11:25:50 +02:00
$this->app->expects($this->any())
2013-08-17 11:16:48 +02:00
->method('getAppName')
->willReturn('apptemplate_advanced');
2013-08-17 11:16:48 +02:00
2013-10-07 11:25:50 +02:00
$this->controller = new ChildController($this->app, $request);
$this->overwriteService(IRequest::class, $request);
$this->request = $request;
2013-08-17 11:16:48 +02:00
}
public function testFormatResonseInvalidFormat() {
$this->expectException(\DomainException::class);
$this->controller->buildResponse(null, 'test');
}
public function testFormat() {
$response = $this->controller->buildResponse(['hi'], 'json');
$this->assertEquals(['hi'], $response->getData());
}
public function testFormatDataResponseJSON() {
$expectedHeaders = [
'test' => 'something',
'Cache-Control' => 'no-cache, no-store, must-revalidate',
'Content-Type' => 'application/json; charset=utf-8',
'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'",
'Feature-Policy' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'",
'X-Request-Id' => $this->request->getId(),
'X-Robots-Tag' => 'noindex, nofollow',
];
$response = $this->controller->customDataResponse(['hi']);
$response = $this->controller->buildResponse($response, 'json');
$this->assertEquals(['hi'], $response->getData());
$this->assertEquals(300, $response->getStatus());
$this->assertEquals($expectedHeaders, $response->getHeaders());
}
public function testCustomFormatter() {
$response = $this->controller->custom('hi');
$response = $this->controller->buildResponse($response, 'json');
$this->assertEquals([2], $response->getData());
}
public function testDefaultResponderToJSON() {
$responder = $this->controller->getResponderByHTTPHeader('*/*');
$this->assertEquals('json', $responder);
}
public function testResponderAcceptHeaderParsed() {
$responder = $this->controller->getResponderByHTTPHeader(
'*/*, application/tom, application/json'
);
$this->assertEquals('tom', $responder);
}
2014-06-11 01:20:09 +02:00
public function testResponderAcceptHeaderParsedUpperCase() {
$responder = $this->controller->getResponderByHTTPHeader(
'*/*, apPlication/ToM, application/json'
);
$this->assertEquals('tom', $responder);
}
2013-08-17 11:16:48 +02:00
}