diff options
author | Lukas Reschke <lukas@owncloud.com> | 2014-11-27 14:19:00 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2014-11-27 14:19:00 +0100 |
commit | 048139074df674d0ac82da12357d3934af3abc2e (patch) | |
tree | 04e66fdbdc9fbb255036e36fe29b2274b3661af6 /tests | |
parent | e306b588d29d71bf547b19d1725ea9ad1a5fbf42 (diff) | |
download | nextcloud-server-048139074df674d0ac82da12357d3934af3abc2e.tar.gz nextcloud-server-048139074df674d0ac82da12357d3934af3abc2e.zip |
Add functions to modify cookies to response class
Currently there is no AppFramework way to modify cookies, which makes it unusable for quite some use-cases or results in untestable code.
This PR adds some basic functionalities to add and invalidate cookies.
Usage:
```php
$response = new TemplateResponse(...);
$response->addCookie('foo', 'bar');
$response->invalidateCookie('foo');
$response->addCookie('bar', 'foo', new \DateTime('2015-01-01 00:00'));
```
Existing cookies can be accessed with the AppFramework using `$this->request->getCookie($name)`.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/appframework/AppTest.php | 2 | ||||
-rw-r--r-- | tests/lib/appframework/http/DispatcherTest.php | 16 | ||||
-rw-r--r-- | tests/lib/appframework/http/ResponseTest.php | 86 |
3 files changed, 95 insertions, 9 deletions
diff --git a/tests/lib/appframework/AppTest.php b/tests/lib/appframework/AppTest.php index bd565e9765e..86128db118f 100644 --- a/tests/lib/appframework/AppTest.php +++ b/tests/lib/appframework/AppTest.php @@ -63,7 +63,7 @@ class AppTest extends \Test\TestCase { public function testControllerNameAndMethodAreBeingPassed(){ - $return = array(null, array(), null); + $return = array(null, array(), array(), null); $this->dispatcher->expects($this->once()) ->method('dispatch') ->with($this->equalTo($this->controller), diff --git a/tests/lib/appframework/http/DispatcherTest.php b/tests/lib/appframework/http/DispatcherTest.php index f92e7161e6b..45ebd6fce96 100644 --- a/tests/lib/appframework/http/DispatcherTest.php +++ b/tests/lib/appframework/http/DispatcherTest.php @@ -227,7 +227,7 @@ class DispatcherTest extends \Test\TestCase { $this->assertEquals($httpHeaders, $response[0]); $this->assertEquals($responseHeaders, $response[1]); - $this->assertEquals($out, $response[2]); + $this->assertEquals($out, $response[3]); } @@ -246,7 +246,7 @@ class DispatcherTest extends \Test\TestCase { $this->assertEquals($httpHeaders, $response[0]); $this->assertEquals($responseHeaders, $response[1]); - $this->assertEquals($out, $response[2]); + $this->assertEquals($out, $response[3]); } @@ -301,7 +301,7 @@ class DispatcherTest extends \Test\TestCase { $this->dispatcherPassthrough(); $response = $this->dispatcher->dispatch($controller, 'exec'); - $this->assertEquals('[3,true,4,1]', $response[2]); + $this->assertEquals('[3,true,4,1]', $response[3]); } @@ -324,7 +324,7 @@ class DispatcherTest extends \Test\TestCase { $this->dispatcherPassthrough(); $response = $this->dispatcher->dispatch($controller, 'exec'); - $this->assertEquals('[3,true,4,7]', $response[2]); + $this->assertEquals('[3,true,4,7]', $response[3]); } @@ -350,7 +350,7 @@ class DispatcherTest extends \Test\TestCase { $this->dispatcherPassthrough(); $response = $this->dispatcher->dispatch($controller, 'exec'); - $this->assertEquals('{"text":[3,false,4,1]}', $response[2]); + $this->assertEquals('{"text":[3,false,4,1]}', $response[3]); } @@ -375,7 +375,7 @@ class DispatcherTest extends \Test\TestCase { $this->dispatcherPassthrough(); $response = $this->dispatcher->dispatch($controller, 'execDataResponse'); - $this->assertEquals('{"text":[3,false,4,1]}', $response[2]); + $this->assertEquals('{"text":[3,false,4,1]}', $response[3]); } @@ -401,7 +401,7 @@ class DispatcherTest extends \Test\TestCase { $this->dispatcherPassthrough(); $response = $this->dispatcher->dispatch($controller, 'exec'); - $this->assertEquals('{"text":[3,false,4,1]}', $response[2]); + $this->assertEquals('{"text":[3,false,4,1]}', $response[3]); } @@ -429,7 +429,7 @@ class DispatcherTest extends \Test\TestCase { $this->dispatcherPassthrough(); $response = $this->dispatcher->dispatch($controller, 'exec'); - $this->assertEquals('{"text":[3,true,4,1]}', $response[2]); + $this->assertEquals('{"text":[3,true,4,1]}', $response[3]); } diff --git a/tests/lib/appframework/http/ResponseTest.php b/tests/lib/appframework/http/ResponseTest.php index 04e19fdaf71..b4352348bae 100644 --- a/tests/lib/appframework/http/ResponseTest.php +++ b/tests/lib/appframework/http/ResponseTest.php @@ -76,6 +76,92 @@ class ResponseTest extends \Test\TestCase { } + public function testAddCookie() { + $this->childResponse->addCookie('foo', 'bar'); + $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01')); + + $expectedResponse = array( + 'foo' => array( + 'value' => 'bar', + 'expireDate' => null, + ), + 'bar' => array( + 'value' => 'foo', + 'expireDate' => new \DateTime('1970-01-01') + ) + ); + $this->assertEquals($expectedResponse, $this->childResponse->getCookies()); + } + + + function testSetCookies() { + $expected = array( + 'foo' => array( + 'value' => 'bar', + 'expireDate' => null, + ), + 'bar' => array( + 'value' => 'foo', + 'expireDate' => new \DateTime('1970-01-01') + ) + ); + + $this->childResponse->setCookies($expected); + $cookies = $this->childResponse->getCookies(); + + $this->assertEquals($expected, $cookies); + } + + + function testInvalidateCookie() { + $this->childResponse->addCookie('foo', 'bar'); + $this->childResponse->invalidateCookie('foo'); + $expected = array( + 'foo' => array( + 'value' => 'expired', + 'expireDate' => new \DateTime('1971-01-01') + ) + ); + + $cookies = $this->childResponse->getCookies(); + + $this->assertEquals($expected, $cookies); + } + + + function testInvalidateCookies() { + $this->childResponse->addCookie('foo', 'bar'); + $this->childResponse->addCookie('bar', 'foo'); + $expected = array( + 'foo' => array( + 'value' => 'bar', + 'expireDate' => null + ), + 'bar' => array( + 'value' => 'foo', + 'expireDate' => null + ) + ); + $cookies = $this->childResponse->getCookies(); + $this->assertEquals($expected, $cookies); + + $this->childResponse->invalidateCookies(array('foo', 'bar')); + $expected = array( + 'foo' => array( + 'value' => 'expired', + 'expireDate' => new \DateTime('1971-01-01') + ), + 'bar' => array( + 'value' => 'expired', + 'expireDate' => new \DateTime('1971-01-01') + ) + ); + + $cookies = $this->childResponse->getCookies(); + $this->assertEquals($expected, $cookies); + } + + public function testRenderReturnNullByDefault(){ $this->assertEquals(null, $this->childResponse->render()); } |