=== modified file 'includes/bootstrap.inc'
--- includes/bootstrap.inc	2009-12-07 07:25:24 +0000
+++ includes/bootstrap.inc	2009-12-10 18:54:43 +0000
@@ -1614,9 +1614,12 @@ function _drupal_bootstrap_database() {
   // The user agent header is used to pass a database prefix in the request when
   // running tests. However, for security reasons, it is imperative that we
   // validate we ourselves made the request.
-  if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], "simpletest") !== FALSE) && !drupal_valid_test_ua($_SERVER['HTTP_USER_AGENT'])) {
-    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
-    exit;
+  if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], "simpletest") !== FALSE)) {
+    if (!drupal_valid_test_ua($_SERVER['HTTP_USER_AGENT'])) {
+      header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
+      exit;
+    }
+    register_shutdown_function('_drupal_finish_request_test');
   }
   // Initialize the database system. Note that the connection
   // won't be initialized until it is actually requested.
@@ -2247,3 +2250,9 @@ function drupal_static_reset($name = NUL
 function drupal_is_cli() {
   return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
 }
+
+function _drupal_finish_request_test() {
+  if (drupal_get_http_header('Content-Type') == 'text/html; charset=utf-8') {
+    print chr(0);
+  }
+}

=== modified file 'modules/simpletest/drupal_web_test_case.php'
--- modules/simpletest/drupal_web_test_case.php	2009-12-06 18:06:22 +0000
+++ modules/simpletest/drupal_web_test_case.php	2009-12-10 19:14:57 +0000
@@ -1285,8 +1285,18 @@ class DrupalWebTestCase extends DrupalTe
     // Reset headers and the session ID.
     $this->session_id = NULL;
     $this->headers = array();
+    $response = curl_exec($this->curlHandle);
+    $headers = $this->drupalGetheaders();
+    if (isset($headers['Content-Type']) && $headers['Content-Type'] == 'text/html; charset=utf-8') {
+      if (substr($response, -1) !== chr(0)) {
+        $this->error('Fatal error', t('Browser'));
+      }
+      elseif (is_string($response)) {
+        $response = substr($response, 0, -1);
+      }
+    }
 
-    $this->drupalSetContent(curl_exec($this->curlHandle), curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
+    $this->drupalSetContent($response, curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
     $message_vars = array(
       '!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'),
       '@url' => $url,

