Skip to content

Commit

Permalink
Fixed last failure
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Jul 13, 2023
1 parent df70155 commit e42480f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tests/middleware/test_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import contextvars
import sys
from contextlib import AsyncExitStack
from typing import AsyncGenerator, Awaitable, Callable, List, Union

import anyio
import pytest
from exceptiongroup import ExceptionGroup, catch

from starlette.applications import Starlette
from starlette.background import BackgroundTask
Expand All @@ -16,9 +16,6 @@
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Message, Receive, Scope, Send

if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup


class CustomMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
Expand Down Expand Up @@ -219,12 +216,23 @@ async def homepage(request):
ctxvar.set("set by endpoint")
return PlainTextResponse("Homepage")

def handle_assertion_error(eg):
if middleware_cls is CustomMiddlewareUsingBaseHTTPMiddleware:
pytest.xfail(
"BaseHTTPMiddleware creates a TaskGroup which copies the context"
"and erases any changes to it made within the TaskGroup"
)

raise

app = Starlette(
middleware=[Middleware(middleware_cls)], routes=[Route("/", homepage)]
)

client = test_client_factory(app)
response = client.get("/")
with catch({AssertionError: handle_assertion_error}):
response = client.get("/")

assert response.status_code == 200, response.content


Expand Down

0 comments on commit e42480f

Please sign in to comment.