@@ -361,33 +361,15 @@ Test interceptors as part of your full application stack. For example, testing t
361361=== "ASGI"
362362
363363 ```python
364- from contextvars import ContextVar, Token
365364 import httpx
366365 import pytest
367366 from connectrpc.code import Code
368367 from connectrpc.errors import ConnectError
369368 from greet.v1.greet_connect import GreetServiceASGIApplication, GreetServiceClient
370369 from greet.v1.greet_pb2 import GreetRequest
370+ from interceptors import ServerAuthInterceptor
371371 from server import Greeter
372372
373- _auth_token = ContextVar["auth_token"]("current_auth_token")
374-
375- class ServerAuthInterceptor:
376- def __init__(self, valid_tokens: list[str]):
377- self._valid_tokens = valid_tokens
378-
379- async def on_start(self, ctx) -> Token["auth_token"]:
380- authorization = ctx.request_headers().get("authorization")
381- if not authorization or not authorization.startswith("Bearer "):
382- raise ConnectError(Code.UNAUTHENTICATED)
383- token = authorization[len("Bearer "):]
384- if token not in self._valid_tokens:
385- raise ConnectError(Code.PERMISSION_DENIED)
386- return _auth_token.set(token)
387-
388- async def on_end(self, token: Token["auth_token"], ctx):
389- _auth_token.reset(token)
390-
391373 @pytest.mark.asyncio
392374 async def test_server_auth_interceptor():
393375 interceptor = ServerAuthInterceptor(["valid-token"])
@@ -429,33 +411,15 @@ Test interceptors as part of your full application stack. For example, testing t
429411=== "WSGI"
430412
431413 ```python
432- from contextvars import ContextVar, Token
433414 import httpx
434415 import pytest
435416 from connectrpc.code import Code
436417 from connectrpc.errors import ConnectError
437418 from greet.v1.greet_connect import GreetServiceWSGIApplication, GreetServiceClientSync
438419 from greet.v1.greet_pb2 import GreetRequest
420+ from interceptors import ServerAuthInterceptor
439421 from server import GreeterSync
440422
441- _auth_token = ContextVar["auth_token"]("current_auth_token")
442-
443- class ServerAuthInterceptor:
444- def __init__(self, valid_tokens: list[str]):
445- self._valid_tokens = valid_tokens
446-
447- def on_start_sync(self, ctx) -> Token["auth_token"]:
448- authorization = ctx.request_headers().get("authorization")
449- if not authorization or not authorization.startswith("Bearer "):
450- raise ConnectError(Code.UNAUTHENTICATED)
451- token = authorization[len("Bearer "):]
452- if token not in self._valid_tokens:
453- raise ConnectError(Code.PERMISSION_DENIED)
454- return _auth_token.set(token)
455-
456- def on_end_sync(self, token: Token["auth_token"], ctx):
457- _auth_token.reset(token)
458-
459423 def test_server_auth_interceptor():
460424 interceptor = ServerAuthInterceptor(["valid-token"])
461425 app = GreetServiceWSGIApplication(
0 commit comments