-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rate limiter refactor test #13994
Merged
SilverFire
merged 16 commits into
yiisoft:master
from
vladis84:rate-limiter-refactor-test
Apr 24, 2017
Merged
Rate limiter refactor test #13994
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
36705f0
Test write for yii\filters\RateLimiter and refactoring
vladis84 4509ea3
fix tests
vladis84 a085ce8
fix tests
vladis84 6cc0633
fix tests
vladis84 723fabe
fix tests
vladis84 3e3c0f1
fix tests
vladis84 a0bbcd2
fix tests
vladis84 f54492b
fix tests
vladis84 dd12adb
add phpdoc
vladis84 2211d0a
Adjusted wording [skip ci]
samdark ca19b05
Adjustments according to @cebe suggestions
samdark 8b0d38a
Merge branch 'master' into rate-limiter-refactor-test
vladis84 cc51e94
Added quotes [skip ci]
samdark 7605737
Got removed changelog line back
samdark 704ff5c
update phpdoc
cebe 767cf0f
fixed spaces
cebe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace yiiunit\framework\filters; | ||
|
||
use Yii; | ||
use yiiunit\TestCase; | ||
use Prophecy\Argument; | ||
use yiiunit\framework\filters\stubs\RateLimit; | ||
use yii\web\User; | ||
use yii\web\Request; | ||
use yii\web\Response; | ||
use yii\log\Logger; | ||
use yii\filters\RateLimiter; | ||
|
||
/** | ||
* @group filters | ||
*/ | ||
class RateLimiterTest extends TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
/* @var $logger Logger|\Prophecy\ObjectProphecy */ | ||
$logger = $this->prophesize(Logger::className()); | ||
$logger | ||
->log(Argument::any(), Argument::any(), Argument::any()) | ||
->will(function ($parameters, $logger) { | ||
$logger->messages = $parameters; | ||
}); | ||
|
||
Yii::setLogger($logger->reveal()); | ||
|
||
$this->mockWebApplication(); | ||
} | ||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
Yii::setLogger(null); | ||
} | ||
|
||
public function testInitFilledRequest() | ||
{ | ||
$rateLimiter = new RateLimiter(['request' => 'Request']); | ||
|
||
$this->assertEquals('Request', $rateLimiter->request); | ||
} | ||
|
||
public function testInitNotFilledRequest() | ||
{ | ||
$rateLimiter = new RateLimiter(); | ||
|
||
$this->assertInstanceOf(Request::className(), $rateLimiter->request); | ||
} | ||
|
||
public function testInitFilledResponse() | ||
{ | ||
$rateLimiter = new RateLimiter(['response' => 'Response']); | ||
|
||
$this->assertEquals('Response', $rateLimiter->response); | ||
} | ||
|
||
public function testInitNotFilledResponse() | ||
{ | ||
$rateLimiter = new RateLimiter(); | ||
|
||
$this->assertInstanceOf(Response::className(), $rateLimiter->response); | ||
} | ||
|
||
public function testBeforeActionUserInstanceOfRateLimitInterface() | ||
{ | ||
$rateLimiter = new RateLimiter(); | ||
$rateLimit = new RateLimit(); | ||
$rateLimit->setAllowance([1, time()]) | ||
->setRateLimit([1, 1]); | ||
$rateLimiter->user = $rateLimit; | ||
|
||
$result = $rateLimiter->beforeAction('test'); | ||
|
||
$this->assertContains('Check rate limit', Yii::getLogger()->messages); | ||
$this->assertTrue($result); | ||
} | ||
|
||
public function testBeforeActionUserNotInstanceOfRateLimitInterface() | ||
{ | ||
$rateLimiter = new RateLimiter(['user' => 'User']); | ||
|
||
$result = $rateLimiter->beforeAction('test'); | ||
|
||
$this->assertContains('Rate limit skipped: "user" does not implement RateLimitInterface.', Yii::getLogger()->messages); | ||
$this->assertTrue($result); | ||
} | ||
|
||
public function testBeforeActionEmptyUser() | ||
{ | ||
$user = new User(['identityClass' => RateLimit::className()]); | ||
Yii::$app->set('user', $user); | ||
$rateLimiter = new RateLimiter(); | ||
|
||
$result = $rateLimiter->beforeAction('test'); | ||
|
||
$this->assertContains('Rate limit skipped: user not logged in.', Yii::getLogger()->messages); | ||
$this->assertTrue($result); | ||
} | ||
|
||
public function testCheckRateLimitTooManyRequests() | ||
{ | ||
/* @var $rateLimit UserIdentity|\Prophecy\ObjectProphecy */ | ||
$rateLimit = new RateLimit; | ||
$rateLimit | ||
->setRateLimit([1, 1]) | ||
->setAllowance([1, time() + 2]); | ||
$rateLimiter = new RateLimiter(); | ||
|
||
$this->setExpectedException('yii\web\TooManyRequestsHttpException'); | ||
$rateLimiter->checkRateLimit($rateLimit, Yii::$app->request, Yii::$app->response, 'testAction'); | ||
} | ||
|
||
public function testCheckRateaddRateLimitHeaders() | ||
{ | ||
/* @var $user UserIdentity|\Prophecy\ObjectProphecy */ | ||
$rateLimit = new RateLimit; | ||
$rateLimit | ||
->setRateLimit([1, 1]) | ||
->setAllowance([1, time()]); | ||
$rateLimiter = $this->getMockBuilder(RateLimiter::className()) | ||
->setMethods(['addRateLimitHeaders']) | ||
->getMock(); | ||
$rateLimiter->expects(self::at(0)) | ||
->method('addRateLimitHeaders') | ||
->willReturn(null); | ||
|
||
$rateLimiter->checkRateLimit($rateLimit, Yii::$app->request, Yii::$app->response, 'testAction'); | ||
} | ||
|
||
public function testAddRateLimitHeadersDisabledRateLimitHeaders() | ||
{ | ||
$rateLimiter = new RateLimiter(); | ||
$rateLimiter->enableRateLimitHeaders = false; | ||
$response = Yii::$app->response; | ||
|
||
$rateLimiter->addRateLimitHeaders($response, 1, 0, 0); | ||
$this->assertCount(0, $response->getHeaders()); | ||
} | ||
|
||
public function testAddRateLimitHeadersEnabledRateLimitHeaders() | ||
{ | ||
$rateLimiter = new RateLimiter(); | ||
$rateLimiter->enableRateLimitHeaders = true; | ||
$response = Yii::$app->response; | ||
|
||
$rateLimiter->addRateLimitHeaders($response, 1, 0, 0); | ||
$this->assertCount(3, $response->getHeaders()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace yiiunit\framework\filters\stubs; | ||
|
||
use yii\base\Object; | ||
use yii\filters\RateLimitInterface; | ||
|
||
class RateLimit extends Object implements RateLimitInterface | ||
{ | ||
private $_rateLimit; | ||
|
||
private $_allowance; | ||
|
||
public function getRateLimit($request, $action) | ||
{ | ||
return $this->_rateLimit; | ||
} | ||
|
||
public function setRateLimit($rateLimit) | ||
{ | ||
$this->_rateLimit = $rateLimit; | ||
|
||
return $this; | ||
} | ||
|
||
public function loadAllowance($request, $action) | ||
{ | ||
return $this->_allowance; | ||
} | ||
|
||
public function setAllowance($allowance) | ||
{ | ||
$this->_allowance = $allowance; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
public function saveAllowance($request, $action, $allowance, $timestamp) | ||
{ | ||
return [$action, $allowance, $timestamp]; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpdoc missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I corrected