You are here

public function FeedsFileHTTPTestCase::testHttpRequestUsingFileCache in Feeds 7.2

Tests if the result of a http request can be cached on the file system.

When a first request is made, the data is retrieved from the given source url and cached on the file system. On the second request, send to the same source url, the HTTP header "If-Modified-Since" is set. This should result into a 304 HTTP status from the source, as the contents did not change in between. In such case, the data should be retrieved from the cache.

File

tests/feeds_fetcher_http.test, line 158
Contains FeedsFileHTTPTestCase.

Class

FeedsFileHTTPTestCase
HTTP fetcher test class.

Code

public function testHttpRequestUsingFileCache() {

  // Include http request functions.
  feeds_include_library('http_request.inc', 'http_request');

  // First request.
  $url = url('testing/feeds/nodes.csv', array(
    'absolute' => TRUE,
  ));
  $result = feeds_http_request($url);

  // Assert that the first request was successful and was not retrieved from
  // cache.
  $this
    ->assertEqual(200, $result->code, 'Request status code is 200.');
  $this
    ->assertTrue(empty($result->from_cache), 'Request was not retrieved from cache.');

  // Assert that a file was created.
  $file_url = 'private://feeds/cache/' . hash('sha256', $url);
  $this
    ->assertTrue(file_exists($file_url), format_string('The file @file_url exists.', array(
    '@file_url' => $file_url,
  )));

  // Reset feeds_http_request() static cache.
  drupal_static_reset('feeds_http_request');

  // Write some garbage to the cached file to ensure that the cache is
  // retrieved from that file.
  $garbage = static::randomString(100);
  file_put_contents($file_url, $garbage);

  // Second request.
  $result2 = feeds_http_request($url);

  // Assert that the second request was successful and was retrieved from
  // cache.
  $this
    ->assertEqual(200, $result2->code, 'Request status code is 200.');
  $this
    ->assertTrue(!empty($result2->from_cache), 'Request was retrieved from cache.');
  $this
    ->assertEqual($garbage, $result2->data, 'The cache came from the file cache.');

  // Assert that the file is removed when caches are cleared.
  drupal_flush_all_caches();
  $this
    ->assertFalse(file_exists($file_url), format_string('The file @file_url no longer exists.', array(
    '@file_url' => $file_url,
  )));
}