-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1686 from jim-parry/refactor/filters
Refactor/filters
- Loading branch information
Showing
20 changed files
with
294 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
namespace CodeIgniter\Filters; | ||
|
||
use Config\Filters as FilterConfig; | ||
use CodeIgniter\Config\Services; | ||
use CodeIgniter\Filters\Exceptions\FilterException; | ||
use CodeIgniter\Honeypot\Exceptions\HoneypotException; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
|
||
/** | ||
* @backupGlobals enabled | ||
*/ | ||
class CSRFTest extends \CIUnitTestCase | ||
{ | ||
|
||
protected $config; | ||
protected $request; | ||
protected $response; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->config = new \Config\Filters(); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
public function testNormal() | ||
{ | ||
$this->config->globals = [ | ||
'before' => ['csrf'], | ||
'after' => [], | ||
]; | ||
|
||
$this->request = Services::request(null, false); | ||
$this->response = Services::response(); | ||
|
||
$filters = new Filters($this->config, $this->request, $this->response); | ||
$uri = 'admin/foo/bar'; | ||
|
||
// we expect CSRF requests to be ignored in CLI | ||
$expected = $this->request; | ||
$request = $filters->run($uri, 'before'); | ||
$this->assertEquals($expected, $request); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace CodeIgniter\Filters; | ||
|
||
use Config\Filters as FilterConfig; | ||
use CodeIgniter\Config\Services; | ||
use CodeIgniter\Filters\Exceptions\FilterException; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
|
||
/** | ||
* @backupGlobals enabled | ||
*/ | ||
class DebugToolbarTest extends \CIUnitTestCase | ||
{ | ||
|
||
protected $request; | ||
protected $response; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->request = Services::request(); | ||
$this->response = Services::response(); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
public function testDebugToolbarFilter() | ||
{ | ||
$_SERVER['REQUEST_METHOD'] = 'GET'; | ||
|
||
$config = new FilterConfig(); | ||
$config->globals = [ | ||
'before' => ['toolbar'], // not normal; exercising its before() | ||
'after' => ['toolbar'], | ||
]; | ||
|
||
$filter = new DebugToolbar(); | ||
|
||
$expectedBefore = $this->request; | ||
$expectedAfter = $this->response; | ||
|
||
// nothing should change here, since we have no before logic | ||
$filter->before($this->request); | ||
$this->assertEquals($expectedBefore, $this->request); | ||
|
||
// nothing should change here, since we are running in the CLI | ||
$filter->after($this->request, $this->response); | ||
$this->assertEquals($expectedAfter, $this->response); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
namespace CodeIgniter\Filters; | ||
|
||
use Config\Filters as FilterConfig; | ||
use CodeIgniter\Config\Services; | ||
use CodeIgniter\Filters\Exceptions\FilterException; | ||
use CodeIgniter\Honeypot\Exceptions\HoneypotException; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
|
||
/** | ||
* @backupGlobals enabled | ||
*/ | ||
class HoneypotTest extends \CIUnitTestCase | ||
{ | ||
|
||
protected $config; | ||
protected $honey; | ||
protected $request; | ||
protected $response; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->config = new \Config\Filters(); | ||
$this->honey = new \Config\Honeypot(); | ||
|
||
unset($_POST[$this->honey->name]); | ||
$_SERVER['REQUEST_METHOD'] = 'POST'; | ||
$_POST[$this->honey->name] = 'hey'; | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
public function testBeforeTriggered() | ||
{ | ||
$this->config->globals = [ | ||
'before' => ['honeypot'], | ||
'after' => [], | ||
]; | ||
|
||
$this->request = Services::request(null, false); | ||
$this->response = Services::response(); | ||
|
||
$filters = new Filters($this->config, $this->request, $this->response); | ||
$uri = 'admin/foo/bar'; | ||
|
||
$this->expectException(HoneypotException::class); | ||
$request = $filters->run($uri, 'before'); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
public function testBeforeClean() | ||
{ | ||
$this->config->globals = [ | ||
'before' => ['honeypot'], | ||
'after' => [], | ||
]; | ||
|
||
unset($_POST[$this->honey->name]); | ||
$this->request = Services::request(null, false); | ||
$this->response = Services::response(); | ||
|
||
$expected = $this->request; | ||
|
||
$filters = new Filters($this->config, $this->request, $this->response); | ||
$uri = 'admin/foo/bar'; | ||
|
||
$request = $filters->run($uri, 'before'); | ||
$this->assertEquals($expected, $request); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testAfter() | ||
{ | ||
$this->config->globals = [ | ||
'before' => [], | ||
'after' => ['honeypot'], | ||
]; | ||
|
||
$this->request = Services::request(null, false); | ||
$this->response = Services::response(); | ||
|
||
$filters = new Filters($this->config, $this->request, $this->response); | ||
$uri = 'admin/foo/bar'; | ||
|
||
$this->response->setBody('<form></form>'); | ||
$this->response = $filters->run($uri, 'after'); | ||
$this->assertContains($this->honey->name, $this->response->getBody()); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testAfterNotApplicable() | ||
{ | ||
$this->config->globals = [ | ||
'before' => [], | ||
'after' => ['honeypot'], | ||
]; | ||
|
||
$this->request = Services::request(null, false); | ||
$this->response = Services::response(); | ||
|
||
$filters = new Filters($this->config, $this->request, $this->response); | ||
$uri = 'admin/foo/bar'; | ||
|
||
$this->response->setBody('<div></div>'); | ||
$this->response = $filters->run($uri, 'after'); | ||
$this->assertNotContains($this->honey->name, $this->response->getBody()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.