From ab4e39da832da4929df3c0a327dffb657f24b544 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 13 Aug 2008 07:11:18 +0000 Subject: - Patch #283806 by mustafau: fixed bug in drupal_http_request() and added some first drupal_http_request() tests to core. Yay. --- includes/common.inc | 8 +- includes/tests/common.test | 115 ----------------------------- modules/simpletest/tests/common.test | 139 +++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 119 deletions(-) delete mode 100644 includes/tests/common.test create mode 100644 modules/simpletest/tests/common.test diff --git a/includes/common.inc b/includes/common.inc index e791deca2..7a61a7512 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -436,13 +436,13 @@ function drupal_http_request($url, $headers = array(), $method = 'GET', $data = // Parse the URL and make sure we can handle the schema. $uri = parse_url($url); - if (!isset($uri['scheme'])) { - $result->error = 'missing schema'; + if ($uri == FALSE) { + $result->error = 'unable to parse URL'; return $result; } - if (!isset($uri['host'])) { - $result->error = 'missing host'; + if (!isset($uri['scheme'])) { + $result->error = 'missing schema'; return $result; } diff --git a/includes/tests/common.test b/includes/tests/common.test deleted file mode 100644 index 2fed68527..000000000 --- a/includes/tests/common.test +++ /dev/null @@ -1,115 +0,0 @@ - t('Format size test'), - 'description' => t('Parse a predefined amount of bytes and compare the output with the expected value.'), - 'group' => t('System') - ); - } - - /** - * Implementation of setUp(). - */ - function setUp() { - $this->exact_test_cases = array( - '1 byte' => 1, // byte - '1 KB' => 1000, // kilobyte - '1 MB' => 1000000, // megabyte - '1 GB' => 1000000000, // gigabyte - '1 TB' => 1000000000000, // terabyte - '1 PB' => 1000000000000000, // petabyte - '1 EB' => 1000000000000000000, // exabyte - '1 ZB' => 1000000000000000000000, // zettabyte - '1 YB' => 1000000000000000000000000, // yottabyte - ); - $this->rounded_test_cases = array( - '2 bytes' => 2, // bytes - '1 MB' => 999999, // 1 MB (not 1000 kilobyte!) - '3.62 MB' => 3623651, // megabytes - '67.23 PB' => 67234178751368124, // petabytes - '235.35 YB' => 235346823821125814962843827, // yottabytes - ); - parent::setUp(); - } - - /** - * testCommonFormatSize - */ - function testCommonFormatSize() { - foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) { - foreach ($test_cases as $expected => $size) { - $this->assertTrue( - ($result = format_size($size, NULL)) == $expected, - $expected . " == " . $result . " (" . $size . " bytes) %s" - ); - } - } - } -} - -/** - * Test drupal_explode_tags() and drupal_implode_tags(). - */ -class DrupalTagsHandlingTestCase extends DrupalWebTestCase { - var $validTags = array( - 'Drupal' => 'Drupal', - 'Drupal with some spaces' => 'Drupal with some spaces', - '"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"', - '"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!', - ); - - /** - * Implementation of getInfo(). - */ - function getInfo() { - return array( - 'name' => t('Drupal tags handling'), - 'description' => t("Performs tests on Drupal's handling of tags, both explosion and implosion tactics used."), - 'group' => t('System') - ); - } - - /** - * Explode a series of tags. - */ - function testDrupalExplodeTags() { - $string = implode(', ', array_keys($this->validTags)); - $tags = drupal_explode_tags($string); - $this->assertTags($tags); - } - - /** - * Implode a series of tags. - */ - function testDrupalImplodeTags() { - $tags = array_values($this->validTags); - // Let's explode and implode to our heart's content. - for ($i = 0; $i < 10; $i++) { - $string = drupal_implode_tags($tags); - $tags = drupal_explode_tags($string); - } - $this->assertTags($tags); - } - - /** - * Helper function: asserts that the ending array of tags is what we wanted. - */ - function assertTags($tags) { - $original = $this->validTags; - foreach ($tags as $tag) { - $key = array_search($tag, $original); - $this->_assert($key !== FALSE, t('Make sure tag %tag shows up in the final tags array (originally %original)', array('%tag' => $tag, '%original' => $key))); - unset($original[$key]); - } - foreach ($original as $leftover) { - $this->_assert(FALSE, t('Leftover tag %leftover was left over.', array('%leftover' => $leftover))); - } - } -} \ No newline at end of file diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test new file mode 100644 index 000000000..d9c6ceaa1 --- /dev/null +++ b/modules/simpletest/tests/common.test @@ -0,0 +1,139 @@ + t('Format size test'), + 'description' => t('Parse a predefined amount of bytes and compare the output with the expected value.'), + 'group' => t('System') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + $this->exact_test_cases = array( + '1 byte' => 1, // byte + '1 KB' => 1000, // kilobyte + '1 MB' => 1000000, // megabyte + '1 GB' => 1000000000, // gigabyte + '1 TB' => 1000000000000, // terabyte + '1 PB' => 1000000000000000, // petabyte + '1 EB' => 1000000000000000000, // exabyte + '1 ZB' => 1000000000000000000000, // zettabyte + '1 YB' => 1000000000000000000000000, // yottabyte + ); + $this->rounded_test_cases = array( + '2 bytes' => 2, // bytes + '1 MB' => 999999, // 1 MB (not 1000 kilobyte!) + '3.62 MB' => 3623651, // megabytes + '67.23 PB' => 67234178751368124, // petabytes + '235.35 YB' => 235346823821125814962843827, // yottabytes + ); + parent::setUp(); + } + + /** + * testCommonFormatSize + */ + function testCommonFormatSize() { + foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) { + foreach ($test_cases as $expected => $size) { + $this->assertTrue( + ($result = format_size($size, NULL)) == $expected, + $expected . " == " . $result . " (" . $size . " bytes) %s" + ); + } + } + } +} + +/** + * Test drupal_explode_tags() and drupal_implode_tags(). + */ +class DrupalTagsHandlingTestCase extends DrupalWebTestCase { + var $validTags = array( + 'Drupal' => 'Drupal', + 'Drupal with some spaces' => 'Drupal with some spaces', + '"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"', + '"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!', + ); + + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Drupal tags handling'), + 'description' => t("Performs tests on Drupal's handling of tags, both explosion and implosion tactics used."), + 'group' => t('System') + ); + } + + /** + * Explode a series of tags. + */ + function testDrupalExplodeTags() { + $string = implode(', ', array_keys($this->validTags)); + $tags = drupal_explode_tags($string); + $this->assertTags($tags); + } + + /** + * Implode a series of tags. + */ + function testDrupalImplodeTags() { + $tags = array_values($this->validTags); + // Let's explode and implode to our heart's content. + for ($i = 0; $i < 10; $i++) { + $string = drupal_implode_tags($tags); + $tags = drupal_explode_tags($string); + } + $this->assertTags($tags); + } + + /** + * Helper function: asserts that the ending array of tags is what we wanted. + */ + function assertTags($tags) { + $original = $this->validTags; + foreach ($tags as $tag) { + $key = array_search($tag, $original); + $this->_assert($key !== FALSE, t('Make sure tag %tag shows up in the final tags array (originally %original)', array('%tag' => $tag, '%original' => $key))); + unset($original[$key]); + } + foreach ($original as $leftover) { + $this->_assert(FALSE, t('Leftover tag %leftover was left over.', array('%leftover' => $leftover))); + } + } +} + +/** + * Test drupal_http_request(). + */ +class DrupalHTTPRequestTestCase extends DrupalWebTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Drupal HTTP request'), + 'description' => t("Performs tests on Drupal's HTTP request mechanism."), + 'group' => t('System') + ); + } + + function testDrupalHTTPRequest() { + $missing_scheme = drupal_http_request('example.com/path'); + $this->assertEqual($missing_scheme->error, 'missing schema', t('Returned with missing scheme 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.')); + } +} -- cgit v1.2.3