Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raw_kwargs = {"content": b""} # type: typing.Dict[str, typing.Any]
template = None
context = None
try:
await self.app(scope, receive, send)
except BaseException as exc:
if not self.suppress_exceptions:
raise exc from None
if not self.suppress_exceptions:
assert response_started, "TestClient did not receive any response."
elif not response_started:
raw_kwargs = {"status_code": 500, "headers": []}
raw = http3.AsyncResponse(**raw_kwargs)
response = self.build_response(request, raw)
if template is not None:
response.template = template
response.context = context
return response
def __init__(self):
self.pool = http3.ConnectionPool()
url = request.url
headers = [(_encode(k), _encode(v)) for k, v in request.headers.items()]
if not request.body:
body = b""
elif isinstance(request.body, str):
body = _encode(request.body)
else:
body = request.body
if isinstance(timeout, tuple):
timeout_kwargs = {"connect_timeout": timeout[0], "read_timeout": timeout[1]}
else:
timeout_kwargs = {"connect_timeout": timeout, "read_timeout": timeout}
timeout = http3.TimeoutConfig(**timeout_kwargs)
try:
response = await self.pool.request(
method,
url,
headers=headers,
data=body,
cert=cert,
verify=verify,
timeout=timeout,
)
except socket.error as err:
raise ConnectionError(err, request=request)
except http3.ConnectTimeout as err:
raise ConnectTimeout(err, request=request)
except http3.ReadTimeout as err:
timeout = http3.TimeoutConfig(**timeout_kwargs)
try:
response = await self.pool.request(
method,
url,
headers=headers,
data=body,
cert=cert,
verify=verify,
timeout=timeout,
)
except socket.error as err:
raise ConnectionError(err, request=request)
except http3.ConnectTimeout as err:
raise ConnectTimeout(err, request=request)
except http3.ReadTimeout as err:
raise ReadTimeout(err, request=request)
if not stream:
await response.read()
return self.build_response(request, response)
try:
response = await self.pool.request(
method,
url,
headers=headers,
data=body,
cert=cert,
verify=verify,
timeout=timeout,
)
except socket.error as err:
raise ConnectionError(err, request=request)
except http3.ConnectTimeout as err:
raise ConnectTimeout(err, request=request)
except http3.ReadTimeout as err:
raise ReadTimeout(err, request=request)
if not stream:
await response.read()
return self.build_response(request, response)
async def connect(
self,
hostname: str,
port: int,
ssl_context: typing.Optional[ssl.SSLContext],
timeout: TimeoutConfig,
) -> typing.Tuple[BaseReader, BaseWriter, Protocol]:
raise NotImplementedError() # pragma: no cover
async def request(
self,
method: str,
url: URLTypes,
*,
data: AsyncRequestData = b"",
params: QueryParamTypes = None,
headers: HeaderTypes = None,
verify: VerifyTypes = None,
cert: CertTypes = None,
timeout: TimeoutTypes = None
) -> AsyncResponse:
request = AsyncRequest(method, url, data=data, params=params, headers=headers)
return await self.send(request, verify=verify, cert=cert, timeout=timeout)
def request(
self,
method: str,
url: URLTypes,
*,
data: RequestData = b"",
params: QueryParamTypes = None,
headers: HeaderTypes = None,
verify: VerifyTypes = None,
cert: CertTypes = None,
timeout: TimeoutTypes = None
) -> Response:
request = Request(method, url, data=data, params=params, headers=headers)
return self.send(request, verify=verify, cert=cert, timeout=timeout)