- Patch #296310 by domas, dmitrig01, boombatower: drupal_http_request tests.

merge-requests/26/head
Dries Buytaert 2008-09-02 17:00:34 +00:00
parent 182798caa4
commit 32981b119b
3 changed files with 146 additions and 2 deletions

View File

@ -129,13 +129,20 @@ class DrupalHTTPRequestTestCase extends DrupalWebTestCase {
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp('system_test');
}
function testDrupalHTTPRequest() {
// Parse URL schema.
$missing_scheme = drupal_http_request('example.com/path');
$this->assertEqual($missing_scheme->error, 'missing schema', t('Returned with missing scheme error.'));
$this->assertEqual($missing_scheme->error, 'missing schema', t('Returned with "missing schema" error.'));
$unable_to_parse = drupal_http_request('http:///path');
$this->assertEqual($unable_to_parse->error, 'unable to parse URL', t('Returned with unable to parse URL error.'));
$this->assertEqual($unable_to_parse->error, 'unable to parse URL', t('Returned with "unable to parse URL" error.'));
// Fetch page.
$result = drupal_http_request(url('node', array('absolute' => TRUE)));
@ -143,4 +150,55 @@ class DrupalHTTPRequestTestCase extends DrupalWebTestCase {
$this->drupalSetContent($result->data);
$this->assertTitle(variable_get('site_name', 'Drupal'), t('Site title matches.'));
}
function testDrupalHTTPRequestBasicAuth() {
$username = $this->randomName();
$password = $this->randomName();
$url = url('system-test/auth', array('absolute' => TRUE));
$auth = str_replace('http://', 'http://' . $username . ':' . $password .'@', $url);
$result = drupal_http_request($auth);
// We use strpos directly.
// assertRaw() cannot be used because we are getting the data
// in a variable instead of $this->_content.
$this->assertTrue(strpos($result->data, $username) !== FALSE, t('$_SERVER[\'PHP_AUTH_USER\'] is passed correctly.'));
$this->assertTrue(strpos($result->data, $password) !== FALSE, t('$_SERVER[\'PHP_AUTH_PW\'] is passed correctly.'));
}
function testDrupalHTTPRequestRedirect() {
$redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_301->redirect_code, 200, t('drupal_http_request follows the 301 redirect.'));
$redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
$this->assertFalse(isset($redirect_301->redirect_code), t('drupal_http_request does not follow 301 redirect if $retry = 0.'));
$redirect_invalid = drupal_http_request(url('system-test/redirect-noscheme', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_invalid->error, 'missing schema', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));
$redirect_invalid = drupal_http_request(url('system-test/redirect-noparse', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_invalid->error, 'unable to parse URL', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));
$redirect_invalid = drupal_http_request(url('system-test/redirect-invalid-scheme', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_invalid->error, 'invalid schema ftp', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));
$redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_302->redirect_code, 200, t('drupal_http_request follows the 302 redirect.'));
$redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
$this->assertFalse(isset($redirect_302->redirect_code), t('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(), 'GET', NULL, 1);
$this->assertEqual($redirect_307->redirect_code, 200, t('drupal_http_request follows the 307 redirect.'));
$redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
$this->assertFalse(isset($redirect_307->redirect_code), t('drupal_http_request does not follow 307 redirect if $retry = 0.'));
}
function testDrupalGetDestination() {
$query = $this->randomName(10);
$url = url('system-test/destination', array('absolute' => TRUE, 'query' => $query));
$this->drupalGet($url);
$this->assertText($query, t('The query passed to the page is correctly represented by drupal_get_detination().'));
}
}

View File

@ -0,0 +1,8 @@
; $Id$
name = System test
description = Support module for system testing.
package = Testing
version = VERSION
core = 7.x
files[] = system_test.module
hidden = TRUE

View File

@ -0,0 +1,78 @@
<?php
// $Id$
/**
* Implementation of hook_menu().
*/
function system_test_menu() {
$items['system-test/auth'] = array(
'page callback' => 'system_test_basic_auth_page',
'access callback' => TRUE,
'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/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/destination'] = array(
'title' => 'Redirect',
'page callback' => 'system_test_destination',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
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("Location: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
exit;
}
return '';
}
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;
}
function system_test_destination() {
return 'The destination: ' . drupal_get_destination();
}