From 5e6c1b902b97f8d06ab18975d3b103a11b20b27f Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Wed, 26 Feb 2014 12:04:47 +0000 Subject: [PATCH] Making Director::test() ignore URL Anchors Anchors should never make it to the server when they are in the browser URL bar, however tests are slightly different and some `Link()` functions may return a URL anchor. Instead of every test checking a link and stripping the anchor, I feel the Director::test() function should strip them off. --- control/Director.php | 4 ++++ tests/control/DirectorTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/control/Director.php b/control/Director.php index 3ea35b395..2a6fdf990 100644 --- a/control/Director.php +++ b/control/Director.php @@ -244,6 +244,10 @@ class Director implements TemplateGlobalProvider { Config::inst()->update('Cookie', 'report_errors', false); Requirements::set_backend(new Requirements_Backend()); + if (strpos($url, '#') !== false) { + $url = substr($url, 0, strpos($url, '#')); + } + // Handle absolute URLs if (@parse_url($url, PHP_URL_HOST) != '') { $bits = parse_url($url); diff --git a/tests/control/DirectorTest.php b/tests/control/DirectorTest.php index 385fc2b03..8e801b956 100644 --- a/tests/control/DirectorTest.php +++ b/tests/control/DirectorTest.php @@ -344,6 +344,23 @@ class DirectorTest extends SapphireTest { $_SERVER = $origServer; } + + public function testTestIgnoresHashes() { + //test that hashes are ignored + $url = "DirectorTestRequest_Controller/returnGetValue?somekey=key"; + $hash = "#test"; + $response = Director::test($url . $hash, null, null, null, null, null, null, $request); + $this->assertFalse($response->isError()); + $this->assertEquals('key', $response->getBody()); + $this->assertEquals($request->getURL(true), $url); + + //test encoded hashes are accepted + $url = "DirectorTestRequest_Controller/returnGetValue?somekey=test%23key"; + $response = Director::test($url, null, null, null, null, null, null, $request); + $this->assertFalse($response->isError()); + $this->assertEquals('test#key', $response->getBody()); + $this->assertEquals($request->getURL(true), $url); + } public function testRequestFilterInDirectorTest() { $filter = new TestRequestFilter;