You are here

protected function RestfulRateLimitTestCase::roleExecute in RESTful 7

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

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

Parameters

$account: The user account object.

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

$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 99
Contains RestfulRateLimitTestCase.

Class

RestfulRateLimitTestCase
@file Contains RestfulRateLimitTestCase.

Code

protected function roleExecute($account, $limit, $resource_options) {
  $handler = restful_get_restful_handler($resource_options[0], $resource_options[1], $resource_options[2]);
  $handler
    ->setAccount($account);

  // Test rate limits.
  for ($count = 0; $count < $limit; $count++) {
    try {
      $handler
        ->get('');
      $this
        ->pass('The rate limit authorized the request.');
    } catch (\RestfulFloodException $e) {
      $this
        ->fail('The rate limit did not authorize the request.');
    }
  }
  try {
    $handler
      ->get('');
    $this
      ->fail('The rate limit authorized the request.');
  } catch (\RestfulFloodException $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.');
  }
}