You are here

public function AdsTxtBasicTestCase::testAdsTxtCachingAndCompressionTestCase in AdsTxt 7

Test ads.txt file encoding with caching and compression.

File

./adstxt.test, line 98
Tests for adstxt.module.

Class

AdsTxtBasicTestCase
Tests basic functionality of configured ads.txt files.

Code

public function testAdsTxtCachingAndCompressionTestCase() {

  // Create an admin user, log in and access settings form.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer site configuration',
    'administer ads.txt',
  ));
  $this
    ->drupalLogin($admin_user);

  // Enable caching and compression.
  $edit = array();
  $edit['cache'] = 1;
  $edit['cache_lifetime'] = 60;
  $edit['page_cache_maximum_age'] = 60;
  $edit['page_compression'] = 1;
  $this
    ->drupalPost('admin/config/development/performance', $edit, t('Save configuration'));
  $this
    ->drupalLogout();
  $url = url('ads.txt', array(
    'absolute' => TRUE,
  ));

  // Use drupal_http_request so gzipped response is not automatically
  // uncompressed.
  $response = drupal_http_request($url, array(
    'headers' => array(
      'Accept-encoding' => 'gzip',
    ),
  ));
  $this
    ->assertEqual(trim(variable_get('adstxt', '')), trim(gzinflate(substr($response->data, 10, -8))), 'The ads.txt content is properly served with compression enabled.');

  // Note: the header may have charset appended.
  $header = $response->headers['content-type'];
  $this
    ->assertIdentical(strpos($header, 'text/plain'), 0, 'The ads.txt file was served with header Content-Type: text/plain');
}