Skip to content
Snippets Groups Projects
Commit 8f6ea900 authored by Thomas Tanghus's avatar Thomas Tanghus
Browse files

Chainable Response in AppFramework

parent 9a31e533
No related branches found
No related tags found
No related merge requests found
......@@ -69,6 +69,8 @@ class JSONResponse extends Response {
*/
public function setData($data){
$this->data = $data;
return $this;
}
......
......@@ -80,6 +80,7 @@ class Response {
$this->addHeader('Cache-Control', 'no-cache, must-revalidate');
}
return $this;
}
......@@ -95,6 +96,8 @@ class Response {
} else {
$this->headers[$name] = $value;
}
return $this;
}
......@@ -133,6 +136,8 @@ class Response {
*/
public function setStatus($status) {
$this->status = $status;
return $this;
}
......@@ -168,6 +173,8 @@ class Response {
*/
public function setETag($ETag) {
$this->ETag = $ETag;
return $this;
}
......@@ -177,6 +184,8 @@ class Response {
*/
public function setLastModified($lastModified) {
$this->lastModified = $lastModified;
return $this;
}
......
......@@ -77,6 +77,8 @@ class TemplateResponse extends Response {
*/
public function setParams(array $params){
$this->params = $params;
return $this;
}
......@@ -107,6 +109,8 @@ class TemplateResponse extends Response {
*/
public function renderAs($renderAs){
$this->renderAs = $renderAs;
return $this;
}
......
......@@ -28,6 +28,7 @@ namespace OC\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http;
//require_once(__DIR__ . "/../classloader.php");
......@@ -95,4 +96,13 @@ class JSONResponseTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($code, $response->getStatus());
}
public function testChainability() {
$params = array('hi', 'yo');
$this->json->setData($params)
->setStatus(Http::STATUS_NOT_FOUND);
$this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus());
$this->assertEquals(array('hi', 'yo'), $this->json->getData());
}
}
......@@ -117,5 +117,25 @@ class ResponseTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
}
public function testChainability() {
$lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
$lastModified->setTimestamp(1);
$this->childResponse->setEtag('hi')
->setStatus(Http::STATUS_NOT_FOUND)
->setLastModified($lastModified)
->cacheFor(33)
->addHeader('hello', 'world');
$headers = $this->childResponse->getHeaders();
$this->assertEquals('world', $headers['hello']);
$this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
$this->assertEquals('hi', $this->childResponse->getEtag());
$this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
$this->assertEquals('max-age=33, must-revalidate',
$headers['Cache-Control']);
}
}
......@@ -25,6 +25,7 @@
namespace OC\AppFramework\Http;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http;
class TemplateResponseTest extends \PHPUnit_Framework_TestCase {
......@@ -98,4 +99,13 @@ class TemplateResponseTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($render, $this->tpl->getRenderAs());
}
public function testChainability() {
$params = array('hi' => 'yo');
$this->tpl->setParams($params)
->setStatus(Http::STATUS_NOT_FOUND);
$this->assertEquals(Http::STATUS_NOT_FOUND, $this->tpl->getStatus());
$this->assertEquals(array('hi' => 'yo'), $this->tpl->getParams());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment