[增添]添加了datasource的setting数据库以及默认值
This commit is contained in:
1
vendor/danharrin/livewire-rate-limiting/.github/FUNDING.yml
vendored
Normal file
1
vendor/danharrin/livewire-rate-limiting/.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
github: [danharrin]
|
||||
25
vendor/danharrin/livewire-rate-limiting/.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
25
vendor/danharrin/livewire-rate-limiting/.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
labels: bug
|
||||
assignees: DanHarrin
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To reproduce**
|
||||
A simplified code example to demonstrate the issue you are facing.
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Context**
|
||||
- Package version: [e.g. 0.1.0]
|
||||
- Livewire version: [e.g. 2.0.0]
|
||||
- Laravel version: [e.g. 8.0.0]
|
||||
- Server OS: [e.g. Ubuntu 18.04.4]
|
||||
- PHP version: [e.g. 7.4]
|
||||
|
||||
**Additional details**
|
||||
Add any other details about the problem here.
|
||||
8
vendor/danharrin/livewire-rate-limiting/.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
8
vendor/danharrin/livewire-rate-limiting/.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Feature request
|
||||
url: https://github.com/danharrin/livewire-rate-limiting/discussions/new
|
||||
about: For ideas or feature requests
|
||||
- name: Support questions & other
|
||||
url: https://github.com/danharrin/livewire-rate-limiting/discussions/new
|
||||
about: If you have a question or need help installing using the package
|
||||
40
vendor/danharrin/livewire-rate-limiting/.github/workflows/tests.yml
vendored
Normal file
40
vendor/danharrin/livewire-rate-limiting/.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
name: tests
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
run-tests:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
php: [ 8.3, 8.2 ]
|
||||
laravel: [ 11.*, 10.* ]
|
||||
dependency-version: [ prefer-stable ]
|
||||
include:
|
||||
- laravel: 11.*
|
||||
testbench: 9.*
|
||||
- laravel: 10.*
|
||||
testbench: 8.*
|
||||
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.composer/cache/files
|
||||
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php }}
|
||||
extensions: mbstring, pdo, pdo_sqlite
|
||||
coverage: none
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
|
||||
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
|
||||
- name: Execute tests
|
||||
run: vendor/bin/phpunit
|
||||
4
vendor/danharrin/livewire-rate-limiting/.gitignore
vendored
Normal file
4
vendor/danharrin/livewire-rate-limiting/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/.idea
|
||||
/vendor
|
||||
.phpunit.result.cache
|
||||
.phpunit.cache
|
||||
76
vendor/danharrin/livewire-rate-limiting/CODE_OF_CONDUCT.md
vendored
Normal file
76
vendor/danharrin/livewire-rate-limiting/CODE_OF_CONDUCT.md
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
||||
level of experience, education, socio-economic status, nationality, personal
|
||||
appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting Dan Harrin at dan@danharrin.com. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see
|
||||
https://www.contributor-covenant.org/faq
|
||||
21
vendor/danharrin/livewire-rate-limiting/LICENSE.md
vendored
Normal file
21
vendor/danharrin/livewire-rate-limiting/LICENSE.md
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# MIT License
|
||||
|
||||
Copyright (c) Dan Harrin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
136
vendor/danharrin/livewire-rate-limiting/README.md
vendored
Normal file
136
vendor/danharrin/livewire-rate-limiting/README.md
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
<p align="center">
|
||||
<img src="https://user-images.githubusercontent.com/41773797/104827274-ddb3a580-5853-11eb-9d53-eaf3e7776734.png" alt="Package banner" style="width: 100%; max-width: 800px;" />
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/danharrin/livewire-rate-limiting/actions"><img alt="Tests passing" src="https://img.shields.io/badge/Tests-passing-green?style=for-the-badge&logo=github"></a>
|
||||
<a href="https://laravel.com"><img alt="Laravel v8.x, v9.x, v10.x, v11.x" src="https://img.shields.io/badge/Laravel-v8.x, v9.x, v10.x, v11.x-FF2D20?style=for-the-badge&logo=laravel"></a>
|
||||
<a href="https://laravel.com"><img alt="PHP 8.0+" src="https://img.shields.io/badge/PHP-8.0+-777BB4?style=for-the-badge&logo=php"></a>
|
||||
</p>
|
||||
|
||||
This package allows you to apply rate limiters to Laravel Livewire actions. This is useful for throttling login attempts and other brute force attacks, reducing spam, and more.
|
||||
|
||||
## Installation
|
||||
|
||||
You can use Composer to install this package into your application:
|
||||
|
||||
```
|
||||
composer require danharrin/livewire-rate-limiting
|
||||
```
|
||||
|
||||
This package requires at least Laravel v8.x, when rate limiting improvements were introduced.
|
||||
|
||||
This package is tested to support the `file` and `redis` cache drivers, but not `array`.
|
||||
|
||||
## Usage
|
||||
|
||||
Apply the `DanHarrin\LivewireRateLimiting\WithRateLimiting` trait to your Livewire component:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Login;
|
||||
|
||||
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
use Livewire\Component;
|
||||
|
||||
class Login extends Component
|
||||
{
|
||||
use WithRateLimiting;
|
||||
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
In this example, we will set up rate limiting on the `submit` action.
|
||||
|
||||
The user will only be able to call this action 10 times every minute.
|
||||
|
||||
If this limit is exceeded, a `TooManyRequestsException` will be thrown. The user is presented with a validation error and instructed how long they have until the limit is lifted:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Login;
|
||||
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Livewire\Component;
|
||||
|
||||
class Login extends Component
|
||||
{
|
||||
use WithRateLimiting;
|
||||
|
||||
public function submit()
|
||||
{
|
||||
try {
|
||||
$this->rateLimit(10);
|
||||
} catch (TooManyRequestsException $exception) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => "Slow down! Please wait another {$exception->secondsUntilAvailable} seconds to log in.",
|
||||
]);
|
||||
}
|
||||
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### Component Methods
|
||||
|
||||
```php
|
||||
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
|
||||
/**
|
||||
* Rate limit a Livewire method, `$maxAttempts` times every `$decaySeconds` seconds.
|
||||
*
|
||||
* @throws DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException
|
||||
*/
|
||||
$this->rateLimit(
|
||||
$maxAttempts, // The number of times that the rate limit can be hit in the given decay period.
|
||||
$decaySeconds = 60, // The length of the decay period in seconds. By default, this is a minute.
|
||||
$method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->rateLimit()` is called from.
|
||||
);
|
||||
|
||||
/**
|
||||
* Hit a method's rate limiter without consequence.
|
||||
*/
|
||||
$this->hitRateLimiter(
|
||||
$method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->hitRateLimiter()` is called from.
|
||||
$decaySeconds = 60, // The length of the decay period in seconds. By default, this is a minute.
|
||||
);
|
||||
|
||||
/**
|
||||
* Clear a method's rate limiter.
|
||||
*/
|
||||
$this->clearRateLimiter(
|
||||
$method, // The name of the method that is being rate limited. By default, this is set to the method that `$this->clearRateLimiter()` is called from.
|
||||
);
|
||||
```
|
||||
|
||||
### Exceptions
|
||||
|
||||
```php
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
|
||||
try {
|
||||
$this->rateLimit(10);
|
||||
} catch (TooManyRequestsException $exception) {
|
||||
$exception->component; // Class of the component that the rate limit was hit within.
|
||||
$exception->ip; // IP of the user that has hit the rate limit.
|
||||
$exception->method; // Name of the method that has hit the rate limit.
|
||||
$exception->minutesUntilAvailable; // Number of minutes until the rate limit is lifted, rounded up.
|
||||
$exception->secondsUntilAvailable; // Number of seconds until the rate limit is lifted.
|
||||
}
|
||||
```
|
||||
|
||||
## Need Help?
|
||||
|
||||
🐞 If you spot a bug with this package, please [submit a detailed issue](https://github.com/danharrin/livewire-rate-limiting/issues/new), and wait for assistance.
|
||||
|
||||
🤔 If you have a question or feature request, please [start a new discussion](https://github.com/danharrin/livewire-rate-limiting/discussions/new).
|
||||
|
||||
🔐 If you discover a vulnerability within the package, please review our [security policy](https://github.com/danharrin/livewire-rate-limiting/blob/main/SECURITY.md).
|
||||
11
vendor/danharrin/livewire-rate-limiting/SECURITY.md
vendored
Normal file
11
vendor/danharrin/livewire-rate-limiting/SECURITY.md
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
| Version | Supported |
|
||||
| ------- | ------------------ |
|
||||
| 0.x | :white_check_mark: |
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
If you discover a security vulnerability within the package, please email Dan Harrin via [dan@danharrin.com](mailto:dan@danharrin.com). All security vulnerabilities will be promptly addressed.
|
||||
40
vendor/danharrin/livewire-rate-limiting/composer.json
vendored
Normal file
40
vendor/danharrin/livewire-rate-limiting/composer.json
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "danharrin/livewire-rate-limiting",
|
||||
"description": "Apply rate limiters to Laravel Livewire actions.",
|
||||
"license": "MIT",
|
||||
"homepage": "https://github.com/danharrin/livewire-rate-limiting",
|
||||
"support": {
|
||||
"issues": "https://github.com/danharrin/livewire-rate-limiting/issues",
|
||||
"source": "https://github.com/danharrin/livewire-rate-limiting"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Dan Harrin",
|
||||
"email": "dan@danharrin.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.0",
|
||||
"illuminate/support": "^9.0|^10.0|^11.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"livewire/livewire": "^3.0",
|
||||
"livewire/volt": "^1.3",
|
||||
"orchestra/testbench": "^7.0|^8.0|^9.0",
|
||||
"phpunit/phpunit": "^9.0|^10.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"DanHarrin\\LivewireRateLimiting\\": "src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"DanHarrin\\LivewireRateLimiting\\Tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
||||
22
vendor/danharrin/livewire-rate-limiting/phpunit.xml.dist
vendored
Normal file
22
vendor/danharrin/livewire-rate-limiting/phpunit.xml.dist
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
beStrictAboutTodoAnnotatedTests="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
executionOrder="depends,defects"
|
||||
cacheDirectory=".phpunit.cache"
|
||||
>
|
||||
<coverage>
|
||||
<include>
|
||||
<directory suffix=".php">src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
<testsuites>
|
||||
<testsuite name="default">
|
||||
<directory suffix="Test.php">tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
27
vendor/danharrin/livewire-rate-limiting/src/Exceptions/TooManyRequestsException.php
vendored
Normal file
27
vendor/danharrin/livewire-rate-limiting/src/Exceptions/TooManyRequestsException.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace DanHarrin\LivewireRateLimiting\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class TooManyRequestsException extends Exception
|
||||
{
|
||||
public $minutesUntilAvailable;
|
||||
|
||||
public function __construct(
|
||||
public $component,
|
||||
public $method,
|
||||
public $ip,
|
||||
public $secondsUntilAvailable,
|
||||
) {
|
||||
$this->minutesUntilAvailable = ceil($this->secondsUntilAvailable / 60);
|
||||
|
||||
parent::__construct(sprintf(
|
||||
'Too many requests from [%s] to method [%s] on component: [%s]. Retry in %d seconds.',
|
||||
$this->ip,
|
||||
$this->method,
|
||||
$this->component,
|
||||
$this->secondsUntilAvailable,
|
||||
));
|
||||
}
|
||||
}
|
||||
58
vendor/danharrin/livewire-rate-limiting/src/WithRateLimiting.php
vendored
Normal file
58
vendor/danharrin/livewire-rate-limiting/src/WithRateLimiting.php
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace DanHarrin\LivewireRateLimiting;
|
||||
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
|
||||
trait WithRateLimiting
|
||||
{
|
||||
protected function clearRateLimiter($method = null, $component = null)
|
||||
{
|
||||
$method ??= debug_backtrace(limit: 2)[1]['function'];
|
||||
|
||||
$component ??= static::class;
|
||||
|
||||
$key = $this->getRateLimitKey($method, $component);
|
||||
|
||||
RateLimiter::clear($key);
|
||||
}
|
||||
|
||||
protected function getRateLimitKey($method, $component)
|
||||
{
|
||||
$method ??= debug_backtrace(limit: 2)[1]['function'];
|
||||
|
||||
$component ??= static::class;
|
||||
|
||||
return sha1($component.'|'.$method.'|'.request()->ip());
|
||||
}
|
||||
|
||||
protected function hitRateLimiter($method = null, $decaySeconds = 60, $component = null)
|
||||
{
|
||||
$method ??= debug_backtrace(limit: 2)[1]['function'];
|
||||
|
||||
$component ??= static::class;
|
||||
|
||||
$key = $this->getRateLimitKey($method, $component);
|
||||
|
||||
RateLimiter::hit($key, $decaySeconds);
|
||||
}
|
||||
|
||||
protected function rateLimit($maxAttempts, $decaySeconds = 60, $method = null, $component = null)
|
||||
{
|
||||
$method ??= debug_backtrace(limit: 2)[1]['function'];
|
||||
|
||||
$component ??= static::class;
|
||||
|
||||
$key = $this->getRateLimitKey($method, $component);
|
||||
|
||||
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
|
||||
$ip = request()->ip();
|
||||
$secondsUntilAvailable = RateLimiter::availableIn($key);
|
||||
|
||||
throw new TooManyRequestsException($component, $method, $ip, $secondsUntilAvailable);
|
||||
}
|
||||
|
||||
$this->hitRateLimiter($method, $decaySeconds, $component);
|
||||
}
|
||||
}
|
||||
124
vendor/danharrin/livewire-rate-limiting/tests/RateLimitingTest.php
vendored
Normal file
124
vendor/danharrin/livewire-rate-limiting/tests/RateLimitingTest.php
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace DanHarrin\LivewireRateLimiting\Tests;
|
||||
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
use Livewire\Livewire;
|
||||
use Livewire\Volt\Volt;
|
||||
|
||||
class RateLimitingTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function can_rate_limit()
|
||||
{
|
||||
$component = Livewire::test(Component::class);
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0);
|
||||
|
||||
sleep(1);
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_hit_and_clear_rate_limiter()
|
||||
{
|
||||
Livewire::test(Component::class)
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0)
|
||||
->call('clear')
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_rate_limit_volt()
|
||||
{
|
||||
$this->mountVolt();
|
||||
$component = Volt::test('volt-component');
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0)
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0);
|
||||
|
||||
sleep(1);
|
||||
|
||||
$component
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_hit_and_clear_rate_limiter_volt()
|
||||
{
|
||||
$this->mountVolt();
|
||||
Volt::test('volt-component')
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('hit')
|
||||
->call('limit')
|
||||
->assertNotSet('secondsUntilAvailable', 0)
|
||||
->call('clear')
|
||||
->call('limit')
|
||||
->assertSet('secondsUntilAvailable', 0);
|
||||
}
|
||||
|
||||
protected function mountVolt()
|
||||
{
|
||||
Volt::mount([
|
||||
__DIR__ . '/views',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
class Component extends \Livewire\Component
|
||||
{
|
||||
use \DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
|
||||
public $secondsUntilAvailable;
|
||||
|
||||
public function clear()
|
||||
{
|
||||
$this->clearRateLimiter('limit');
|
||||
}
|
||||
|
||||
public function hit()
|
||||
{
|
||||
$this->hitRateLimiter('limit', 1);
|
||||
}
|
||||
|
||||
public function limit()
|
||||
{
|
||||
try {
|
||||
$this->rateLimit(3, 1);
|
||||
} catch (TooManyRequestsException $exception) {
|
||||
return $this->secondsUntilAvailable = $exception->secondsUntilAvailable;
|
||||
}
|
||||
|
||||
$this->secondsUntilAvailable = 0;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('component');
|
||||
}
|
||||
}
|
||||
28
vendor/danharrin/livewire-rate-limiting/tests/TestCase.php
vendored
Normal file
28
vendor/danharrin/livewire-rate-limiting/tests/TestCase.php
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace DanHarrin\LivewireRateLimiting\Tests;
|
||||
|
||||
use Livewire\LivewireServiceProvider;
|
||||
use Livewire\Volt\Volt;
|
||||
use Livewire\Volt\VoltServiceProvider;
|
||||
|
||||
class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
protected function getEnvironmentSetUp($app)
|
||||
{
|
||||
$app['config']->set('view.paths', [
|
||||
__DIR__ . '/views',
|
||||
resource_path('views'),
|
||||
]);
|
||||
|
||||
$app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return [
|
||||
LivewireServiceProvider::class,
|
||||
VoltServiceProvider::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
1
vendor/danharrin/livewire-rate-limiting/tests/views/component.blade.php
vendored
Normal file
1
vendor/danharrin/livewire-rate-limiting/tests/views/component.blade.php
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<div></div>
|
||||
36
vendor/danharrin/livewire-rate-limiting/tests/views/volt-component.blade.php
vendored
Normal file
36
vendor/danharrin/livewire-rate-limiting/tests/views/volt-component.blade.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Livewire\Volt\Component;
|
||||
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
||||
|
||||
new class extends Component {
|
||||
use \DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
|
||||
public $secondsUntilAvailable;
|
||||
|
||||
public function clear()
|
||||
{
|
||||
$this->clearRateLimiter('limit', component: 'VoltComponent');
|
||||
}
|
||||
|
||||
public function hit()
|
||||
{
|
||||
$this->hitRateLimiter('limit', 1, component: 'VoltComponent');
|
||||
}
|
||||
|
||||
public function limit()
|
||||
{
|
||||
try {
|
||||
$this->rateLimit(3, 1, component: 'VoltComponent');
|
||||
} catch (TooManyRequestsException $exception) {
|
||||
return $this->secondsUntilAvailable = $exception->secondsUntilAvailable;
|
||||
}
|
||||
|
||||
$this->secondsUntilAvailable = 0;
|
||||
}
|
||||
|
||||
}; ?>
|
||||
|
||||
<div>
|
||||
//
|
||||
</div>
|
||||
Reference in New Issue
Block a user