You are here

protected function RestfulRateLimitTestCase::roleExecute in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulRateLimitTestCase.test \RestfulRateLimitTestCase::roleExecute()

Tests the total amount of allowed calls and the following fail.

Parameters

object $account: The user account object.

int $limit: The number of calls allowed for a user with the same roles as $account.

array $resource_options: Array of options as received in restful_get_restful_handler.

2 calls to RestfulRateLimitTestCase::roleExecute()
RestfulRateLimitTestCase::testGlobalLimits in tests/RestfulRateLimitTestCase.test
Tests global rate limits.
RestfulRateLimitTestCase::testLimits in tests/RestfulRateLimitTestCase.test
Tests the rate limits and its expiration feature.

File

tests/RestfulRateLimitTestCase.test, line 91
Contains RestfulRateLimitTestCase.

Class

RestfulRateLimitTestCase

Code

protected function roleExecute($account, $limit, array $resource_options) {
  $resource_manager = restful()
    ->getResourceManager();
  $handler = $resource_manager
    ->getPlugin($resource_options[0] . ':' . $resource_options[1] . '.' . $resource_options[2]);
  $handler
    ->setAccount($account);

  // Test rate limits.
  $handler
    ->setRequest(Request::create(''));
  $handler
    ->setPath('');
  for ($count = 0; $count < $limit; $count++) {
    try {
      restful()
        ->getFormatterManager()
        ->format($handler
        ->process(), 'json');
      $this
        ->pass('The rate limit authorized the request.');
    } catch (FloodException $e) {
      $this
        ->fail('The rate limit did not authorize the request.');
    }
  }
  try {
    restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json');
    $this
      ->fail('The rate limit authorized the request.');
  } catch (FloodException $e) {
    $this
      ->pass('The rate limit did not authorize the request.');
    $headers = $e
      ->getHeaders();
    $this
      ->assertTrue(in_array('Retry-After', array_keys($headers)), 'Retry-After header found after rate limit exception.');
    $this
      ->assertTrue(new \DateTime($headers['Retry-After']) > new \DateTime(), 'Retry-After is set to a time in the future.');
  }
}