Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

agriconnect / aiohttp   python

Repository URL to install this package:

/ test_utils.py

"""Utilities shared by tests."""

import asyncio
import contextlib
import functools
import gc
import socket
import sys
import unittest
from abc import ABC, abstractmethod
from types import TracebackType
from typing import (  # noqa
from unittest import mock

from multidict import CIMultiDict, CIMultiDictProxy
from yarl import URL

import aiohttp
from aiohttp.client import (

from . import ClientSession, hdrs
from .abc import AbstractCookieJar
from .client_reqrep import ClientResponse  # noqa
from .client_ws import ClientWebSocketResponse  # noqa
from .helpers import sentinel
from .http import HttpVersion, RawRequestMessage
from .signals import Signal
from .web import (
from .web_protocol import _RequestHandler

if TYPE_CHECKING:  # pragma: no cover
    from ssl import SSLContext
    SSLContext = None

def get_unused_port_socket(host: str) -> socket.socket:
    return get_port_socket(host, 0)

def get_port_socket(host: str, port: int) -> socket.socket:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    return s

def unused_port() -> int:
    """Return a port that is unused on the current host."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(('', 0))
        return s.getsockname()[1]

class BaseTestServer(ABC):
    def __init__(self,
                 scheme: Union[str, object]=sentinel,
                 loop: Optional[asyncio.AbstractEventLoop]=None,
                 host: str='',
                 port: Optional[int]=None,
                 skip_url_asserts: bool=False,
                 **kwargs: Any) -> None:
        self._loop = loop
        self.runner = None  # type: Optional[BaseRunner]
        self._root = None  # type: Optional[URL]
        self.host = host
        self.port = port
        self._closed = False
        self.scheme = scheme
        self.skip_url_asserts = skip_url_asserts

    async def start_server(self,
                           loop: Optional[asyncio.AbstractEventLoop]=None,
                           **kwargs: Any) -> None:
        if self.runner:
        self._loop = loop
        self._ssl = kwargs.pop('ssl', None)
        self.runner = await self._make_runner(**kwargs)
        await self.runner.setup()
        if not self.port:
            self.port = 0
        _sock = get_port_socket(self.host, self.port)
        self.host, self.port = _sock.getsockname()[:2]
        site = SockSite(self.runner, sock=_sock, ssl_context=self._ssl)
        await site.start()
        server = site._server
        assert server is not None
        sockets = server.sockets
        assert sockets is not None
        self.port = sockets[0].getsockname()[1]
        if self.scheme is sentinel:
            if self._ssl:
                scheme = 'https'
                scheme = 'http'
            self.scheme = scheme
        self._root = URL('{}://{}:{}'.format(self.scheme,

    @abstractmethod  # pragma: no cover
    async def _make_runner(self, **kwargs: Any) -> BaseRunner:

    def make_url(self, path: str) -> URL:
        assert self._root is not None
        url = URL(path)
        if not self.skip_url_asserts:
            assert not url.is_absolute()
            return self._root.join(url)
            return URL(str(self._root) + path)

    def started(self) -> bool:
        return self.runner is not None

    def closed(self) -> bool:
        return self._closed

    def handler(self) -> Server:
        # for backward compatibility
        # web.Server instance
        runner = self.runner
        assert runner is not None
        assert runner.server is not None
        return runner.server

    async def close(self) -> None:
        """Close all fixtures created by the test client.

        After that point, the TestClient is no longer usable.

        This is an idempotent function: running close multiple times
        will not have any additional effects.

        close is also run when the object is garbage collected, and on
        exit when used as a context manager.

        if self.started and not self.closed:
            assert self.runner is not None
            await self.runner.cleanup()
            self._root = None
            self.port = None
            self._closed = True

    def __enter__(self) -> None:
        raise TypeError("Use async with instead")

    def __exit__(self,
                 exc_type: Optional[Type[BaseException]],
                 exc_value: Optional[BaseException],
                 traceback: Optional[TracebackType]) -> None:
        # __exit__ should exist in pair with __enter__ but never executed
        pass  # pragma: no cover

    async def __aenter__(self) -> 'BaseTestServer':
        await self.start_server(loop=self._loop)
        return self

    async def __aexit__(self,
                        exc_type: Optional[Type[BaseException]],
                        exc_value: Optional[BaseException],
                        traceback: Optional[TracebackType]) -> None:
        await self.close()

class TestServer(BaseTestServer):

    def __init__(self, app: Application, *,
                 scheme: Union[str, object]=sentinel,
                 host: str='',
                 port: Optional[int]=None,
                 **kwargs: Any):
        self.app = app
        super().__init__(scheme=scheme, host=host, port=port, **kwargs)

    async def _make_runner(self, **kwargs: Any) -> BaseRunner:
        return AppRunner(self.app, **kwargs)

class RawTestServer(BaseTestServer):

    def __init__(self, handler: _RequestHandler, *,
                 scheme: Union[str, object]=sentinel,
                 host: str='',
                 port: Optional[int]=None,
                 **kwargs: Any) -> None:
        self._handler = handler
        super().__init__(scheme=scheme, host=host, port=port, **kwargs)

    async def _make_runner(self,
                           debug: bool=True,
                           **kwargs: Any) -> ServerRunner:
        srv = Server(
            self._handler, loop=self._loop, debug=debug, **kwargs)
        return ServerRunner(srv, debug=debug, **kwargs)

class TestClient:
    A test client implementation.

    To write functional tests for aiohttp based servers.


    def __init__(self, server: BaseTestServer, *,
                 cookie_jar: Optional[AbstractCookieJar]=None,
                 loop: Optional[asyncio.AbstractEventLoop]=None,
                 **kwargs: Any) -> None:
        if not isinstance(server, BaseTestServer):
            raise TypeError("server must be TestServer "
                            "instance, found type: %r" % type(server))
        self._server = server
        self._loop = loop
        if cookie_jar is None:
            cookie_jar = aiohttp.CookieJar(unsafe=True, loop=loop)
        self._session = ClientSession(loop=loop,
        self._closed = False
        self._responses = []  # type: List[ClientResponse]
        self._websockets = []  # type: List[ClientWebSocketResponse]

    async def start_server(self) -> None:
        await self._server.start_server(loop=self._loop)

    def host(self) -> str:
        return self._server.host

    def port(self) -> Optional[int]:
        return self._server.port

    def server(self) -> BaseTestServer:
        return self._server

    def app(self) -> Application:
        return getattr(self._server, "app", None)

    def session(self) -> ClientSession:
        """An internal aiohttp.ClientSession.

        Unlike the methods on the TestClient, client session requests
        do not automatically include the host in the url queried, and
        will require an absolute path to the resource.

        return self._session

    def make_url(self, path: str) -> URL:
        return self._server.make_url(path)

    async def request(self, method: str, path: str,
                      **kwargs: Any) -> ClientResponse:
        """Routes a request to tested http server.

        The interface is identical to aiohttp.ClientSession.request,
        except the loop kwarg is overridden by the instance used by the
        test server.

        resp = await self._session.request(
            method, self.make_url(path), **kwargs
        # save it to close later
        return resp

    def get(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP GET request."""
        return _RequestContextManager(
            self.request(hdrs.METH_GET, path, **kwargs)

    def post(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP POST request."""
        return _RequestContextManager(
            self.request(hdrs.METH_POST, path, **kwargs)

    def options(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP OPTIONS request."""
        return _RequestContextManager(
            self.request(hdrs.METH_OPTIONS, path, **kwargs)

    def head(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP HEAD request."""
        return _RequestContextManager(
            self.request(hdrs.METH_HEAD, path, **kwargs)

    def put(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP PUT request."""
        return _RequestContextManager(
            self.request(hdrs.METH_PUT, path, **kwargs)

    def patch(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP PATCH request."""
        return _RequestContextManager(
            self.request(hdrs.METH_PATCH, path, **kwargs)

    def delete(self, path: str, **kwargs: Any) -> _RequestContextManager:
        """Perform an HTTP PATCH request."""
        return _RequestContextManager(
            self.request(hdrs.METH_DELETE, path, **kwargs)

    def ws_connect(self, path: str, **kwargs: Any) -> _WSRequestContextManager:
        """Initiate websocket connection.
Loading ...