Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Better way to parse the request URI in 'ugly URLs' mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicchabant committed Oct 7, 2011
1 parent 7a00ae4 commit c0c1188
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 7 deletions.
16 changes: 9 additions & 7 deletions _piecrust/src/PieCrust/Util/ServerHelper.php
Expand Up @@ -19,14 +19,16 @@ public static function getRequestUri(array $server, $prettyUrls)
if (!$prettyUrls)
{
// Using standard query (no pretty URLs / URL rewriting)
$requestUri = $server['QUERY_STRING'];
if ($requestUri == null or $requestUri == '')
$requestUri = '/';
$requestVars = array();
parse_str($server['QUERY_STRING'], $requestVars);
foreach ($requestVars as $key => $value)
{
$requestUri = '/';
}
else if (strpos($requestUri, '&') !== false)
{
$requestUri = strstr($requestUri, '&', true);
if ($key[0] == '/' and $value == null)
{
$requestUri = $key;
break;
}
}
}
else
Expand Down
84 changes: 84 additions & 0 deletions tests/src/test-cases/ServerHelperTest.php
@@ -0,0 +1,84 @@
<?php

require_once (dirname(__DIR__) . '/unittest_setup.php');

use PieCrust\Util\ServerHelper;


class ServerHelperTest extends PHPUnit_Framework_TestCase
{
public function getRequestUriDataProvider()
{
return array(
array(
array('QUERY_STRING' => ''),
false,
'/'
),
array(
array('QUERY_STRING' => '/'),
false,
'/'
),
array(
array('QUERY_STRING' => '/blah'),
false,
'/blah'
),
array(
array('QUERY_STRING' => '/foo/bar'),
false,
'/foo/bar'
),
array(
array('QUERY_STRING' => '!debug'),
false,
'/'
),
array(
array('QUERY_STRING' => '/&!debug'),
false,
'/'
),
array(
array('QUERY_STRING' => '/foo/bar&!debug'),
false,
'/foo/bar'
),
array(
array('QUERY_STRING' => '/blah&!debug&!nocache'),
false,
'/blah'
),
array(
array('REQUEST_URI' => '/blah'),
true,
'/blah'
),
array(
array('REQUEST_URI' => '/blah?!debug'),
true,
'/blah'
),
array(
array('REQUEST_URI' => '/foo/bar'),
true,
'/foo/bar'
),
array(
array('REQUEST_URI' => '/foo/bar?!debug&!nocache'),
true,
'/foo/bar'
)
);
}

/**
* @dataProvider getRequestUriDataProvider
*/
public function testGetRequestUri($server, $prettyUrls, $expectedRequestUri)
{
$actualUri = ServerHelper::getRequestUri($server, $prettyUrls);
$this->assertEquals($expectedRequestUri, $actualUri, 'The request URI was not what was expected.');
}
}

0 comments on commit c0c1188

Please sign in to comment.