diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-10-28 16:34:04 +0100 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-10-29 09:43:47 +0100 |
commit | 0696099bad56727d96c60f6221fe02dc7c71f511 (patch) | |
tree | 5c4784f13481c73950784eca881cc410659c5450 /tests | |
parent | c93ddf77b9e75b9e38764ad4360d82600eae3096 (diff) | |
download | nextcloud-server-0696099bad56727d96c60f6221fe02dc7c71f511.tar.gz nextcloud-server-0696099bad56727d96c60f6221fe02dc7c71f511.zip |
add dataresponse
fix docstrings
adjust copyright date
another copyright date update
another header update
implement third headers argument, fix indention, fix docstrings
fix docstrings
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/appframework/controller/ControllerTest.php | 22 | ||||
-rw-r--r-- | tests/lib/appframework/http/DataResponseTest.php | 87 | ||||
-rw-r--r-- | tests/lib/appframework/http/DispatcherTest.php | 74 | ||||
-rw-r--r-- | tests/lib/appframework/http/ResponseTest.php | 26 |
4 files changed, 185 insertions, 24 deletions
diff --git a/tests/lib/appframework/controller/ControllerTest.php b/tests/lib/appframework/controller/ControllerTest.php index e97ec548939..0de94ff5b70 100644 --- a/tests/lib/appframework/controller/ControllerTest.php +++ b/tests/lib/appframework/controller/ControllerTest.php @@ -27,6 +27,7 @@ namespace OCP\AppFramework; use OC\AppFramework\Http\Request; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\DataResponse; class ChildController extends Controller { @@ -45,6 +46,12 @@ class ChildController extends Controller { return $in; } + + public function customDataResponse($in) { + $response = new DataResponse($in, 300); + $response->addHeader('test', 'something'); + return $response; + } }; class ControllerTest extends \PHPUnit_Framework_TestCase { @@ -161,6 +168,21 @@ class ControllerTest extends \PHPUnit_Framework_TestCase { } + public function testFormatDataResponseJSON() { + $expectedHeaders = array( + 'test' => 'something', + 'Cache-Control' => 'no-cache, must-revalidate' + ); + + $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()); + } + + public function testCustomFormatter() { $response = $this->controller->custom('hi'); $response = $this->controller->buildResponse($response, 'json'); diff --git a/tests/lib/appframework/http/DataResponseTest.php b/tests/lib/appframework/http/DataResponseTest.php new file mode 100644 index 00000000000..961327c978c --- /dev/null +++ b/tests/lib/appframework/http/DataResponseTest.php @@ -0,0 +1,87 @@ +<?php + +/** + * ownCloud - App Framework + * + * @author Bernhard Posselt + * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com> + * + * 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/>. + * + */ + + +namespace OC\AppFramework\Http; + + +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http; + + +class DataResponseTest extends \PHPUnit_Framework_TestCase { + + /** + * @var DataResponse + */ + private $response; + + protected function setUp() { + $this->response = new DataResponse(); + } + + + public function testSetData() { + $params = array('hi', 'yo'); + $this->response->setData($params); + + $this->assertEquals(array('hi', 'yo'), $this->response->getData()); + } + + + public function testConstructorAllowsToSetData() { + $data = array('hi'); + $code = 300; + $response = new DataResponse($data, $code); + + $this->assertEquals($data, $response->getData()); + $this->assertEquals($code, $response->getStatus()); + } + + + public function testConstructorAllowsToSetHeaders() { + $data = array('hi'); + $code = 300; + $headers = array('test' => 'something'); + $response = new DataResponse($data, $code, $headers); + + $expectedHeaders = array('Cache-Control' => 'no-cache, must-revalidate'); + $expectedHeaders = array_merge($expectedHeaders, $headers); + + $this->assertEquals($data, $response->getData()); + $this->assertEquals($code, $response->getStatus()); + $this->assertEquals($expectedHeaders, $response->getHeaders()); + } + + + public function testChainability() { + $params = array('hi', 'yo'); + $this->response->setData($params) + ->setStatus(Http::STATUS_NOT_FOUND); + + $this->assertEquals(Http::STATUS_NOT_FOUND, $this->response->getStatus()); + $this->assertEquals(array('hi', 'yo'), $this->response->getData()); + } + + +} diff --git a/tests/lib/appframework/http/DispatcherTest.php b/tests/lib/appframework/http/DispatcherTest.php index 9d5ec09a293..f082ddc8b3a 100644 --- a/tests/lib/appframework/http/DispatcherTest.php +++ b/tests/lib/appframework/http/DispatcherTest.php @@ -28,6 +28,7 @@ use OC\AppFramework\Middleware\MiddlewareDispatcher; use OC\AppFramework\Utility\ControllerMethodReflector; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Controller; @@ -46,6 +47,18 @@ class TestController extends Controller { }); return array($int, $bool, $test, $test2); } + + + /** + * @param int $int + * @param bool $bool + */ + public function execDataResponse($int, $bool, $test=4, $test2=1) { + return new DataResponse(array( + 'text' => array($int, $bool, $test, $test2) + )); + } + } @@ -84,7 +97,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $this->controller = $this->getMock( '\OCP\AppFramework\Controller', array($this->controllerMethod), array($app, $request)); - + $this->request = $this->getMockBuilder( '\OC\AppFramework\Http\Request') ->disableOriginalConstructor() @@ -96,7 +109,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $this->http, $this->middlewareDispatcher, $this->reflector, $this->request ); - + $this->response = $this->getMockBuilder( '\OCP\AppFramework\Http\Response') ->disableOriginalConstructor() @@ -111,7 +124,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { * @param string $out * @param string $httpHeaders */ - private function setMiddlewareExpectations($out=null, + private function setMiddlewareExpectations($out=null, $httpHeaders=null, $responseHeaders=array(), $ex=false, $catchEx=true) { @@ -119,20 +132,20 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $exception = new \Exception(); $this->middlewareDispatcher->expects($this->once()) ->method('beforeController') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod)) ->will($this->throwException($exception)); if($catchEx) { $this->middlewareDispatcher->expects($this->once()) ->method('afterException') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod), $this->equalTo($exception)) ->will($this->returnValue($this->response)); } else { $this->middlewareDispatcher->expects($this->once()) ->method('afterException') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod), $this->equalTo($exception)) ->will($this->returnValue(null)); @@ -141,7 +154,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { } else { $this->middlewareDispatcher->expects($this->once()) ->method('beforeController') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod)); $this->controller->expects($this->once()) ->method($this->controllerMethod) @@ -165,38 +178,38 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { ->will($this->returnValue($responseHeaders)); $this->http->expects($this->once()) ->method('getStatusHeader') - ->with($this->equalTo(Http::STATUS_OK), + ->with($this->equalTo(Http::STATUS_OK), $this->equalTo($this->lastModified), $this->equalTo($this->etag)) ->will($this->returnValue($httpHeaders)); - + $this->middlewareDispatcher->expects($this->once()) ->method('afterController') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod), $this->equalTo($this->response)) ->will($this->returnValue($this->response)); $this->middlewareDispatcher->expects($this->once()) ->method('afterController') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod), $this->equalTo($this->response)) ->will($this->returnValue($this->response)); $this->middlewareDispatcher->expects($this->once()) ->method('beforeOutput') - ->with($this->equalTo($this->controller), + ->with($this->equalTo($this->controller), $this->equalTo($this->controllerMethod), $this->equalTo($out)) - ->will($this->returnValue($out)); + ->will($this->returnValue($out)); } public function testDispatcherReturnsArrayWith2Entries() { $this->setMiddlewareExpectations(); - $response = $this->dispatcher->dispatch($this->controller, + $response = $this->dispatcher->dispatch($this->controller, $this->controllerMethod); $this->assertNull($response[0]); $this->assertEquals(array(), $response[1]); @@ -210,7 +223,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $responseHeaders = array('hell' => 'yeah'); $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders); - $response = $this->dispatcher->dispatch($this->controller, + $response = $this->dispatcher->dispatch($this->controller, $this->controllerMethod); $this->assertEquals($httpHeaders, $response[0]); @@ -227,9 +240,9 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $out = 'yo'; $httpHeaders = 'Http'; $responseHeaders = array('hell' => 'yeah'); - $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true); + $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true); - $response = $this->dispatcher->dispatch($this->controller, + $response = $this->dispatcher->dispatch($this->controller, $this->controllerMethod); $this->assertEquals($httpHeaders, $response[0]); @@ -249,7 +262,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true, false); $this->setExpectedException('\Exception'); - $response = $this->dispatcher->dispatch($this->controller, + $response = $this->dispatcher->dispatch($this->controller, $this->controllerMethod); } @@ -342,6 +355,31 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase { } + public function testResponseTransformsDataResponse() { + $this->request = new Request(array( + 'post' => array( + 'int' => '3', + 'bool' => 'false' + ), + 'urlParams' => array( + 'format' => 'json' + ), + 'method' => 'GET' + )); + $this->dispatcher = new Dispatcher( + $this->http, $this->middlewareDispatcher, $this->reflector, + $this->request + ); + $controller = new TestController('app', $this->request); + + // reflector is supposed to be called once + $this->dispatcherPassthrough(); + $response = $this->dispatcher->dispatch($controller, 'execDataResponse'); + + $this->assertEquals('{"text":[3,false,4,1]}', $response[2]); + } + + public function testResponseTransformedByAcceptHeader() { $this->request = new Request(array( 'post' => array( diff --git a/tests/lib/appframework/http/ResponseTest.php b/tests/lib/appframework/http/ResponseTest.php index e83fe9e2d84..b1dddd9ebc7 100644 --- a/tests/lib/appframework/http/ResponseTest.php +++ b/tests/lib/appframework/http/ResponseTest.php @@ -48,10 +48,24 @@ class ResponseTest extends \PHPUnit_Framework_TestCase { } + function testSetHeaders(){ + $expected = array( + 'Last-Modified' => 1, + 'ETag' => 3, + 'Something-Else' => 'hi' + ); + + $this->childResponse->setHeaders($expected); + $headers = $this->childResponse->getHeaders(); + + $this->assertEquals($expected, $headers); + } + + public function testAddHeaderValueNullDeletesIt(){ $this->childResponse->addHeader('hello', 'world'); $this->childResponse->addHeader('hello', null); - $this->assertEquals(1, count($this->childResponse->getHeaders())); + $this->assertEquals(1, count($this->childResponse->getHeaders())); } @@ -93,18 +107,18 @@ class ResponseTest extends \PHPUnit_Framework_TestCase { public function testCacheSecondsZero() { $this->childResponse->cacheFor(0); - + $headers = $this->childResponse->getHeaders(); - $this->assertEquals('no-cache, must-revalidate', $headers['Cache-Control']); + $this->assertEquals('no-cache, must-revalidate', $headers['Cache-Control']); } public function testCacheSeconds() { $this->childResponse->cacheFor(33); - + $headers = $this->childResponse->getHeaders(); - $this->assertEquals('max-age=33, must-revalidate', - $headers['Cache-Control']); + $this->assertEquals('max-age=33, must-revalidate', + $headers['Cache-Control']); } |