-
Notifications
You must be signed in to change notification settings - Fork 555
feat(django): Pick custom urlconf up from request if any #1308
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b5a2c21
Pick custom urlconf up from request if any
sl0thentr0py d239797
fix: Formatting
cadc705
Stupid
sl0thentr0py f39388a
Try out middleware logic
sl0thentr0py 2f485e1
fix: Formatting
f140349
Trigger gh
sl0thentr0py af34c3a
Try another resolve with custom request.urlconf in _after_get_response
sl0thentr0py 652280b
Only test new middleware on django > 2
sl0thentr0py c504384
Fix middleware test
sl0thentr0py e745c19
Cleanup MIDDLEWARE mutation after test
sl0thentr0py 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""myapp URL Configuration | ||
|
||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/2.0/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from __future__ import absolute_import | ||
|
||
try: | ||
from django.urls import path | ||
except ImportError: | ||
from django.conf.urls import url | ||
|
||
def path(path, *args, **kwargs): | ||
return url("^{}$".format(path), *args, **kwargs) | ||
|
||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
path("custom/ok", views.custom_ok, name="custom_ok"), | ||
AbhiPrasad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] |
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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import asyncio | ||
from django.utils.decorators import sync_and_async_middleware | ||
import django | ||
|
||
if django.VERSION >= (3, 1): | ||
import asyncio | ||
from django.utils.decorators import sync_and_async_middleware | ||
|
||
@sync_and_async_middleware | ||
def simple_middleware(get_response): | ||
if asyncio.iscoroutinefunction(get_response): | ||
@sync_and_async_middleware | ||
def simple_middleware(get_response): | ||
if asyncio.iscoroutinefunction(get_response): | ||
|
||
async def middleware(request): | ||
response = await get_response(request) | ||
return response | ||
async def middleware(request): | ||
response = await get_response(request) | ||
return response | ||
|
||
else: | ||
else: | ||
|
||
def middleware(request): | ||
response = get_response(request) | ||
return response | ||
def middleware(request): | ||
response = get_response(request) | ||
return response | ||
|
||
return middleware | ||
|
||
|
||
def custom_urlconf_middleware(get_response): | ||
def middleware(request): | ||
request.urlconf = "tests.integrations.django.myapp.custom_urls" | ||
response = get_response(request) | ||
return response | ||
|
||
return middleware |
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 |
---|---|---|
|
@@ -755,3 +755,30 @@ def test_csrf(sentry_init, client): | |
content, status, _headers = client.post(reverse("message")) | ||
assert status.lower() == "200 ok" | ||
assert b"".join(content) == b"ok" | ||
|
||
|
||
@pytest.mark.skipif(DJANGO_VERSION < (2, 0), reason="Requires Django > 2.0") | ||
def test_custom_urlconf_middleware( | ||
settings, sentry_init, client, capture_events, render_span_tree | ||
): | ||
""" | ||
Some middlewares (for instance in django-tenants) overwrite request.urlconf. | ||
Test that the resolver picks up the correct urlconf for transaction naming. | ||
""" | ||
urlconf = "tests.integrations.django.myapp.middleware.custom_urlconf_middleware" | ||
settings.ROOT_URLCONF = "" | ||
settings.MIDDLEWARE.insert(0, urlconf) | ||
client.application.load_middleware() | ||
|
||
sentry_init(integrations=[DjangoIntegration()], traces_sample_rate=1.0) | ||
events = capture_events() | ||
|
||
content, status, _headers = client.get("/custom/ok") | ||
assert status.lower() == "200 ok" | ||
assert b"".join(content) == b"custom ok" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need the bytes literal here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a byte stream or something from |
||
|
||
(event,) = events | ||
assert event["transaction"] == "/custom/ok" | ||
assert "custom_urlconf_middleware" in render_span_tree(event) | ||
|
||
settings.MIDDLEWARE.pop(0) |
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.
Uh oh!
There was an error while loading. Please reload this page.