4

I want to write tests for a web client. As it was recommended by the aiohttp library author, I set up a local test server. This setup (boiling down this example) works well with plain http/port 80.

import ssl
import socket as skt
import unittest as ut

import aiohttp as ah
import aiohttp.web as ahweb
import aiohttp.test_utils as ahtest


EXPECTED_SERVER_URL = 'https://anywhere.com'


async def function_to_test(client_session):
    async with client_session.get(EXPECTED_SERVER_URL) as response:
        return await response.json()


class ResolverMock(ah.resolver.AbstractResolver):
    def __init__(self, server_port, host, request_port):
        self.__server_port = server_port
        self.__host = host.rstrip('/').rpartition('://')[-1]
        self.__request_port = request_port

    async def resolve(self, host, request_port=0, family=skt.AF_INET):
        if host != self.__host or request_port != self.__request_port:
            raise OSError('No test server known for %s' % host)
        return [dict(hostname=host, host='127.0.0.1', port=self.__server_port,
                     family=skt.AF_INET, proto=0, flags=skt.AI_NUMERICHOST)]

    async def close(self) -> None:
        pass


class ServerMock(ahtest.RawTestServer):
    def __init__(self, **kwargs):
        super().__init__(self.__handle_request, **kwargs)

    async def __handle_request(self, _request):
        return ahweb.json_response({})


class Fetch(ut.IsolatedAsyncioTestCase):
    async def test_request_server(self):
        async with ServerMock(port=443) as server_mock:
            ssl_ctx = False
            resolver = ResolverMock(server_port=server_mock.port, host=EXPECTED_SERVER_URL, request_port=443)
            connector = ah.TCPConnector(resolver=resolver, ssl=ssl_ctx, use_dns_cache=False)
            async with ah.ClientSession(connector=connector, trust_env=True) as session:
                response = await function_to_test(session)

        self.assertEqual(response, {})


if __name__ == '__main__':
    ut.main()

But it fails for https/port 443 with aiohttp.client_exceptions.ClientConnectorSSLError: Cannot connect to host anywhere.com:443 ssl:default [[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1108)]

Using a non_validating ssl context did not change anything:

            ssl_ctx = ssl.create_default_context()
            ssl_ctx.check_hostname = False
            ssl_ctx.verify_mode = ssl.CERT_NONE

I have no idea how I could set up a compatible protocol or whatever (I am far away from being an ssl expert) on the test server. What do I miss? How can I make this work for https?

CodeNStuff
  • 266
  • 3
  • 11
  • The error is not about a certificate problem so the your attempt to disable certificate validation does not help. Instead the error is because you are not starting a HTTPS server but instead only a HTTP server. Just by switching the port does not magically make a HTTP server HTTPS, instead you need to have the TLS stuff, certificates etc. – Steffen Ullrich Sep 15 '20 at 12:16
  • "Just by switching the port does not magically make a HTTP server HTTPS" A pity. It is somehow what I expect from a test server provided by the library. Otherwise it is way to much effort to follow the recommended way of testing. For most clients I don't deal with setting up SSL stuff but just put together the right requests. Nobody expects me to rebuild the whole server API as well. – CodeNStuff Sep 16 '20 at 08:15
  • *"It is somehow what I expect from a test server .."* - actually I would not expect it. The port is just a layer 4, i.e. TCP level feature and is in not directly associated with a specific application protocol. One could run a HTTP server on port 8000, 3000 ... (which are actually common). Which application protocol to choose from http or https I would expect from a different setting - like `scheme`, which is also documented exactly for this. – Steffen Ullrich Sep 16 '20 at 16:33
  • " I would expect from a different setting - like scheme" - On that point my post was incomplete. Tried that. Did not help - regardless any documentation. – CodeNStuff Sep 17 '20 at 20:43

0 Answers0