Permalink
Browse files

Merge pull request #1007 from tractorcow/pulls/3.1-security-errorpage

API Error page support for Security controller errors
  • Loading branch information...
2 parents 1f4d9e4 + f9cb880 commit 435a5ee8430dd5fbdaf5831841f29876b42d52fd Hamish Friedlander committed May 5, 2014
Showing with 34 additions and 2 deletions.
  1. +4 −1 _config/config.yml
  2. +17 −0 code/controllers/ErrorPageControllerExtension.php
  3. +13 −1 tests/model/ErrorPageTest.php
View
@@ -1,3 +1,6 @@
LeftAndMain:
extensions:
- - LeftAndMainPageIconsExtension
+ - LeftAndMainPageIconsExtension
+Security:
+ extensions:
+ - ErrorPageControllerExtension
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * Enhances error handling for a controller with ErrorPage generated output
+ *
+ * @package cms
+ * @subpackage controller
+ */
+class ErrorPageControllerExtension extends Extension {
+
+ public function onBeforeHTTPError($statusCode, $request) {
+ $response = ErrorPage::response_for($statusCode);
+ if($response) {
+ throw new SS_HTTPResponse_Exception($response, $statusCode);
+ }
+ }
+}
@@ -70,4 +70,16 @@ public function testBehaviourOf403() {
$this->assertEquals($response->getStatusCode(), '403');
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
}
-}
+
+ public function testSecurityError() {
+ // Generate 404 page
+ $page = $this->objFromFixture('ErrorPage', '404');
+ $page->publish('Stage', 'Live');
+
+ // Test invalid action
+ $response = $this->get('Security/nosuchaction');
+ $this->assertEquals($response->getStatusCode(), '404');
+ $this->assertNotNull($response->getBody());
+ $this->assertContains('text/html', $response->getHeader('Content-Type'));
+ }
+}

0 comments on commit 435a5ee

Please sign in to comment.