|  | 
| 2 | 2 | from urllib.parse import urlparse | 
| 3 | 3 | from yarl import URL | 
| 4 | 4 | 
 | 
| 5 |  | -from aiohttp import ClientResponse | 
| 6 |  | -from aiohttp.client_exceptions import ServerDisconnectedError | 
|  | 5 | +from aiohttp import ClientResponse, web | 
| 7 | 6 | from aiohttp.helpers import TimerNoop | 
| 8 |  | -from aiohttp.test_utils import make_mocked_coro | 
|  | 7 | +from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop, make_mocked_coro | 
| 9 | 8 | import jsonschema | 
| 10 | 9 | import pytest | 
| 11 | 10 | from requests import Response | 
| @@ -1946,39 +1945,6 @@ async def test_history_async_post(loop, session): | 
| 1946 | 1945 |     assert latest.status_code == 500 | 
| 1947 | 1946 | 
 | 
| 1948 | 1947 | 
 | 
| 1949 |  | -@pytest.mark.asyncio | 
| 1950 |  | -async def test_on_request_chunk_sent_async_hook(): | 
| 1951 |  | -    data_sent = make_mocked_coro() | 
| 1952 |  | -    s = Session( | 
| 1953 |  | -        'http://localhost/api', | 
| 1954 |  | -        schema=api_schema_all, | 
| 1955 |  | -        enable_async=True, | 
| 1956 |  | -        enable_history=True, | 
| 1957 |  | -        event_hooks={'on_request_chunk_sent': data_sent} | 
| 1958 |  | -    ) | 
| 1959 |  | - | 
| 1960 |  | -    s.create('leases') | 
| 1961 |  | -    a = s.create('leases') | 
| 1962 |  | -    assert a.is_dirty | 
| 1963 |  | -    a.lease_id = '1' | 
| 1964 |  | -    a.active_status = 'pending' | 
| 1965 |  | -    a.reference_number = 'test' | 
| 1966 |  | -    with pytest.raises(ConnectionRefusedError): | 
| 1967 |  | -        await a.commit() | 
| 1968 |  | -    assert data_sent.called | 
| 1969 |  | -    assert json.loads(data_sent.call_args[0][2].chunk) == { | 
| 1970 |  | -        'data': { | 
| 1971 |  | -            'attributes': { | 
| 1972 |  | -                'active-status': 'pending', | 
| 1973 |  | -                'lease-id': '1', | 
| 1974 |  | -                'reference-number': 'test' | 
| 1975 |  | -            }, | 
| 1976 |  | -            'relationships': {}, | 
| 1977 |  | -            'type': 'leases' | 
| 1978 |  | -        } | 
| 1979 |  | -    } | 
| 1980 |  | - | 
| 1981 |  | - | 
| 1982 | 1948 | def test_set_event_hooks_for_requests(): | 
| 1983 | 1949 |     """Event hooks for requests library is a keyword argument | 
| 1984 | 1950 |        so this test only tests that the request_kwargs are updated correctly. | 
| @@ -2018,3 +1984,76 @@ def test_set_event_hooks_for_requests(): | 
| 2018 | 1984 |     ) | 
| 2019 | 1985 |     assert 'hooks' in s._request_kwargs | 
| 2020 | 1986 |     assert s._request_kwargs.get('hooks') == {'test': None} | 
|  | 1987 | + | 
|  | 1988 | + | 
|  | 1989 | +class TestEventHooks(AioHTTPTestCase): | 
|  | 1990 | +    async def get_application(self): | 
|  | 1991 | +        async def leases_create(request): | 
|  | 1992 | +            headers = {'Content-Type': 'application/vnd.api+json'} | 
|  | 1993 | +            data = {'errors': [{'title': 'Internal server error'}]} | 
|  | 1994 | +            data = json.dumps(data) | 
|  | 1995 | +            return web.Response(body=data, status=500, headers=headers) | 
|  | 1996 | + | 
|  | 1997 | +        app = web.Application() | 
|  | 1998 | +        app.router.add_post('/api/leases', leases_create) | 
|  | 1999 | +        return app | 
|  | 2000 | + | 
|  | 2001 | +    @unittest_run_loop | 
|  | 2002 | +    async def test_on_request_chunk_sent_async_hook(self): | 
|  | 2003 | +        data_sent = make_mocked_coro() | 
|  | 2004 | + | 
|  | 2005 | +        url = f'http://{self.server.host}:{self.server.port}/api' | 
|  | 2006 | +        s = Session( | 
|  | 2007 | +            url, | 
|  | 2008 | +            schema=api_schema_all, | 
|  | 2009 | +            enable_async=True, | 
|  | 2010 | +            enable_history=True, | 
|  | 2011 | +            event_hooks={'on_request_chunk_sent': data_sent} | 
|  | 2012 | +        ) | 
|  | 2013 | + | 
|  | 2014 | +        s.create('leases') | 
|  | 2015 | +        a = s.create('leases') | 
|  | 2016 | +        assert a.is_dirty | 
|  | 2017 | +        a.lease_id = '1' | 
|  | 2018 | +        a.active_status = 'pending' | 
|  | 2019 | +        a.reference_number = 'test' | 
|  | 2020 | +        with pytest.raises(DocumentError): | 
|  | 2021 | +            await a.commit() | 
|  | 2022 | +        assert data_sent.called | 
|  | 2023 | +        assert json.loads(data_sent.call_args[0][2].chunk) == { | 
|  | 2024 | +            'data': { | 
|  | 2025 | +                'attributes': { | 
|  | 2026 | +                    'active-status': 'pending', | 
|  | 2027 | +                    'lease-id': '1', | 
|  | 2028 | +                    'reference-number': 'test' | 
|  | 2029 | +                }, | 
|  | 2030 | +                'relationships': {}, | 
|  | 2031 | +                'type': 'leases' | 
|  | 2032 | +            } | 
|  | 2033 | +        } | 
|  | 2034 | + | 
|  | 2035 | +    @unittest_run_loop | 
|  | 2036 | +    async def test_on_response_chunk_received_async_hook(self): | 
|  | 2037 | +        data_received = make_mocked_coro() | 
|  | 2038 | + | 
|  | 2039 | +        url = f'http://{self.server.host}:{self.server.port}/api' | 
|  | 2040 | +        s = Session( | 
|  | 2041 | +            url, | 
|  | 2042 | +            schema=api_schema_all, | 
|  | 2043 | +            enable_async=True, | 
|  | 2044 | +            enable_history=True, | 
|  | 2045 | +            event_hooks={'on_response_chunk_received': data_received} | 
|  | 2046 | +        ) | 
|  | 2047 | + | 
|  | 2048 | +        s.create('leases') | 
|  | 2049 | +        a = s.create('leases') | 
|  | 2050 | +        assert a.is_dirty | 
|  | 2051 | +        a.lease_id = '1' | 
|  | 2052 | +        a.active_status = 'pending' | 
|  | 2053 | +        a.reference_number = 'test' | 
|  | 2054 | +        with pytest.raises(DocumentError): | 
|  | 2055 | +            await a.commit() | 
|  | 2056 | +        assert data_received.called | 
|  | 2057 | +        assert json.loads(data_received.call_args[0][2].chunk) == { | 
|  | 2058 | +            'errors': [{'title': 'Internal server error'}] | 
|  | 2059 | +        } | 
0 commit comments