File tree 2 files changed +29
-14
lines changed
2 files changed +29
-14
lines changed Original file line number Diff line number Diff line change @@ -13,26 +13,25 @@ def AccountMiddleware(get_response):
13
13
14
14
async def middleware (request ):
15
15
with context .request_context (request ):
16
- try :
17
- response = await get_response (request )
18
- if _should_check_dangling_login (request , response ):
19
- await _acheck_dangling_login (request )
20
- return response
21
- except ImmediateHttpResponse as e :
22
- return e .response
16
+ response = await get_response (request )
17
+ if _should_check_dangling_login (request , response ):
18
+ await _acheck_dangling_login (request )
19
+ return response
23
20
24
21
else :
25
22
26
23
def middleware (request ):
27
24
with context .request_context (request ):
28
- try :
29
- response = get_response (request )
30
- if _should_check_dangling_login (request , response ):
31
- _check_dangling_login (request )
32
- return response
33
- except ImmediateHttpResponse as e :
34
- return e .response
25
+ response = get_response (request )
26
+ if _should_check_dangling_login (request , response ):
27
+ _check_dangling_login (request )
28
+ return response
35
29
30
+ def process_exception (request , exception ):
31
+ if isinstance (exception , ImmediateHttpResponse ):
32
+ return exception .response
33
+
34
+ middleware .process_exception = process_exception
36
35
return middleware
37
36
38
37
Original file line number Diff line number Diff line change 1
1
from django .conf import settings
2
2
from django .http import HttpResponse
3
+ from django .urls import path
3
4
4
5
import pytest
5
6
6
7
from allauth .account .middleware import AccountMiddleware
8
+ from allauth .core .exceptions import ImmediateHttpResponse
7
9
8
10
9
11
@pytest .mark .parametrize (
@@ -35,3 +37,17 @@ def test_remove_dangling_login(
35
37
mw = AccountMiddleware (lambda request : response )
36
38
mw (request )
37
39
assert ("account_login" in request .session ) is (not login_removed )
40
+
41
+
42
+ def raise_immediate_http_response (request ):
43
+ response = HttpResponse (content = "raised-response" )
44
+ raise ImmediateHttpResponse (response = response )
45
+
46
+
47
+ urlpatterns = [path ("raise" , raise_immediate_http_response )]
48
+
49
+
50
+ def test_immediate_http_response (settings , client ):
51
+ settings .ROOT_URLCONF = "allauth.account.tests.test_middleware"
52
+ resp = client .get ("/raise" )
53
+ assert resp .content == b"raised-response"
You can’t perform that action at this time.
0 commit comments