Commit b5c0d297 authored by webchick's avatar webchick

Issue #1862398 by Berdir: Replace drupal_http_request() with Guzzle.

parent b77715c3
This diff is collapsed.
......@@ -81,11 +81,11 @@ public function testNodeCounterIntegration() {
// Manually calling statistics.php, simulating ajax behavior.
// @see \Drupal\statistics\Tests\StatisticsLoggingTest::testLogging().
$post = http_build_query(array('nid' => $this->node->id()));
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$client = \Drupal::httpClient();
$client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
$client->post($stats_path, array(), $post)->send();
$this->drupalGet('test_statistics_integration');
$expected = statistics_get($this->node->id());
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\HttpRequestTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Tests drupal_http_request().
*/
class HttpRequestTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system_test', 'language', 'test_page_test');
public static function getInfo() {
return array(
'name' => 'Drupal HTTP request',
'description' => "Performs tests on Drupal's HTTP request mechanism.",
'group' => 'Common',
);
}
/**
* Checks HTTP requests.
*/
function testDrupalHTTPRequest() {
global $is_https;
// Parse URL schema.
$missing_scheme = drupal_http_request('example.com/path');
$this->assertEqual($missing_scheme->code, -1002, 'Returned with "-1002" error code.');
$this->assertEqual($missing_scheme->error, 'missing schema', 'Returned with "missing schema" error message.');
$unable_to_parse = drupal_http_request('http:///path');
$this->assertEqual($unable_to_parse->code, -1001, 'Returned with "-1001" error code.');
$this->assertEqual($unable_to_parse->error, 'unable to parse URL', 'Returned with "unable to parse URL" error message.');
// Fetch the test page.
$result = drupal_http_request(url('test-page', array('absolute' => TRUE)));
$this->assertEqual($result->code, 200, 'Fetched page successfully.');
$this->drupalSetContent($result->data);
$this->assertTitle(t('Test page | @site-name', array('@site-name' => config('system.site')->get('name'))));
// Test that Drupal.settings is properly parsed.
$settings = $this->drupalGetSettings();
$this->assertIdentical($settings['test-setting'], 'azAZ09();.,\\\/-_{}');
// Test that code and status message is returned.
$result = drupal_http_request(url('pagedoesnotexist', array('absolute' => TRUE)));
$this->assertTrue(!empty($result->protocol), 'Result protocol is returned.');
$this->assertEqual($result->code, '404', 'Result code is 404');
$this->assertEqual($result->status_message, 'Not Found', 'Result status message is "Not Found"');
// Skip the timeout tests when the testing environment is HTTPS because
// stream_set_timeout() does not work for SSL connections.
// @link http://bugs.php.net/bug.php?id=47929
if (!$is_https) {
// Test that timeout is respected. The test machine is expected to be able
// to make the connection (i.e. complete the fsockopen()) in 2 seconds and
// return within a total of 5 seconds. If the test machine is extremely
// slow, the test will fail. fsockopen() has been seen to time out in
// slightly less than the specified timeout, so allow a little slack on
// the minimum expected time (i.e. 1.8 instead of 2).
timer_start(__METHOD__);
$result = drupal_http_request(url('system-test/sleep/10', array('absolute' => TRUE)), array('timeout' => 2));
$time = timer_read(__METHOD__) / 1000;
$this->assertTrue(1.8 < $time && $time < 5, format_string('Request timed out (%time seconds).', array('%time' => $time)));
$this->assertTrue($result->error, 'An error message was returned.');
$this->assertEqual($result->code, HTTP_REQUEST_TIMEOUT, 'Proper error code was returned.');
}
}
/**
* Tests HTTP basic authorization.
*/
function testDrupalHTTPRequestBasicAuth() {
$username = $this->randomName();
$password = $this->randomName();
$url = url('system-test/auth', array('absolute' => TRUE));
$auth = str_replace('://', '://' . $username . ':' . $password . '@', $url);
$result = drupal_http_request($auth);
$this->drupalSetContent($result->data);
$this->assertRaw($username, '$_SERVER["PHP_AUTH_USER"] is passed correctly.');
$this->assertRaw($password, '$_SERVER["PHP_AUTH_PW"] is passed correctly.');
}
/**
* Tests HTTP redirect requests.
*/
function testDrupalHTTPRequestRedirect() {
$redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($redirect_301->redirect_code, 301, 'drupal_http_request follows the 301 redirect.');
$redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array('max_redirects' => 0));
$this->assertFalse(isset($redirect_301->redirect_code), 'drupal_http_request does not follow 301 redirect if max_redirects = 0.');
$redirect_invalid = drupal_http_request(url('system-test/redirect-noscheme', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($redirect_invalid->code, -1002, format_string('301 redirect to invalid URL returned with error code !error.', array('!error' => $redirect_invalid->error)));
$this->assertEqual($redirect_invalid->error, 'missing schema', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
$redirect_invalid = drupal_http_request(url('system-test/redirect-noparse', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($redirect_invalid->code, -1001, format_string('301 redirect to invalid URL returned with error message code "!error".', array('!error' => $redirect_invalid->error)));
$this->assertEqual($redirect_invalid->error, 'unable to parse URL', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
$redirect_invalid = drupal_http_request(url('system-test/redirect-invalid-scheme', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($redirect_invalid->code, -1003, format_string('301 redirect to invalid URL returned with error code !error.', array('!error' => $redirect_invalid->error)));
$this->assertEqual($redirect_invalid->error, 'invalid schema ftp', format_string('301 redirect to invalid URL returned with error message "!error".', array('!error' => $redirect_invalid->error)));
$redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($redirect_302->redirect_code, 302, 'drupal_http_request follows the 302 redirect.');
$redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array('max_redirects' => 0));
$this->assertFalse(isset($redirect_302->redirect_code), 'drupal_http_request does not follow 302 redirect if $retry = 0.');
$redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($redirect_307->redirect_code, 307, 'drupal_http_request follows the 307 redirect.');
$redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array('max_redirects' => 0));
$this->assertFalse(isset($redirect_307->redirect_code), 'drupal_http_request does not follow 307 redirect if max_redirects = 0.');
$multiple_redirect_final_url = url('system-test/multiple-redirects/0', array('absolute' => TRUE));
$multiple_redirect_1 = drupal_http_request(url('system-test/multiple-redirects/1', array('absolute' => TRUE)), array('max_redirects' => 1));
$this->assertEqual($multiple_redirect_1->redirect_url, $multiple_redirect_final_url, 'redirect_url contains the final redirection location after 1 redirect.');
$multiple_redirect_3 = drupal_http_request(url('system-test/multiple-redirects/3', array('absolute' => TRUE)), array('max_redirects' => 3));
$this->assertEqual($multiple_redirect_3->redirect_url, $multiple_redirect_final_url, 'redirect_url contains the final redirection location after 3 redirects.');
}
/**
* Tests Content-language headers generated by Drupal.
*/
function testDrupalHTTPRequestHeaders() {
// Check the default header.
$request = drupal_http_request(url('<front>', array('absolute' => TRUE)));
$this->assertEqual($request->headers['content-language'], 'en', 'Content-Language HTTP header is English.');
// Add French language.
$language = new Language(array(
'langcode' => 'fr',
'name' => 'French',
));
language_save($language);
// Request front page in French and check for matching Content-language.
$request = drupal_http_request(url('<front>', array('absolute' => TRUE, 'language' => $language)));
$this->assertEqual($request->headers['content-language'], 'fr', 'Content-Language HTTP header is French.');
}
}
......@@ -4,12 +4,6 @@
* Implements hook_menu().
*/
function system_test_menu() {
$items['system-test/sleep/%'] = array(
'page callback' => 'system_test_sleep',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['system-test/auth'] = array(
'page callback' => 'system_test_basic_auth_page',
'access callback' => TRUE,
......@@ -21,41 +15,11 @@ function system_test_menu() {
'access arguments' => array('administer software updates'),
'type' => MENU_CALLBACK,
);
$items['system-test/redirect/%'] = array(
'title' => 'Redirect',
'page callback' => 'system_test_redirect',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['system-test/multiple-redirects/%'] = array(
'title' => 'Redirect',
'page callback' => 'system_test_multiple_redirects',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['system-test/set-header'] = array(
'page callback' => 'system_test_set_header',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['system-test/redirect-noscheme'] = array(
'page callback' => 'system_test_redirect_noscheme',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['system-test/redirect-noparse'] = array(
'page callback' => 'system_test_redirect_noparse',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['system-test/redirect-invalid-scheme'] = array(
'page callback' => 'system_test_redirect_invalid_scheme',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['system-test/variable-get'] = array(
'title' => 'Variable Get',
'page callback' => 'variable_get',
......@@ -109,70 +73,17 @@ function system_test_menu() {
return $items;
}
function system_test_sleep($seconds) {
sleep($seconds);
}
function system_test_basic_auth_page() {
$output = t('$_SERVER[\'PHP_AUTH_USER\'] is @username.', array('@username' => $_SERVER['PHP_AUTH_USER']));
$output .= t('$_SERVER[\'PHP_AUTH_PW\'] is @password.', array('@password' => $_SERVER['PHP_AUTH_PW']));
return $output;
}
function system_test_redirect($code) {
$code = (int) $code;
if ($code != 200) {
// Header names are case-insensitive.
header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
exit;
}
return '';
}
/**
* Menu callback; sends a redirect header to itself until $count argument is 0.
*
* Emulates the variable number of redirects (given by initial $count argument)
* to the final destination URL by continuous sending of 301 HTTP redirect
* headers to itself together with decrementing the $count parameter until the
* $count parameter reaches 0. After that it returns an empty string to render
* the final destination page.
*
* @param $count
* The count of redirects left until the final destination page.
*
* @returns
* The location redirect if the $count > 0, otherwise an empty string.
*/
function system_test_multiple_redirects($count) {
$count = (int) $count;
if ($count > 0) {
header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
exit;
}
return '';
}
function system_test_set_header() {
drupal_add_http_header($_GET['name'], $_GET['value']);
return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
}
function system_test_redirect_noscheme() {
header("Location: localhost/path", TRUE, 301);
exit;
}
function system_test_redirect_noparse() {
header("Location: http:///path", TRUE, 301);
exit;
}
function system_test_redirect_invalid_scheme() {
header("Location: ftp://localhost/path", TRUE, 301);
exit;
}
/**
* Implements hook_modules_installed().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment