Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUG Director::test now calls RequestProcessor

This fixes #2517
and provides some testing around the use of RequestProcessor in general.
  • Loading branch information...
commit 7bcb180f27972e3e80fb97c39dc00d2f6dfb3682 1 parent 14e5c80
Marcus nyeholt authored
17 control/Director.php
View
@@ -258,8 +258,18 @@ public static function test($url, $postVars = null, $session = null, $httpMethod
$request = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
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
- $result = Director::handleRequest($request, $session, DataModel::inst());
+ $result = Director::handleRequest($request, $session, $model);
// Ensure that the result is an SS_HTTPResponse object
if(is_string($result)) {
@@ -272,6 +282,11 @@ public static function test($url, $postVars = null, $session = null, $httpMethod
}
}
+ $output = Injector::inst()->get('RequestProcessor')->postRequest($request, $result, $model);
+ if ($output === false) {
+ throw new SS_HTTPResponse_Exception("Invalid response");
+ }
+
// Restore the superglobals
$_REQUEST = $existingRequestVars;
$_GET = $existingGetVars;
9 control/injector/Injector.php
View
@@ -683,23 +683,24 @@ public function hasService($name) {
* class name of the object to register)
*
*/
- public function registerService($service, $replace=null) {
+ public function registerService($service, $replace = null) {
$registerAt = get_class($service);
if ($replace != null) {
$registerAt = $replace;
}
+ $this->specs[$registerAt] = array('class' => get_class($service));
$this->serviceCache[$registerAt] = $service;
$this->inject($service);
}
/**
* Register a service with an explicit name
+ *
+ * @deprecated since 3.1.1
*/
public function registerNamedService($name, $service) {
- $this->specs[$name] = array('class' => get_class($service));
- $this->serviceCache[$name] = $service;
- $this->inject($service);
+ return $this->registerService($service, $name);
}
/**
66 tests/control/DirectorTest.php
View
@@ -344,6 +344,72 @@ public function testIsHttps() {
$_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 {
Please sign in to comment.
Something went wrong with that request. Please try again.