Repository navigation
Add Retry-After to rate-limited responses #771
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
Open
iacobdaniel
wants to merge
1
commit into
main
Choose a base branch
from
feat/retry-after-header
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from .asgi import AikidoASGIMiddleware | ||
|
|
||
|
|
||
| async def call_middleware(result): | ||
| sent = [] | ||
|
|
||
| async def app(scope, receive, send): | ||
| raise AssertionError("app should not be called") | ||
|
|
||
| async def send(message): | ||
| sent.append(message) | ||
|
|
||
| with patch("aikido_zen.middleware.asgi.should_block_request", return_value=result): | ||
| await AikidoASGIMiddleware(app)({"type": "http"}, None, send) | ||
| return sent | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_rate_limited_response_has_retry_after(): | ||
| sent = await call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": 7, | ||
| } | ||
| ) | ||
| assert sent[0]["status"] == 429 | ||
| assert sent[0]["headers"] == [ | ||
| (b"content-type", b"text/plain"), | ||
| (b"retry-after", b"7"), | ||
| ] | ||
| assert sent[1]["body"] == b"You are rate limited by Zen." | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_rate_limited_response_without_retry_after_seconds(): | ||
| sent = await call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": None, | ||
| } | ||
| ) | ||
| assert sent[0]["status"] == 429 | ||
| assert sent[0]["headers"] == [(b"content-type", b"text/plain")] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_blocked_user_response_has_no_retry_after(): | ||
| sent = await call_middleware({"block": True, "type": "blocked", "trigger": "user"}) | ||
| assert sent[0]["status"] == 403 | ||
| assert sent[0]["headers"] == [(b"content-type", b"text/plain")] |
This file contains hidden or 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 hidden or 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,49 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from .django import AikidoDjangoMiddleware | ||
|
|
||
|
|
||
| class FakeHttpResponse(dict): | ||
| def __init__(self, content, content_type=None, status=200): | ||
| super().__init__() | ||
| self.content = content | ||
| self.content_type = content_type | ||
| self.status_code = status | ||
|
|
||
|
|
||
| def call_middleware(result): | ||
| middleware = AikidoDjangoMiddleware(get_response=None) | ||
| middleware.HttpResponse = FakeHttpResponse | ||
| with patch( | ||
| "aikido_zen.middleware.django.should_block_request", return_value=result | ||
| ): | ||
| return middleware(request=None) | ||
|
|
||
|
|
||
| def test_rate_limited_response_has_retry_after(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": 7, | ||
| } | ||
| ) | ||
| assert response.status_code == 429 | ||
| assert response["Retry-After"] == "7" | ||
| assert response.content == "You are rate limited by Zen." | ||
|
|
||
|
|
||
| def test_rate_limited_response_without_retry_after_seconds(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": None, | ||
| } | ||
| ) | ||
| assert response.status_code == 429 | ||
| assert "Retry-After" not in response |
This file contains hidden or 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 hidden or 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,47 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from werkzeug.test import create_environ | ||
|
|
||
| from .flask import AikidoFlaskMiddleware | ||
|
|
||
|
|
||
| def call_middleware(result): | ||
| captured = {} | ||
|
|
||
| def start_response(status, headers, exc_info=None): | ||
| captured["status"] = status | ||
| captured["headers"] = dict(headers) | ||
|
|
||
| with patch("aikido_zen.middleware.flask.should_block_request", return_value=result): | ||
| middleware = AikidoFlaskMiddleware(app=None) | ||
| captured["body"] = b"".join(middleware(create_environ("/"), start_response)) | ||
| return captured | ||
|
|
||
|
|
||
| def test_rate_limited_response_has_retry_after(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "ip", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": 7, | ||
| } | ||
| ) | ||
| assert response["status"] == "429 TOO MANY REQUESTS" | ||
| assert response["headers"]["Retry-After"] == "7" | ||
| assert response["body"] == b"You are rate limited by Zen. (Your IP: 1.1.1.1)" | ||
|
|
||
|
|
||
| def test_rate_limited_response_without_retry_after_seconds(): | ||
| response = call_middleware( | ||
| { | ||
| "block": True, | ||
| "type": "ratelimited", | ||
| "trigger": "user", | ||
| "ip": "1.1.1.1", | ||
| "retry_after_seconds": None, | ||
| } | ||
| ) | ||
| assert response["status"] == "429 TOO MANY REQUESTS" | ||
| assert "Retry-After" not in response["headers"] |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
The group, user, and IP branches repeat the same blocked-result construction, including
retry_after_seconds. Consolidate the shared handling so changes to this response stay consistent across all three paths.Details
✨ AI Reasoning
The group, user, and IP branches each inspect the same
allowedresult and construct the same blocked response shape, differing only in the key and trigger. This repeats the newly added retry-delay handling and response construction, so consolidating that shared behavior would reduce the chance that one path is updated differently.🔧 How do I fix it?
Delete extra code. Extract repeated code sequences into reusable functions or methods. Use loops or data structures to eliminate repetitive patterns.
Reply on this comment to give feedback or ignore the issue.
More info