2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt
|
2014-05-07 00:25:05 +04:00
|
|
|
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
|
2013-08-17 13:16:48 +04: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
namespace OCP\AppFramework;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
use OC\AppFramework\Http\Request;
|
2013-08-21 02:41:20 +04:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2014-05-06 18:29:19 +04:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-10-28 18:34:04 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
|
2014-05-06 18:29:19 +04:00
|
|
|
class ChildController extends Controller {
|
2014-06-11 02:54:25 +04:00
|
|
|
|
|
|
|
public function __construct($appName, $request) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->registerResponder('tom', function ($respone) {
|
|
|
|
return 'hi';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-06 18:29:19 +04:00
|
|
|
public function custom($in) {
|
2014-05-06 22:25:41 +04:00
|
|
|
$this->registerResponder('json', function ($response) {
|
2014-05-06 18:29:19 +04:00
|
|
|
return new JSONResponse(array(strlen($response)));
|
|
|
|
});
|
|
|
|
|
|
|
|
return $in;
|
|
|
|
}
|
2014-10-28 18:34:04 +03:00
|
|
|
|
|
|
|
public function customDataResponse($in) {
|
|
|
|
$response = new DataResponse($in, 300);
|
|
|
|
$response->addHeader('test', 'something');
|
|
|
|
return $response;
|
|
|
|
}
|
2014-05-06 18:29:19 +04:00
|
|
|
};
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
class ControllerTest extends \Test\TestCase {
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
private $controller;
|
2013-10-07 13:25:50 +04:00
|
|
|
private $app;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
protected function setUp(){
|
2014-11-11 01:30:38 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
$request = new Request(
|
2015-02-09 13:41:48 +03:00
|
|
|
[
|
|
|
|
'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 13:16:48 +04:00
|
|
|
'method' => 'hi',
|
2015-02-09 13:41:48 +03:00
|
|
|
],
|
2015-02-10 15:02:48 +03:00
|
|
|
$this->getMock('\OCP\Security\ISecureRandom'),
|
|
|
|
$this->getMock('\OCP\IConfig')
|
2013-08-17 13:16:48 +04:00
|
|
|
);
|
|
|
|
|
2013-10-07 13:25:50 +04:00
|
|
|
$this->app = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
|
2013-08-17 13:16:48 +04:00
|
|
|
array('getAppName'), array('test'));
|
2013-10-07 13:25:50 +04:00
|
|
|
$this->app->expects($this->any())
|
2013-08-17 13:16:48 +04:00
|
|
|
->method('getAppName')
|
|
|
|
->will($this->returnValue('apptemplate_advanced'));
|
|
|
|
|
2013-10-07 13:25:50 +04:00
|
|
|
$this->controller = new ChildController($this->app, $request);
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testParamsGet(){
|
|
|
|
$this->assertEquals('Johnny Weissmüller', $this->controller->params('name', 'Tarzan'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testParamsGetDefault(){
|
|
|
|
$this->assertEquals('Tarzan', $this->controller->params('Ape Man', 'Tarzan'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testParamsFile(){
|
|
|
|
$this->assertEquals('filevalue', $this->controller->params('file', 'filevalue'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGetUploadedFile(){
|
|
|
|
$this->assertEquals('filevalue', $this->controller->getUploadedFile('file'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testGetUploadedFileDefault(){
|
|
|
|
$this->assertEquals('default', $this->controller->params('files', 'default'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGetParams(){
|
|
|
|
$params = array(
|
|
|
|
'name' => 'Johnny Weissmüller',
|
|
|
|
'nickname' => 'Janey',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($params, $this->controller->getParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testRender(){
|
|
|
|
$this->assertTrue($this->controller->render('') instanceof TemplateResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testSetParams(){
|
|
|
|
$params = array('john' => 'foo');
|
|
|
|
$response = $this->controller->render('home', $params);
|
|
|
|
|
|
|
|
$this->assertEquals($params, $response->getParams());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testRenderHeaders(){
|
|
|
|
$headers = array('one', 'two');
|
|
|
|
$response = $this->controller->render('', array(), '', $headers);
|
|
|
|
|
|
|
|
$this->assertTrue(in_array($headers[0], $response->getHeaders()));
|
|
|
|
$this->assertTrue(in_array($headers[1], $response->getHeaders()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGetRequestMethod(){
|
|
|
|
$this->assertEquals('hi', $this->controller->method());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testGetEnvVariable(){
|
|
|
|
$this->assertEquals('daheim', $this->controller->env('PATH'));
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
|
2014-05-06 18:29:19 +04:00
|
|
|
/**
|
|
|
|
* @expectedException \DomainException
|
|
|
|
*/
|
|
|
|
public function testFormatResonseInvalidFormat() {
|
2014-05-06 22:25:41 +04:00
|
|
|
$this->controller->buildResponse(null, 'test');
|
2014-05-06 18:29:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testFormat() {
|
2014-05-06 22:25:41 +04:00
|
|
|
$response = $this->controller->buildResponse(array('hi'), 'json');
|
2014-05-06 18:29:19 +04:00
|
|
|
|
|
|
|
$this->assertEquals(array('hi'), $response->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-28 18:34:04 +03:00
|
|
|
public function testFormatDataResponseJSON() {
|
2015-02-09 18:30:01 +03:00
|
|
|
$expectedHeaders = [
|
2014-10-28 18:34:04 +03:00
|
|
|
'test' => 'something',
|
2014-12-04 17:54:32 +03:00
|
|
|
'Cache-Control' => 'no-cache, must-revalidate',
|
2015-02-09 18:30:01 +03:00
|
|
|
'Content-Type' => 'application/json; charset=utf-8',
|
2015-08-10 13:42:42 +03:00
|
|
|
'Content-Security-Policy' => "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data:;font-src 'self';connect-src 'self';media-src 'self'",
|
2015-02-09 18:30:01 +03:00
|
|
|
];
|
2014-10-28 18:34:04 +03:00
|
|
|
|
|
|
|
$response = $this->controller->customDataResponse(array('hi'));
|
|
|
|
$response = $this->controller->buildResponse($response, 'json');
|
|
|
|
|
|
|
|
$this->assertEquals(array('hi'), $response->getData());
|
|
|
|
$this->assertEquals(300, $response->getStatus());
|
|
|
|
$this->assertEquals($expectedHeaders, $response->getHeaders());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-06 18:29:19 +04:00
|
|
|
public function testCustomFormatter() {
|
|
|
|
$response = $this->controller->custom('hi');
|
2014-05-06 22:25:41 +04:00
|
|
|
$response = $this->controller->buildResponse($response, 'json');
|
2014-05-06 18:29:19 +04:00
|
|
|
|
2014-05-29 21:14:47 +04:00
|
|
|
$this->assertEquals(array(2), $response->getData());
|
2014-05-06 18:29:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-11 02:54:25 +04:00
|
|
|
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-05-06 18:29:19 +04:00
|
|
|
|
2014-06-11 03:20:09 +04:00
|
|
|
public function testResponderAcceptHeaderParsedUpperCase() {
|
|
|
|
$responder = $this->controller->getResponderByHTTPHeader(
|
|
|
|
'*/*, apPlication/ToM, application/json'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('tom', $responder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|