Esempio n. 1
0
 /**
  * Get information about the current rate
  *
  * @param  string $key
  * @return RateLimitInfo Rate limit information
  */
 public function getRateInfo($key)
 {
     $info = $this->rates[$key];
     $rateLimitInfo = new RateLimitInfo();
     $rateLimitInfo->setCalls($info['calls']);
     $rateLimitInfo->setResetTimestamp($info['reset']);
     $rateLimitInfo->setLimit($info['limit']);
     return $rateLimitInfo;
 }
 public function testRateInfoSetters()
 {
     $rateInfo = new RateLimitInfo();
     $rateInfo->setLimit(1234);
     $this->assertEquals(1234, $rateInfo->getLimit());
     $rateInfo->setCalls(5);
     $this->assertEquals(5, $rateInfo->getCalls());
     $rateInfo->setResetTimestamp(100000);
     $this->assertEquals(100000, $rateInfo->getResetTimestamp());
 }
 public function testListenerRemainingCannotBeNegative()
 {
     $rateLimitInfo = new RateLimitInfo();
     $rateLimitInfo->setCalls(500);
     $rateLimitInfo->setLimit(10);
     $rateLimitInfo->setResetTimestamp(1520000);
     $event = $this->createEvent();
     $event->getRequest()->attributes->set('rate_limit_info', $rateLimitInfo);
     $listener = new HeaderModificationListener();
     $listener->setParameter('display_headers', true);
     $listener->setParameter('header_limit_name', 'X-RateLimit-Limit');
     $listener->setParameter('header_remaining_name', 'X-RateLimit-Remaining');
     $listener->setParameter('header_reset_name', 'X-RateLimit-Reset');
     $listener->onKernelResponse($event);
     $this->assertEquals(0, $event->getResponse()->headers->get('X-RateLimit-Remaining'));
 }