summaryrefslogtreecommitdiff
path: root/modules/simpletest/drupal_web_test_case.php
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-12-02 20:03:57 +0000
committerDries Buytaert <dries@buytaert.net>2008-12-02 20:03:57 +0000
commit99afbc53a736ba30b294ed62611767345fe8c3dd (patch)
treefb6bb1133728048682b4dcc3d05f570e7c486367 /modules/simpletest/drupal_web_test_case.php
parent71a22f1c1f4da46a370dd11b9e31537f255c7375 (diff)
downloadbrdo-99afbc53a736ba30b294ed62611767345fe8c3dd.tar.gz
brdo-99afbc53a736ba30b294ed62611767345fe8c3dd.tar.bz2
- Patch #330582 by Darren Oh, c960657 et al: better API for retrieving HTTP headers and working with HTTP headers in tests.
Diffstat (limited to 'modules/simpletest/drupal_web_test_case.php')
-rw-r--r--modules/simpletest/drupal_web_test_case.php101
1 files changed, 98 insertions, 3 deletions
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index e5d043792..6ebf9d8df 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -28,6 +28,13 @@ class DrupalWebTestCase {
protected $curlHandle;
/**
+ * The headers of the page currently loaded in the internal browser.
+ *
+ * @var Array
+ */
+ protected $headers;
+
+ /**
* The content of the page currently loaded in the internal browser.
*
* @var string
@@ -923,6 +930,7 @@ class DrupalWebTestCase {
$this->curlInitialize();
$url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL];
curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
+ $this->headers = array();
$this->drupalSetContent(curl_exec($this->curlHandle), curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
$this->assertTrue($this->content !== FALSE, t('!method to !url, response is !length bytes.', array('!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'), '!url' => $url, '!length' => strlen($this->content))), t('Browser'));
return $this->drupalGetContent();
@@ -939,6 +947,7 @@ class DrupalWebTestCase {
* An header.
*/
protected function curlHeaderCallback($curlHandler, $header) {
+ $this->headers[] = $header;
// Errors are being sent via X-Drupal-Assertion-* headers,
// generated by _drupal_log_error() in the exact form required
// by DrupalWebTestCase::error().
@@ -1001,7 +1010,7 @@ class DrupalWebTestCase {
// We re-using a CURL connection here. If that connection still has certain
// options set, it might change the GET into a POST. Make sure we clear out
// previous options.
- $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_HEADER => FALSE, CURLOPT_NOBODY => FALSE));
+ $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_NOBODY => FALSE));
$this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up.
// Replace original page output with new output from redirected page(s).
@@ -1083,7 +1092,7 @@ class DrupalWebTestCase {
}
$post = implode('&', $post);
}
- $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HEADER => FALSE));
+ $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post));
// Ensure that any changes to variables in the other thread are picked up.
$this->refreshVariables();
@@ -1137,7 +1146,7 @@ class DrupalWebTestCase {
*/
protected function drupalHead($path, Array $options = array()) {
$options['absolute'] = TRUE;
- $out = $this->curlExec(array(CURLOPT_HEADER => TRUE, CURLOPT_NOBODY => TRUE, CURLOPT_URL => url($path, $options)));
+ $out = $this->curlExec(array(CURLOPT_NOBODY => TRUE, CURLOPT_URL => url($path, $options)));
$this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up.
return $out;
}
@@ -1421,6 +1430,92 @@ class DrupalWebTestCase {
}
/**
+ * Gets the HTTP response headers of the requested page. Normally we are only
+ * interested in the headers returned by the last request. However, if a page
+ * is redirected or HTTP authentication is in use, multiple requests will be
+ * required to retrieve the page. Headers from all requests may be requested
+ * by passing TRUE to this function.
+ *
+ * @param $all_requests
+ * Boolean value specifying whether to return headers from all requests
+ * instead of just the last request. Defaults to FALSE.
+ * @return
+ * A name/value array if headers from only the last request are requested.
+ * If headers from all requests are requested, an array of name/value
+ * arrays, one for each request.
+ *
+ * The pseudonym ":status" is used for the HTTP status line.
+ *
+ * Values for duplicate headers are stored as a single comma-separated list.
+ */
+ protected function drupalGetHeaders($all_requests = FALSE) {
+ $request = 0;
+ $headers = array($request => array());
+ foreach ($this->headers as $header) {
+ $header = trim($header);
+ if ($header === '') {
+ $request++;
+ }
+ else {
+ if (strpos($header, 'HTTP/') === 0) {
+ $name = ':status';
+ $value = $header;
+ }
+ else {
+ list($name, $value) = explode(':', $header, 2);
+ $name = strtolower($name);
+ }
+ if (isset($headers[$request][$name])) {
+ $headers[$request][$name] .= ',' . trim($value);
+ }
+ else {
+ $headers[$request][$name] = trim($value);
+ }
+ }
+ }
+ if (!$all_requests) {
+ $headers = array_pop($headers);
+ }
+ return $headers;
+ }
+
+ /**
+ * Gets the value of an HTTP response header. If multiple requests were
+ * required to retrieve the page, only the headers from the last request will
+ * be checked by default. However, if TRUE is passed as the second argument,
+ * all requests will be processed from last to first until the header is
+ * found.
+ *
+ * @param $name
+ * The name of the header to retrieve. Names are case-insensitive (see RFC
+ * 2616 section 4.2).
+ * @param $all_requests
+ * Boolean value specifying whether to check all requests if the header is
+ * not found in the last request. Defaults to FALSE.
+ * @return
+ * The HTTP header value or FALSE if not found.
+ */
+ protected function drupalGetHeader($name, $all_requests = FALSE) {
+ $name = strtolower($name);
+ $header = FALSE;
+ if ($all_requests) {
+ foreach (array_reverse($this->drupalGetHeaders(TRUE)) as $headers) {
+ if (isset($headers[$name])) {
+ $header = $headers[$name];
+ break;
+ }
+ }
+ }
+ else {
+ $headers = $this->drupalGetHeaders();
+ if (isset($headers[$name])) {
+ $header = $headers[$name];
+ }
+ }
+ return $header;
+ }
+
+ /**
* Gets the current raw HTML of requested page.
*/
protected function drupalGetContent() {