22from starlette .responses import PlainTextResponse
33
44from ellar .common import Controller , Module , exception_handler , get
5- from ellar .constants import ASGI_CONTEXT_VAR
65from ellar .core import TestClientFactory
76from ellar .core .context import (
87 ExecutionContext ,
1514from ellar .core .exceptions import IExceptionMiddlewareService
1615from ellar .core .exceptions .service import ExceptionMiddlewareService
1716from ellar .di import ProviderConfig , injectable
18- from ellar .di .exceptions import ServiceUnavailable
1917from ellar .services import Reflector
2018
2119
@@ -42,35 +40,23 @@ class NewExecutionHostFactory(IExecutionContextFactory):
4240 def __init__ (self , reflector : Reflector ):
4341 self .reflector = reflector
4442
45- def create_context (self , operation , scope , receive , send ) -> IExecutionContext :
46- scoped_request_args = ASGI_CONTEXT_VAR .get ()
47-
48- if not scoped_request_args :
49- raise ServiceUnavailable ()
50-
43+ def create_context (
44+ self , operation , scope , receive = None , send = None
45+ ) -> IExecutionContext :
5146 i_execution_context = NewExecutionContext (
5247 scope = scope ,
5348 receive = receive ,
5449 send = send ,
5550 operation_handler = operation .endpoint ,
5651 reflector = self .reflector ,
5752 )
58- i_execution_context .get_service_provider ().update_scoped_context (
59- IExecutionContext , i_execution_context
60- )
6153
6254 return i_execution_context
6355
6456
6557@injectable ()
6658class NewHostContextFactory (IHostContextFactory ):
67- def create_context (self ) -> IHostContext :
68- scoped_request_args = ASGI_CONTEXT_VAR .get ()
69-
70- if not scoped_request_args :
71- raise ServiceUnavailable ()
72-
73- scope , receive , send = scoped_request_args .get_args ()
59+ def create_context (self , scope , receive = None , send = None ) -> IHostContext :
7460 host_context = NewHostContext (scope = scope , receive = receive , send = send )
7561 host_context .get_service_provider ().update_scoped_context (
7662 IHostContext , host_context
@@ -98,9 +84,8 @@ def test_can_replace_host_context():
9884
9985 assert hasattr (NewHostContext , "worked" ) is False
10086 client = tm .get_client ()
101- res = client .get ("/example/" )
102- assert res .status_code == 200
103- assert res .text == '"ExecutionContext"'
87+ res = client .get ("/example/exception" )
88+ assert res .json () == {"detail" : "Bad Request" , "status_code" : 400 }
10489
10590 assert hasattr (NewHostContext , "worked" ) is True
10691 assert NewHostContext .worked is True
0 commit comments