You are here

public function HttpResponseHeadersUITestCase::testHeaderRuleHelpers in HTTP Response Headers 7

Test header rule helpers that changes the user input.

File

modules/http_response_headers_ui/http_response_headers_ui.test, line 171
Tests for http_response_headers_ui.module.

Class

HttpResponseHeadersUITestCase
@file Tests for http_response_headers_ui.module.

Code

public function testHeaderRuleHelpers() {

  // Create a header rule with 'Expires' header.
  $name = $this
    ->randomName(8);
  $visibility_options = array(
    'pages' => 'user*',
    'visibility' => HTTP_RESPONSE_HEADERS_VISIBILITY_LISTED,
  );
  $header = 'Expires';
  $header_value = '3600';
  $this
    ->createHeaderRule(strtolower($name), $name, $header, $header_value, $visibility_options);

  // Get user page to check Expires has
  // 'Mon, 14 Oct 2013 15:42:25 +0000' format value, instead of 3600.
  $this
    ->drupalGet('user');
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers[strtolower($header)], http_response_headers_expires_callback($header_value), 'Expires header was applied to user pages.');

  // Create a header rule with 'Last-Modified' header.
  $name = $this
    ->randomName(8);
  $visibility_options = array(
    'pages' => 'user*',
    'visibility' => HTTP_RESPONSE_HEADERS_VISIBILITY_LISTED,
  );
  $header = 'Last-Modified';
  $header_value = '3600';
  $this
    ->createHeaderRule(strtolower($name), $name, $header, $header_value, $visibility_options);

  // Get user page to check Last-Modified has
  // 'Mon, 14 Oct 2013 15:42:25 GMT' format value, instead of 3600.
  // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified
  $this
    ->drupalGet('user');
  $headers = $this
    ->drupalGetHeaders();
  $rfc7231 = preg_match('/[A-Z][a-z]{2}, [0-9]{1,2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT/', $headers[strtolower($header)]);
  $this
    ->assertTrue((bool) $rfc7231, 'Last-Modified header was applied to user pages.');

  // Check the Last-Modified header matched the Date header. This is somewhat
  // reliant on the webserver, but most seem to follow the spec.
  // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date
  // N.B. this test is only valid for an uncached response.
  // @see http_response_headers_last_modified_callback()
  $this
    ->assertEqual($headers[strtolower($header)], $headers[strtolower('Date')], 'Last-Modified header matched the Date response header.');
}