mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Director::test now calls RequestProcessor
This fixes https://github.com/silverstripe/silverstripe-framework/issues/2517 and provides some testing around the use of RequestProcessor in general.
This commit is contained in:
parent
14e5c80dad
commit
7bcb180f27
@ -258,8 +258,18 @@ class Director implements TemplateGlobalProvider {
|
|||||||
|
|
||||||
$request = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
|
$request = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
|
||||||
if($headers) foreach($headers as $k => $v) $request->addHeader($k, $v);
|
if($headers) foreach($headers as $k => $v) $request->addHeader($k, $v);
|
||||||
|
|
||||||
|
// Pre-request filtering
|
||||||
|
// @see issue #2517
|
||||||
|
$model = DataModel::inst();
|
||||||
|
$output = Injector::inst()->get('RequestProcessor')->preRequest($request, $session, $model);
|
||||||
|
if ($output === false) {
|
||||||
|
// @TODO Need to NOT proceed with the request in an elegant manner
|
||||||
|
throw new SS_HTTPResponse_Exception(_t('Director.INVALID_REQUEST', 'Invalid request'), 400);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Pass in the DataModel
|
// TODO: Pass in the DataModel
|
||||||
$result = Director::handleRequest($request, $session, DataModel::inst());
|
$result = Director::handleRequest($request, $session, $model);
|
||||||
|
|
||||||
// Ensure that the result is an SS_HTTPResponse object
|
// Ensure that the result is an SS_HTTPResponse object
|
||||||
if(is_string($result)) {
|
if(is_string($result)) {
|
||||||
@ -272,6 +282,11 @@ class Director implements TemplateGlobalProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$output = Injector::inst()->get('RequestProcessor')->postRequest($request, $result, $model);
|
||||||
|
if ($output === false) {
|
||||||
|
throw new SS_HTTPResponse_Exception("Invalid response");
|
||||||
|
}
|
||||||
|
|
||||||
// Restore the superglobals
|
// Restore the superglobals
|
||||||
$_REQUEST = $existingRequestVars;
|
$_REQUEST = $existingRequestVars;
|
||||||
$_GET = $existingGetVars;
|
$_GET = $existingGetVars;
|
||||||
|
@ -683,23 +683,24 @@ class Injector {
|
|||||||
* class name of the object to register)
|
* class name of the object to register)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function registerService($service, $replace=null) {
|
public function registerService($service, $replace = null) {
|
||||||
$registerAt = get_class($service);
|
$registerAt = get_class($service);
|
||||||
if ($replace != null) {
|
if ($replace != null) {
|
||||||
$registerAt = $replace;
|
$registerAt = $replace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->specs[$registerAt] = array('class' => get_class($service));
|
||||||
$this->serviceCache[$registerAt] = $service;
|
$this->serviceCache[$registerAt] = $service;
|
||||||
$this->inject($service);
|
$this->inject($service);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a service with an explicit name
|
* Register a service with an explicit name
|
||||||
|
*
|
||||||
|
* @deprecated since 3.1.1
|
||||||
*/
|
*/
|
||||||
public function registerNamedService($name, $service) {
|
public function registerNamedService($name, $service) {
|
||||||
$this->specs[$name] = array('class' => get_class($service));
|
return $this->registerService($service, $name);
|
||||||
$this->serviceCache[$name] = $service;
|
|
||||||
$this->inject($service);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -344,6 +344,72 @@ class DirectorTest extends SapphireTest {
|
|||||||
|
|
||||||
$_SERVER = $origServer;
|
$_SERVER = $origServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRequestFilterInDirectorTest() {
|
||||||
|
$filter = new TestRequestFilter;
|
||||||
|
|
||||||
|
$processor = new RequestProcessor(array($filter));
|
||||||
|
|
||||||
|
$currentProcessor = Injector::inst()->get('RequestProcessor');
|
||||||
|
|
||||||
|
Injector::inst()->registerService($processor, 'RequestProcessor');
|
||||||
|
|
||||||
|
$response = Director::test('some-dummy-url');
|
||||||
|
|
||||||
|
$this->assertEquals(1, $filter->preCalls);
|
||||||
|
$this->assertEquals(1, $filter->postCalls);
|
||||||
|
|
||||||
|
$filter->failPost = true;
|
||||||
|
|
||||||
|
$this->setExpectedException('SS_HTTPResponse_Exception');
|
||||||
|
|
||||||
|
$response = Director::test('some-dummy-url');
|
||||||
|
|
||||||
|
$this->assertEquals(2, $filter->preCalls);
|
||||||
|
$this->assertEquals(2, $filter->postCalls);
|
||||||
|
|
||||||
|
$filter->failPre = true;
|
||||||
|
|
||||||
|
$response = Director::test('some-dummy-url');
|
||||||
|
|
||||||
|
$this->assertEquals(3, $filter->preCalls);
|
||||||
|
|
||||||
|
// preCall 'false' will trigger an exception and prevent post call execution
|
||||||
|
$this->assertEquals(2, $filter->postCalls);
|
||||||
|
|
||||||
|
// swap back otherwise our wrapping test execution request may fail in the post processing later
|
||||||
|
Injector::inst()->registerService($currentProcessor, 'RequestProcessor');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestRequestFilter implements RequestFilter, TestOnly {
|
||||||
|
public $preCalls = 0;
|
||||||
|
public $postCalls = 0;
|
||||||
|
|
||||||
|
public $failPre = false;
|
||||||
|
public $failPost = false;
|
||||||
|
|
||||||
|
public function preRequest(\SS_HTTPRequest $request, \Session $session, \DataModel $model) {
|
||||||
|
++$this->preCalls;
|
||||||
|
|
||||||
|
if ($this->failPre) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postRequest(\SS_HTTPRequest $request, \SS_HTTPResponse $response, \DataModel $model) {
|
||||||
|
++$this->postCalls;
|
||||||
|
|
||||||
|
if ($this->failPost) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reset() {
|
||||||
|
$this->preCalls = 0;
|
||||||
|
$this->postCalls = 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DirectorTestRequest_Controller extends Controller implements TestOnly {
|
class DirectorTestRequest_Controller extends Controller implements TestOnly {
|
||||||
|
Loading…
Reference in New Issue
Block a user