# Buggy code
async def _get_response(
    self,
    request: AsyncRequest,
    *,
    stream: bool = False,
    auth: AuthTypes = None,
    allow_redirects: bool = True,
    verify: VerifyTypes = None,
    cert: CertTypes = None,
    timeout: TimeoutTypes = None,
    trust_env: bool = None,
    proxies: ProxiesTypes = None,
) -> AsyncResponse:
    if request.url.scheme not in ("http", "https"):
        raise InvalidURL('URL scheme must be "http" or "https".')

    if proxies is not None:
        dispatch_proxies = _proxies_to_dispatchers(proxies)
    else:
        dispatch_proxies = self.proxies
    dispatch = self._dispatcher_for_request(request, dispatch_proxies)

    async def get_response(request: AsyncRequest) -> AsyncResponse:
        try:
            with ElapsedTimer() as timer:
                response = await dispatch.send(
                    request, verify=verify, cert=cert, timeout=timeout
                )
            response.elapsed = timer.elapsed
        except HTTPError as exc:
            # Add the original request to any HTTPError unless
            # there'a already a request attached in the case of
            # a ProxyError.
            if exc.request is None:
                exc.request = request
            raise

        self.cookies.extract_cookies(response)
        if not stream:
            try:
                await response.read()
            finally:
                await response.close()

        return response

    def wrap(
        get_response: typing.Callable, middleware: BaseMiddleware
    ) -> typing.Callable:
        return functools.partial(middleware, get_response=get_response)

    get_response = wrap(
        get_response,
        RedirectMiddleware(allow_redirects=allow_redirects, cookies=self.cookies),
    )

    auth_middleware = self._get_auth_middleware(
        request=request,
        trust_env=self.trust_env if trust_env is None else trust_env,
        auth=self.auth if auth is None else auth,
    )

    if auth_middleware is not None:
        get_response = wrap(get_response, auth_middleware)

    return await get_response(request)


# Traceback (ssl.SSLCertVerificationError)
SSL handshake failed on verifying the certificate
protocol: <asyncio.sslproto.SSLProtocol object at 0x0000020CDCD7E470>
transport: <_SelectorSocketTransport fd=768 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\sslproto.py", line 625, in _on_handshake_complete
raise handshake_exc
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\sslproto.py", line 189, in feed_ssldata
self._sslobj.do_handshake()
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 763, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)
SSL error in data received
protocol: <asyncio.sslproto.SSLProtocol object at 0x0000020CDCD7E470>
transport: <_SelectorSocketTransport closing fd=768 read=idle write=<idle, bufsize=0>>
Traceback (most recent call last):
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\sslproto.py", line 526, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\sslproto.py", line 189, in feed_ssldata
self._sslobj.do_handshake()
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 763, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)
Traceback (most recent call last):
File "C:/Users/localhost/PycharmProjects/TribalW_Test_Env/Basic/tests/httpx/proxy_test.py", line 11, in <module>
request = client.get("http://whoer.net",headers=headers,verify=client_cer)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\client.py", line 818, in get
proxies=proxies,
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\client.py", line 729, in request
proxies=proxies,
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\client.py", line 760, in send
async_response = concurrency_backend.run(coroutine, *args, **kwargs)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\concurrency\asyncio.py", line 241, in run
return self.loop.run_until_complete(coroutine(*args, **kwargs))
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 584, in run_until_complete
return future.result()
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\client.py", line 230, in _get_response
return await get_response(request)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\middleware\redirect.py", line 41, in __call__
return await self(next_request, get_response)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\middleware\redirect.py", line 31, in __call__
response = await get_response(request)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\client.py", line 191, in get_response
request, verify=verify, cert=cert, timeout=timeout
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\dispatch\proxy_http.py", line 243, in send
request=request, verify=verify, cert=cert, timeout=timeout
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\dispatch\connection_pool.py", line 118, in send
connection = await self.acquire_connection(origin=request.url.origin)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\dispatch\proxy_http.py", line 92, in acquire_connection
return await self.tunnel_connection(origin)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\dispatch\proxy_http.py", line 110, in tunnel_connection
await self.tunnel_start_tls(origin, connection)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\dispatch\proxy_http.py", line 194, in tunnel_start_tls
timeout=timeout,
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\httpx\concurrency\asyncio.py", line 216, in start_tls
timeout=timeout.connect_timeout,
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py", line 416, in wait_for
return fut.result()
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 1134, in start_tls
await waiter
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\sslproto.py", line 526, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\sslproto.py", line 189, in feed_ssldata
self._sslobj.do_handshake()
File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 763, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)

# Fixed code
async def _get_response(
    self,
    request: AsyncRequest,
    *,
    stream: bool = False,
    auth: AuthTypes = None,
    allow_redirects: bool = True,
    verify: VerifyTypes = None,
    cert: CertTypes = None,
    timeout: TimeoutTypes = None,
    trust_env: bool = None,
) -> AsyncResponse:
    if request.url.scheme not in ("http", "https"):
        raise InvalidURL('URL scheme must be "http" or "https".')

    dispatch = self._dispatcher_for_request(request, self.proxies)

    async def get_response(request: AsyncRequest) -> AsyncResponse:
        try:
            with ElapsedTimer() as timer:
                response = await dispatch.send(
                    request, verify=verify, cert=cert, timeout=timeout
                )
            response.elapsed = timer.elapsed
        except HTTPError as exc:
            # Add the original request to any HTTPError unless
            # there'a already a request attached in the case of
            # a ProxyError.
            if exc.request is None:
                exc.request = request
            raise

        self.cookies.extract_cookies(response)
        if not stream:
            try:
                await response.read()
            finally:
                await response.close()

        return response

    def wrap(
        get_response: typing.Callable, middleware: BaseMiddleware
    ) -> typing.Callable:
        return functools.partial(middleware, get_response=get_response)

    get_response = wrap(
        get_response,
        RedirectMiddleware(allow_redirects=allow_redirects, cookies=self.cookies),
    )

    auth_middleware = self._get_auth_middleware(
        request=request,
        trust_env=self.trust_env if trust_env is None else trust_env,
        auth=self.auth if auth is None else auth,
    )

    if auth_middleware is not None:
        get_response = wrap(get_response, auth_middleware)

    return await get_response(request)