Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
die_coolen_jungs
our_own_cloud_project
Commits
e2001c6d
Commit
e2001c6d
authored
Oct 30, 2014
by
Morris Jobke
Browse files
Merge pull request #11821 from owncloud/generic-response
Add a generic data response
parents
5f76f825
0696099b
Changes
8
Hide whitespace changes
Inline
Side-by-side
lib/private/appframework/http/dispatcher.php
View file @
e2001c6d
...
...
@@ -30,6 +30,7 @@ use \OC\AppFramework\Utility\ControllerMethodReflector;
use
OCP\AppFramework\Controller
;
use
OCP\AppFramework\Http\Response
;
use
OCP\AppFramework\Http\DataResponse
;
use
OCP\IRequest
;
...
...
@@ -154,8 +155,8 @@ class Dispatcher {
$response
=
call_user_func_array
(
array
(
$controller
,
$methodName
),
$arguments
);
// format response
if not of type response
if
(
!
(
$response
instanceof
Response
))
{
// format response
if
(
$response
instanceof
DataResponse
||
!
(
$response
instanceof
Response
))
{
// get format from the url format or request format parameter
$format
=
$this
->
request
->
getParam
(
'format'
);
...
...
lib/public/appframework/controller.php
View file @
e2001c6d
...
...
@@ -29,6 +29,7 @@ namespace OCP\AppFramework;
use
OCP\AppFramework\Http\TemplateResponse
;
use
OCP\AppFramework\Http\JSONResponse
;
use
OCP\AppFramework\Http\DataResponse
;
use
OCP\IRequest
;
...
...
@@ -63,8 +64,17 @@ abstract class Controller {
// default responders
$this
->
responders
=
array
(
'json'
=>
function
(
$response
)
{
return
new
JSONResponse
(
$response
);
'json'
=>
function
(
$data
)
{
if
(
$data
instanceof
DataResponse
)
{
$response
=
new
JSONResponse
(
$data
->
getData
(),
$data
->
getStatus
()
);
$response
->
setHeaders
(
$data
->
getHeaders
());
return
$response
;
}
else
{
return
new
JSONResponse
(
$data
);
}
}
);
}
...
...
lib/public/appframework/http/dataresponse.php
0 → 100644
View file @
e2001c6d
<?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/>.
*
*/
/**
* Public interface of ownCloud for apps to use.
* AppFramework\HTTP\DataResponse class
*/
namespace
OCP\AppFramework\Http
;
use
OCP\AppFramework\Http
;
/**
* A generic DataResponse class that is used to return generic data responses
* for responders to transform
*/
class
DataResponse
extends
Response
{
/**
* response data
* @var array|object
*/
protected
$data
;
/**
* @param array|object $data the object or array that should be transformed
* @param int $statusCode the Http status code, defaults to 200
* @param array $headers additional key value based headers
*/
public
function
__construct
(
$data
=
array
(),
$statusCode
=
Http
::
STATUS_OK
,
array
$headers
=
array
())
{
$this
->
data
=
$data
;
$this
->
setStatus
(
$statusCode
);
$this
->
setHeaders
(
array_merge
(
$this
->
getHeaders
(),
$headers
));
}
/**
* Sets values in the data json array
* @param array|object $data an array or object which will be transformed
* @return DataResponse Reference to this object
*/
public
function
setData
(
$data
){
$this
->
data
=
$data
;
return
$this
;
}
/**
* Used to get the set parameters
* @return array the data
*/
public
function
getData
(){
return
$this
->
data
;
}
}
lib/public/appframework/http/response.php
View file @
e2001c6d
...
...
@@ -93,7 +93,7 @@ class Response {
*/
public
function
addHeader
(
$name
,
$value
)
{
$name
=
trim
(
$name
);
// always remove leading and trailing whitespace
// to be able to reliably check for security
// to be able to reliably check for security
// headers
if
(
is_null
(
$value
))
{
...
...
@@ -106,6 +106,18 @@ class Response {
}
/**
* Set the headers
* @param array key value header pairs
* @return Response Reference to this object
*/
public
function
setHeaders
(
$headers
)
{
$this
->
headers
=
$headers
;
return
$this
;
}
/**
* Returns the set headers
* @return array the headers
...
...
tests/lib/appframework/controller/ControllerTest.php
View file @
e2001c6d
...
...
@@ -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'
);
...
...
tests/lib/appframework/http/DataResponseTest.php
0 → 100644
View file @
e2001c6d
<?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
());
}
}
tests/lib/appframework/http/DispatcherTest.php
View file @
e2001c6d
...
...
@@ -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
(
...
...
tests/lib/appframework/http/ResponseTest.php
View file @
e2001c6d
...
...
@@ -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'
]);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment