Skip to content

feat: add RedirectResponse for convenient HTTP redirects#1367

Open
sansyrox wants to merge 3 commits intomainfrom
feat/redirect-response
Open

feat: add RedirectResponse for convenient HTTP redirects#1367
sansyrox wants to merge 3 commits intomainfrom
feat/redirect-response

Conversation

@sansyrox
Copy link
Copy Markdown
Member

@sansyrox sansyrox commented Apr 11, 2026

Summary

  • Adds a RedirectResponse class to robyn/responses.py that extends Response and automatically sets the Location header and redirect status code.
  • Defaults to 307 Temporary Redirect; also supports 301, 302, 303, and 308.
  • Exports RedirectResponse from the top-level robyn package via __init__.py and __all__.

Test plan

  • Unit tests added in unit_tests/test_redirect_response.py
    • test_redirect_response_defaults — verifies default 307 status and Location header
    • test_redirect_response_301 — verifies permanent redirect with custom status code
    • test_redirect_response_with_extra_headers — verifies Location is set alongside custom headers
  • Run pytest unit_tests/test_redirect_response.py to confirm all tests pass
  • Verify no regressions in existing test suite

Made with Cursor

Summary by CodeRabbit

  • New Features

    • New publicly available RedirectResponse for issuing HTTP redirects with configurable status codes (default 307) and support for custom headers.
  • Tests

    • Added unit tests covering default behavior, alternative status codes, header preservation, and validation for invalid status codes.

Adds a `RedirectResponse` class that sets the `Location` header and
status code automatically. Defaults to 307 (Temporary Redirect).
Supports 301, 302, 303, 307, and 308 redirect codes.

Made-with: Cursor
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
robyn Ready Ready Preview, Comment Apr 12, 2026 0:57am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d7b30a39-0fbd-415b-b882-49524510719f

📥 Commits

Reviewing files that changed from the base of the PR and between f53dfaf and 4223c98.

📒 Files selected for processing (2)
  • robyn/responses.py
  • unit_tests/test_redirect_response.py
✅ Files skipped from review due to trivial changes (1)
  • unit_tests/test_redirect_response.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • robyn/responses.py

📝 Walkthrough

Walkthrough

A new RedirectResponse class was added to construct HTTP redirect responses (default status 307) that set a Location header; it is exported from the package root and covered by new unit tests validating status codes and header behavior.

Changes

Cohort / File(s) Summary
RedirectResponse Implementation
robyn/responses.py, robyn/__init__.py
Added _REDIRECT_STATUS_CODES and a RedirectResponse(Response) class that validates allowed redirect status codes, sets/merges headers including Location: <url>, and is exported via robyn.__all__.
Test Coverage
unit_tests/test_redirect_response.py
New tests verifying default status code (307), explicit status code (e.g., 301), header preservation when passing a Headers object, and that invalid status codes raise ValueError with an appropriate message.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I found a path and set Location true,
A gentle hop to 307 or 301 too,
Headers kept snug, errors caught tight,
The rabbit redirects into the night ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main feature being added: a RedirectResponse class for HTTP redirects.
Description check ✅ Passed The description covers the key changes and provides a test plan with specific test names, matching the template's requirements for summary and testing coverage.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/redirect-response

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
unit_tests/test_redirect_response.py (1)

4-22: Add one negative test for unsupported status codes.

Current tests validate happy paths well; adding a failure-path case (e.g., status_code=200) will lock in redirect-only behavior and prevent regressions.

✅ Suggested test addition
 from robyn.responses import RedirectResponse
+import pytest
@@
 def test_redirect_response_with_extra_headers():
@@
     assert resp.headers.get("X-Custom") == "value"
+
+
+def test_redirect_response_invalid_status_code():
+    with pytest.raises(ValueError):
+        RedirectResponse("/target", status_code=200)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@unit_tests/test_redirect_response.py` around lines 4 - 22, Add a negative
unit test in test_redirect_response.py that constructs RedirectResponse with a
non-redirect status_code (e.g., status_code=200) and asserts that this is
rejected or normalized to a redirect status (depending on implementation) —
reference the RedirectResponse constructor and its status_code handling to
locate the logic to test; ensure the test asserts the expected failure/exception
or that status_code is one of allowed redirect codes (e.g., raises ValueError or
sets to 307) so regressions that allow non-redirect codes are caught.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@robyn/responses.py`:
- Around line 32-44: The RedirectResponse.__init__ currently accepts any integer
status_code and forwards it, allowing non-redirect codes with a Location header;
update RedirectResponse.__init__ to validate the status_code is a valid redirect
code (e.g., 300–399 or explicitly one of 301,302,303,307,308) and raise a
ValueError (or TypeError) if not, before setting the Location header and calling
super().__init__; reference the RedirectResponse.__init__ constructor and the
redirect_headers/Headers usage when making the change.

---

Nitpick comments:
In `@unit_tests/test_redirect_response.py`:
- Around line 4-22: Add a negative unit test in test_redirect_response.py that
constructs RedirectResponse with a non-redirect status_code (e.g.,
status_code=200) and asserts that this is rejected or normalized to a redirect
status (depending on implementation) — reference the RedirectResponse
constructor and its status_code handling to locate the logic to test; ensure the
test asserts the expected failure/exception or that status_code is one of
allowed redirect codes (e.g., raises ValueError or sets to 307) so regressions
that allow non-redirect codes are caught.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d6735529-d16b-4bb1-b474-c6484a0fe645

📥 Commits

Reviewing files that changed from the base of the PR and between 3e04c65 and f53dfaf.

📒 Files selected for processing (3)
  • robyn/__init__.py
  • robyn/responses.py
  • unit_tests/test_redirect_response.py

Comment thread robyn/responses.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant