From 32981b119b076014d3f5774020f115b4811d795f Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Tue, 2 Sep 2008 17:00:34 +0000 Subject: [PATCH] - Patch #296310 by domas, dmitrig01, boombatower: drupal_http_request tests. --- modules/simpletest/tests/common.test | 62 +++++++++++++++- modules/simpletest/tests/system_test.info | 8 +++ modules/simpletest/tests/system_test.module | 78 +++++++++++++++++++++ 3 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 modules/simpletest/tests/system_test.info create mode 100644 modules/simpletest/tests/system_test.module diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index dc76829e5cd..25c15ab1eb3 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -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().')); + } } diff --git a/modules/simpletest/tests/system_test.info b/modules/simpletest/tests/system_test.info new file mode 100644 index 00000000000..f70a1a79fa0 --- /dev/null +++ b/modules/simpletest/tests/system_test.info @@ -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 diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module new file mode 100644 index 00000000000..60b1931524f --- /dev/null +++ b/modules/simpletest/tests/system_test.module @@ -0,0 +1,78 @@ + '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(); +}