2012-08-26 06:49:11 +08:00
|
|
|
<?php
|
|
|
|
class trafficlimiterTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2013-11-01 08:15:14 +08:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'trafficlimit';
|
2012-08-26 06:49:11 +08:00
|
|
|
trafficlimiter::setPath($this->_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2013-11-01 08:15:14 +08:00
|
|
|
helper::rmdir($this->_path . DIRECTORY_SEPARATOR);
|
2012-08-26 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTrafficGetsLimited()
|
|
|
|
{
|
|
|
|
trafficlimiter::setLimit(4);
|
|
|
|
$this->assertTrue(trafficlimiter::canPass('127.0.0.1'), 'first request may pass');
|
|
|
|
sleep(2);
|
|
|
|
$this->assertFalse(trafficlimiter::canPass('127.0.0.1'), 'second request is to fast, may not pass');
|
|
|
|
sleep(3);
|
|
|
|
$this->assertTrue(trafficlimiter::canPass('127.0.0.1'), 'third request waited long enough and may pass');
|
|
|
|
$this->assertTrue(trafficlimiter::canPass('2001:1620:2057:dead:beef::cafe:babe'), 'fourth request has different ip and may pass');
|
|
|
|
$this->assertFalse(trafficlimiter::canPass('127.0.0.1'), 'fifth request is to fast, may not pass');
|
|
|
|
}
|
|
|
|
}
|