Add RootController::MakeURL()
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 14 Aug 2011 16:01:54 +0000 (12:01 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 14 Aug 2011 16:01:54 +0000 (12:01 -0400)
http/root_controller.php
testing/tests/http/root_controller_test.php

index 4a0832f271c6cf71f2b00b96f6aa65cba36a3c9c..85a7174d7e37292c37a4b41b7b87ae4d6ad08c6d 100644 (file)
@@ -185,6 +185,40 @@ class RootController
         return $pattern;
     }
   }
+
+  /*!
+    Given a relative path, return an absolute path from the root controller.
+    @param string The relative path for which a URL will be created
+    @param bool Include the HTTP scheme and host to create an RFC URL if true;
+                if false an absolute path will be returned.
+  */
+  public function MakeURL($new_path, $url = FALSE)
+  {
+    // Detect the common paths between the REQUEST_URI and the PATH_INFO. That
+    // common piece will be the path to the root controller.
+    $request_uri = $this->request()->data['_SERVER']['REQUEST_URI'];
+    $path_info = $this->request()->data['_SERVER']['PATH_INFO'];
+    $common_uri = strstr($request_uri, $path_info, TRUE);
+
+    // If just constructing an absolute path, return that now.
+    if (!$url)
+      return $common_uri . $new_path;
+
+    // Otherwise, build the host part.
+    $url = 'http';
+    if (isset($this->request()->data['_SERVER']['HTTPS']) &&
+        $this->request()->data['_SERVER']['HTTPS'] == 'on') {
+      $url .= 's';
+    }
+    $url .= '://' . $this->request()->data['_SERVER']['HTTP_HOST'];
+
+    $port = $this->request()->data['_SERVER']['SERVER_PORT'];
+    if ($port != 80 && $port != 443)
+      $url .= ':' . $port;
+
+    $url .= $common_uri;
+    return $url . $new_path;
+  }
 }
 
 /*!
index 2135db57e5368c6ed9a7d876189dd48270b9b496..332454531fc325df0c803013f004bfdd23e56d67 100644 (file)
@@ -160,4 +160,38 @@ class RootControllerTest extends \PHPUnit_Framework_TestCase
     $mock->set_output_filter($output_filter);
     $mock->Stop();
   }
+
+  public function testAbsolutify()
+  {
+    $globals = array(
+      '_SERVER' => array(
+        'HTTP_HOST' => 'www.bluestatic.org',
+        'REQUEST_URI' => '/hoplite/webapp/test/path',
+        'PATH_INFO' => '/test/path',
+        'SERVER_PORT' => 80,
+      ),
+    );
+    $mock = new \hoplite\http\RootController($globals);
+
+    $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
+    $this->assertEquals($mock->MakeURL('/', TRUE), 'http://www.bluestatic.org/hoplite/webapp/');
+
+    $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
+    $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'http://www.bluestatic.org/hoplite/webapp/path/3');
+
+    $globals['_SERVER']['HTTPS'] = 'on';
+    $globals['_SERVER']['SERVER_PORT'] = 443;
+    $mock = new \hoplite\http\RootController($globals);
+    $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
+    $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org/hoplite/webapp/');
+
+    $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
+    $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org/hoplite/webapp/path/3');
+
+    $globals['_SERVER']['SERVER_PORT'] = 8080;
+    $mock = new \hoplite\http\RootController($globals);
+    $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
+    $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/');
+    $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/path/3');
+  }
 }