mindflayer / python-mocket

a socket mock framework - for all kinds of socket animals, web-clients included
BSD 3-Clause "New" or "Revised" License
280 stars 42 forks source link

Support httpx #182

Closed gregbrowndev closed 2 years ago

gregbrowndev commented 2 years ago

Hi,

I'm in the process of converting my requests-based application to httpx, and therefore also having to convert all the responses-based mocks within my tests. I wanted to avoid coupling to another client-specific test library like RESPX and instead use mocket.

However, I get an error when trying to instantiate httpx's AsyncClient. Here's a simple async pytest:

import json

import httpx
from mocket import async_mocketize
from mocket.mockhttp import Entry

@async_mocketize
async def test_mocket():
    async with httpx.AsyncClient() as client:
        url = "https://example.org/"
        data = {"message": "Hello"}

        Entry.single_register(
            Entry.GET,
            url,
            body=json.dumps(data),
            headers={'content-type': 'application/json'}
        )

        response = await client.get(url)

        assert response.json() == data
Stacktrack ``` tests/adapters/junifer/client/test_mocket.py:7 (test_mocket) @async_mocketize async def test_mocket(): > async with httpx.AsyncClient() as client: test_mocket.py:10: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_client.py:1394: in __init__ self._transport = self._init_transport( /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_client.py:1442: in _init_transport return AsyncHTTPTransport( /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_transports/default.py:261: in __init__ ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env) /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_config.py:49: in create_ssl_context return SSLConfig( /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_config.py:73: in __init__ self.ssl_context = self.load_ssl_context() /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_config.py:85: in load_ssl_context return self.load_ssl_context_verify() /Users/gregorybrown/Library/Caches/pypoetry/virtualenvs/tariff-management-6yv2RoDp-py3.10/lib/python3.10/site-packages/httpx/_config.py:122: in load_ssl_context_verify context = self._create_default_ssl_context() _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = def _create_default_ssl_context(self) -> ssl.SSLContext: """ Creates the default SSLContext object that's used for both verified and unverified connections. """ context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) set_minimum_tls_version_1_2(context) context.options |= ssl.OP_NO_COMPRESSION > context.set_ciphers(DEFAULT_CIPHERS) E TypeError: 'NoneType' object is not callable ```

Is httpx not supported? There are no references to it in the source or docs.

Thanks!

mindflayer commented 2 years ago

Hi @gregbrowndev, thanks for opening this issue. I don't think it has been tested before, but we should be able to make it work. I believe It's calling a method that I am not defining on FakeSSLContext side. You could try to add set_ciphers to this tuple. If you don't want to or cannot try, I'll give it a shot when I am done with work.

mindflayer commented 2 years ago

It may fail for a similar reason after you add that one, the fix would still be adding what's missing. I hope you won't see anything worse than that.

gregbrowndev commented 2 years ago

Thanks, @mindflayer, for the quick response on this!

I'll struggle to be able to try it today because I have plans after work. I'll try to investigate when I get the chance.

mindflayer commented 2 years ago

I tried the fix and it worked, but it looks like there is something more profound I have to investigate.

mindflayer commented 2 years ago

Here is a new version supporting httpx: https://pypi.org/project/mocket/3.10.7/