Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.399
diff -u -p -r1.399 bootstrap.inc
--- includes/bootstrap.inc	14 Jun 2010 13:24:32 -0000	1.399
+++ includes/bootstrap.inc	16 Jun 2010 19:21:03 -0000
@@ -456,21 +456,23 @@ function conf_path($require_settings = T
  * @see ip_address()
  */
 function drupal_override_server_variables($variables = array()) {
-  // Set defaults based on the provided URL.
+  // Allow the provided URL to override any existing values in $_SERVER.
   if (isset($variables['url'])) {
     $url = parse_url($variables['url']);
+    if (isset($url['host'])) {
+      $_SERVER['HTTP_HOST'] = $url['host'];
+    }
+    if (isset($url['path'])) {
+      $_SERVER['SCRIPT_NAME'] = $url['path'];
+    }
     unset($variables['url']);
   }
-  else {
-    $url = array();
-  }
-  $url += array(
-    'path' => '',
-    'host' => 'localhost',
-  );
+  // Define default values for $_SERVER keys. These will be used if $_SERVER
+  // does not already define them and no other values are passed in to this
+  // function.
   $defaults = array(
-    'HTTP_HOST' => $url['host'],
-    'SCRIPT_NAME' => $url['path'],
+    'HTTP_HOST' => 'localhost',
+    'SCRIPT_NAME' => NULL,
     'REMOTE_ADDR' => '127.0.0.1',
     'REQUEST_METHOD' => 'GET',
     'SERVER_NAME' => NULL,
Index: modules/simpletest/tests/bootstrap.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/bootstrap.test,v
retrieving revision 1.31
diff -u -p -r1.31 bootstrap.test
--- modules/simpletest/tests/bootstrap.test	14 Jun 2010 13:24:32 -0000	1.31
+++ modules/simpletest/tests/bootstrap.test	16 Jun 2010 19:21:03 -0000
@@ -426,3 +426,50 @@ class BootstrapResettableStaticTestCase 
     $this->assertEqual($var, 'foo', t('Variable was reset after second invocation of global reset.'));
   }
 }
+
+/**
+ * Tests for overriding server variables via the API.
+ */
+class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Overriding server variables',
+      'description' => 'Test that drupal_override_server_variables() works correctly.',
+      'group' => 'Bootstrap',
+    );
+  }
+
+  /**
+   * Test providing a direct URL to to drupal_override_server_variables().
+   */
+  function testDrupalOverrideServerVariablesProvidedURL() {
+    $tests = array(
+      'http://example.com' => array(
+        'HTTP_HOST' => 'example.com',
+        'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
+      ),
+      'http://example.com/index.php' => array(
+        'HTTP_HOST' => 'example.com',
+        'SCRIPT_NAME' => '/index.php',
+      ),
+      'http://example.com/subdirectory/index.php' => array(
+        'HTTP_HOST' => 'example.com',
+        'SCRIPT_NAME' => '/subdirectory/index.php',
+      ),
+    );
+    foreach ($tests as $url => $expected_server_values) {
+      // Remember the original value of $_SERVER, since the function call below
+      // will modify it.
+      $original_server = $_SERVER;
+      // Call drupal_override_server_variables() and ensure that all expected
+      // $_SERVER variables were modified correctly.
+      drupal_override_server_variables(array('url' => $url));
+      foreach ($expected_server_values as $key => $value) {
+        $this->assertIdentical($_SERVER[$key], $value);
+      }
+      // Restore the original value of $_SERVER.
+      $_SERVER = $original_server;
+    }
+  }
+}
+
