Special case the empty rule.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 22:15:37 +0000 (18:15 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 6 Aug 2011 22:15:37 +0000 (18:15 -0400)
http/url_map.php
testing/tests/http/url_map_test.php

index 8df90e82f7aa1c15d23c0f851c8452b716f7e030..fac6a4260bb0aab122b35dac59521b42d2629a00 100644 (file)
@@ -103,6 +103,14 @@ class UrlMap
     $path_length = strlen($request->url);
 
     foreach ($this->map as $rule => $action) {
+      // First check if this is the empty rule.
+      if (empty($rule)) {
+        if (empty($request->url))
+          return $action;
+        else
+          continue;
+      }
+
       // Check if this is a regular expression rule and match it.
       if ($rule[0] == '/' && substr($rule, -1) == '/') {
         $matches = array();
index afe5436aa5e22a8b2469597b305f137168948d60..722eb3225d169a05763cf9387b5395be6f6f47c7 100644 (file)
@@ -74,6 +74,25 @@ class UrlMapTest extends \PHPUnit_Framework_TestCase
     $this->assertEquals('Second', $this->fixture->Evaluate($request));
   }
 
+  public function testEmptyRule()
+  {
+    $map = array(
+      'some/first' => 'First',
+      '' => 'Index',
+      'some/second' => 'Second'
+    );
+    $this->fixture->set_map($map);
+
+    $request = new http\Request('some/first');
+    $this->assertEquals('First', $this->fixture->Evaluate($request));
+
+    $request = new http\Request('');
+    $this->assertEquals('Index', $this->fixture->Evaluate($request));
+
+    $request = new http\Request('some/second');
+    $this->assertEquals('Second', $this->fixture->Evaluate($request));
+  }
+
   public function testExtractSingle()
   {
     $map = array(