Skip to content
Snippets Groups Projects
http_request.test 9.12 KiB
Newer Older
<?php

/**
 * @file
 * Tests for http_request.inc.
 */

/**
 * Tests for the http library.
 */
class FeedsHTTPRequestTestCase extends FeedsUnitTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'HTTP library',
      'description' => 'Tests for Feeds HTTP library.',
      'group' => 'Feeds',
    );
  }

  public function setUp() {
    parent::setUp();
    feeds_include_library('http_request.inc', 'http_request');
  }

  /**
   * Tests http_request_find_feeds().
   */
  public function testHTTPRequestFindFeeds() {
    $html = <<<EOF
<html>
  <head>
    <title>Welcome to Example.com</title>
    <link rel="stylesheet" type="text/css" media="screen, projection" href="/stuff.css" >
    <link rel="search"    title="Something" href="//example.com/search">
    <link rel="alternate" title="Something RSS" href="http://example.com/rss.xml" type="application/rss+xml">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  </head>
  <body>
    This is a body.
  </body>
</html
EOF;

    $links = http_request_find_feeds($html);
    $this->assertEqual(count($links), 1);
    $this->assertEqual($links[0], 'http://example.com/rss.xml');

    // Test single quoted HTML.
    $links = http_request_find_feeds(str_replace('"', "'", $html));
    $this->assertEqual(count($links), 1);
    $this->assertEqual($links[0], 'http://example.com/rss.xml');
  }

  /**
   * Tests http_request_create_absolute_url().
   */
  public function testHTTPRequestCreateAbsoluteUrl() {
    $test_urls = array(
      // Rels that do not start with "/".
      array(
        'rel'    => 'h',
        'base'   => 'http://www',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => 'h',
        'base'   => 'http://www/',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => 'h',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => 'h',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/a/h',
      ),

      array(
        'rel'    => 'h/j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => 'h/j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => 'h/j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => 'h/j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/a/h/j',
      ),
      array(
        'rel'    => 'h/j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/a/b/h/j',
      ),

      // Rels that start with "/".
      array(
        'rel'    => '/h',
        'base'   => 'http://www',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => '/h',
        'base'   => 'http://www/',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => '/h',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => '/h',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/h',
      ),

      array(
        'rel'    => '/h/j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/h/j',
      ),

      // Rels that contain ".".
      array(
        'rel'    => './h',
        'base'   => 'http://www',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => './h',
        'base'   => 'http://www/',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => './h',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h',
      ),
      array(
        'rel'    => './h',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/a/h',
      ),

      array(
        'rel'    => './h/j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => './h/j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => './h/j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => './h/j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/a/h/j',
      ),
      array(
        'rel'    => './h/j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/a/b/h/j',
      ),

      array(
        'rel'    => 'h/./j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => 'h/./j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => 'h/./j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => 'h/./j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/a/h/j',
      ),
      array(
        'rel'    => 'h/./j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/a/b/h/j',
      ),

      array(
        'rel'    => '/h/./j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/./j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/./j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/./j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '/h/./j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/h/j',
      ),

      // Rels that starts with "../".
      array(
        'rel'    => '../h/j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../h/j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../h/j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../h/j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../h/j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/a/h/j',
      ),
      array(
        'rel'    => '../h/j',
        'base'   => 'http://www/a/b/c/',
        'expected' => 'http://www/a/b/h/j',
      ),

      // Rels that start with "../../".
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www/?c;d=e#f',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www/a/b',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www/a/b/',
        'expected' => 'http://www/h/j',
      ),
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www/a/b/c/',
        'expected' => 'http://www/a/h/j',
      ),
      array(
        'rel'    => '../../h/j',
        'base'   => 'http://www/a/b/c/d',
        'expected' => 'http://www/a/h/j',
      ),

      // Crazy rels.
      array(
        'rel'    => 'h/../../j/./k',
        'base'   => 'http://www/a/b/c/',
        'expected' => 'http://www/a/b/j/k',
      ),
      array(
        'rel'    => 'h/../../j/./k',
        'base'   => 'http://www/a/b/c/d',
        'expected' => 'http://www/a/b/j/k',
      ),
      array(
        'rel'    => '../../../',
        'base'   => 'http://www/a/b/c/',
        'expected' => 'http://www/',
      ),
      array(
        'rel'    => 'h/j/k/../../',
        'base'   => 'http://www/a/b/c/',
        'expected' => 'http://www/a/b/c/h',
      ),
    );

    foreach ($test_urls as $test_url) {
      $result_url = http_request_create_absolute_url($test_url['rel'], $test_url['base']);
      $this->assertEqual($test_url['expected'], $result_url, format_string('Creating an absolute URL from base @base and rel @rel resulted into @expected (actual: @actual).', array(
        '@actual' => var_export($result_url, TRUE),
        '@expected' => var_export($test_url['expected'], TRUE),
        '@rel' => var_export($test_url['rel'], TRUE),
        '@base' => var_export($test_url['base'], TRUE),
      )));
    }

  }