You are here

function CDNOriginPullTestCase::testMapping in CDN 7.2

File

tests/cdn.test, line 247
Test CDN.

Class

CDNOriginPullTestCase

Code

function testMapping() {
  $this
    ->setRequestProtocol('http');
  $this
    ->assertEqual('', cdn_basic_get_mapping(), 'The default CDN mapping is empty.');

  // Ensure the parsing of the raw mapping works correctly.
  $this
    ->assertMapping('', array(), array());
  $this
    ->assertMapping('http://cdn-a.com', array(
    '*' => array(
      'http://cdn-a.com',
    ),
  ), array(
    'cdn-a.com',
  ));
  $this
    ->assertMapping('http://cdn-a.com/', array(
    '*' => array(
      'http://cdn-a.com',
    ),
  ), array(
    'cdn-a.com',
  ));
  $this
    ->assertMapping('//cdn-a.com', array(
    '*' => array(
      '//cdn-a.com',
    ),
  ), array(
    'cdn-a.com',
  ));
  $this
    ->assertMapping('//cdn-a.com/', array(
    '*' => array(
      '//cdn-a.com',
    ),
  ), array(
    'cdn-a.com',
  ));
  $parsed_mapping = array(
    'css' => array(
      'http://cdn-a.com',
    ),
    'jpg' => array(
      'http://cdn-a.com',
    ),
    'jpeg' => array(
      'http://cdn-a.com',
    ),
    'png' => array(
      'http://cdn-a.com',
    ),
    'zip' => array(
      'http://cdn-b.com',
    ),
    '*' => array(
      'http://cdn-c.com',
    ),
  );
  $domains = array(
    'cdn-a.com',
    'cdn-b.com',
    'cdn-c.com',
  );
  $this
    ->assertMapping("http://cdn-a.com|.css .jpg .jpeg .png\nhttp://cdn-b.com|.zip\nhttp://cdn-c.com", $parsed_mapping, $domains);
  $parsed_mapping = array(
    'css' => array(
      'http://cdn-a.com',
      'http://cdn-d.com',
    ),
    'jpg' => array(
      'http://cdn-a.com',
      'http://cdn-d.com',
    ),
    'jpeg' => array(
      'http://cdn-a.com',
      'http://cdn-d.com',
    ),
    'png' => array(
      'http://cdn-a.com',
      'http://cdn-d.com',
      'http://cdn-b.com',
    ),
    '*' => array(
      'http://cdn-c.com',
    ),
  );
  $domains = array(
    'cdn-a.com',
    'cdn-b.com',
    'cdn-c.com',
    'cdn-d.com',
  );
  $this
    ->assertMapping("http://cdn-a.com http://cdn-d.com|.css .jpg .jpeg .png\nhttp://cdn-b.com|.png\nhttp://cdn-c.com", $parsed_mapping, $domains);

  // When a HTTPS request is performed and the CDN is not marked to support
  // HTTPS, then it should fall back to the default CDN mapping.
  $this
    ->setRequestProtocol('https');
  $this
    ->assertMapping(FALSE, $parsed_mapping, $domains);

  // When a HTTPS request is performed and the CDN *is* marked to support
  // HTTPS, then it should still fall back to the default CDN mapping. (When
  // file URLs are actually altered, it will then replace `http://` with
  // `https://` -- this will be tested in a different test.)
  $this
    ->configureHTTPS(TRUE);
  $this
    ->assertMapping(FALSE, $parsed_mapping, $domains);

  // When a HTTPS request is performed *and* the CDN is marked to support
  // HTTPS *and* there's a HTTPS-specific CDN mapping, that mapping should
  // be used instead!
  $this
    ->configureHTTPS(TRUE, "https://cdn-a.com|.css .jpg .jpeg .png\nhttps://cdn-b.com|.zip\nhttps://cdn-c.com");
  $this
    ->assertMapping(FALSE, array(
    'css' => array(
      'https://cdn-a.com',
    ),
    'jpg' => array(
      'https://cdn-a.com',
    ),
    'jpeg' => array(
      'https://cdn-a.com',
    ),
    'png' => array(
      'https://cdn-a.com',
    ),
    'zip' => array(
      'https://cdn-b.com',
    ),
    '*' => array(
      'https://cdn-c.com',
    ),
  ), array(
    'cdn-a.com',
    'cdn-b.com',
    'cdn-c.com',
  ));

  // Ensure the default CDN mapping is used whenever a HTTP request occurs
  // and the CDN is marked to suppport HTTPS and there's a HTTPS-specific
  // CDN mapping.
  $this
    ->configureHTTPS(FALSE);
  $this
    ->assertMapping(FALSE, $parsed_mapping, $domains);
}