Ejemplo n.º 1
0
    def test_https_connect_runtime_error(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))

        connector = aiohttp.ProxyConnector(
            'http://proxy.example.com', loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
        with self.assertRaisesRegex(
                RuntimeError, "Transport does not expose socket instance"):
            self.loop.run_until_complete(connector._create_connection(req))

        proxy_req.close()
        proxy_resp.close()
        req.close()
Ejemplo n.º 2
0
    def test_auth_from_url(self, ClientRequestMock):
        proxy_req = ClientRequest('GET', 'http://*****:*****@proxy.example.com')
        ClientRequestMock.return_value = proxy_req
        self.assertIn('AUTHORIZATION', proxy_req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)

        loop_mock = unittest.mock.Mock()
        connector = aiohttp.ProxyConnector(
            'http://*****:*****@proxy.example.com', loop=loop_mock)
        connector._resolve_host = resolve_mock = unittest.mock.Mock()
        self._fake_coroutine(resolve_mock, [unittest.mock.MagicMock()])

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'http://www.python.org')
        self.assertNotIn('AUTHORIZATION', req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
        self.loop.run_until_complete(connector.connect(req))

        self.assertEqual(req.path, 'http://www.python.org/')
        self.assertNotIn('AUTHORIZATION', req.headers)
        self.assertIn('PROXY-AUTHORIZATION', req.headers)
        self.assertNotIn('AUTHORIZATION', proxy_req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)

        ClientRequestMock.assert_called_with(
            'GET',
            'http://*****:*****@proxy.example.com',
            auth=None,
            loop=unittest.mock.ANY,
            headers=unittest.mock.ANY)
Ejemplo n.º 3
0
    def test_https_connect(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET',
                                  'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))

        connector = aiohttp.ProxyConnector('http://proxy.example.com',
                                           loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org')
        self.loop.run_until_complete(connector._create_connection(req))

        self.assertEqual(proxy_req.method, 'CONNECT')
        self.assertEqual(proxy_req.path, 'www.python.org:443')
        tr.pause_reading.assert_called_once_with()
        tr.get_extra_info.assert_called_once_with('socket', default=None)
Ejemplo n.º 4
0
    def test_https_connect_http_proxy_error(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET',
                                  'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(
            start_mock, unittest.mock.Mock(status=400, reason='bad request'))

        connector = aiohttp.ProxyConnector('http://proxy.example.com',
                                           loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org')
        with self.assertRaisesRegex(aiohttp.HttpProxyError,
                                    "400, message='bad request'"):
            self.loop.run_until_complete(connector._create_connection(req))
Ejemplo n.º 5
0
    def test_no_content_length(self):
        req = ClientRequest('get', 'http://python.org', loop=self.loop)
        req.send(self.transport, self.protocol)
        self.assertEqual('0', req.headers.get('CONTENT-LENGTH'))

        req = ClientRequest('head', 'http://python.org', loop=self.loop)
        req.send(self.transport, self.protocol)
        self.assertEqual('0', req.headers.get('CONTENT-LENGTH'))
Ejemplo n.º 6
0
    def test_method(self):
        req = ClientRequest('get', 'http://python.org/')
        self.assertEqual(req.method, 'GET')

        req = ClientRequest('head', 'http://python.org/')
        self.assertEqual(req.method, 'HEAD')

        req = ClientRequest('HEAD', 'http://python.org/')
        self.assertEqual(req.method, 'HEAD')
Ejemplo n.º 7
0
    def test_path_is_not_double_encoded(self):
        req = ClientRequest('get', "http://0.0.0.0/get/test case")
        self.assertEqual(req.path, "/get/test%20case")

        req = ClientRequest('get', "http://0.0.0.0/get/test%2fcase")
        self.assertEqual(req.path, "/get/test%2fcase")

        req = ClientRequest('get', "http://0.0.0.0/get/test%20case")
        self.assertEqual(req.path, "/get/test%20case")
Ejemplo n.º 8
0
    def test_basic_auth_from_url(self):
        req = ClientRequest('get', 'http://*****:*****@python.org')
        self.assertIn('AUTHORIZATION', req.headers)
        self.assertEqual('Basic bmtpbToxMjM0', req.headers['AUTHORIZATION'])

        req = ClientRequest('get',
                            'http://[email protected]',
                            auth=aiohttp.helpers.BasicAuth('nkim', '1234'))
        self.assertIn('AUTHORIZATION', req.headers)
        self.assertEqual('Basic bmtpbToxMjM0', req.headers['AUTHORIZATION'])
Ejemplo n.º 9
0
    def test_params_are_added_before_fragment(self):
        req = ClientRequest('GET',
                            "http://example.com/path#fragment",
                            params={"a": "b"})
        self.assertEqual(req.path, "/path?a=b#fragment")

        req = ClientRequest('GET',
                            "http://example.com/path?key=value#fragment",
                            params={"a": "b"})
        self.assertEqual(req.path, "/path?key=value&a=b#fragment")
Ejemplo n.º 10
0
    def test_cookies(self):
        req = ClientRequest(
            'get', 'http://test.com/path', cookies={'cookie1': 'val1'})
        self.assertIn('COOKIE', req.headers)
        self.assertEqual('cookie1=val1', req.headers['COOKIE'])

        req = ClientRequest(
            'get', 'http://test.com/path',
            headers={'cookie': 'cookie1=val1'},
            cookies={'cookie2': 'val2'})
        self.assertEqual('cookie1=val1; cookie2=val2', req.headers['COOKIE'])
Ejemplo n.º 11
0
    def test_chunked(self):
        req = ClientRequest(
            'get', 'http://python.org/',
            headers={'TRANSFER-ENCODING': 'gzip'}, loop=self.loop)
        req.send(self.transport, self.protocol)
        self.assertEqual('gzip', req.headers['TRANSFER-ENCODING'])

        req = ClientRequest(
            'get', 'http://python.org/',
            headers={'Transfer-encoding': 'chunked'}, loop=self.loop)
        req.send(self.transport, self.protocol)
        self.assertEqual('chunked', req.headers['TRANSFER-ENCODING'])
Ejemplo n.º 12
0
    def test_expect100(self):
        req = ClientRequest('get', 'http://python.org/',
                            expect100=True, loop=self.loop)
        req.send(self.transport, self.protocol)
        self.assertEqual('100-continue', req.headers['EXPECT'])
        self.assertIsNotNone(req._continue)

        req = ClientRequest('get', 'http://python.org/',
                            headers={'expect': '100-continue'}, loop=self.loop)
        req.send(self.transport, self.protocol)
        self.assertEqual('100-continue', req.headers['EXPECT'])
        self.assertIsNotNone(req._continue)
Ejemplo n.º 13
0
    def test_unicode_get(self):
        def join(*suffix):
            return urllib.parse.urljoin('http://python.org/', '/'.join(suffix))

        url = 'http://python.org'
        req = ClientRequest('get', url, params={'foo': 'f\xf8\xf8'})
        self.assertEqual('/?foo=f%C3%B8%C3%B8', req.path)
        req = ClientRequest('', url, params={'f\xf8\xf8': 'f\xf8\xf8'})
        self.assertEqual('/?f%C3%B8%C3%B8=f%C3%B8%C3%B8', req.path)
        req = ClientRequest('', url, params={'foo': 'foo'})
        self.assertEqual('/?foo=foo', req.path)
        req = ClientRequest('', join('\xf8'), params={'foo': 'foo'})
        self.assertEqual('/%C3%B8?foo=foo', req.path)
Ejemplo n.º 14
0
    def test_request_port(self, ClientRequestMock):
        proxy_req = ClientRequest('GET', 'http://proxy.example.com')
        ClientRequestMock.return_value = proxy_req

        loop_mock = unittest.mock.Mock()
        connector = aiohttp.ProxyConnector('http://proxy.example.com',
                                           loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'http://localhost:1234/path')
        self.loop.run_until_complete(connector._create_connection(req))
        self.assertEqual(req.path, 'http://localhost:1234/path')
Ejemplo n.º 15
0
    def test_host_port(self):
        req = ClientRequest('get', 'http://python.org/')
        self.assertEqual(req.host, 'python.org')
        self.assertEqual(req.port, 80)
        self.assertFalse(req.ssl)

        req = ClientRequest('get', 'https://python.org/')
        self.assertEqual(req.host, 'python.org')
        self.assertEqual(req.port, 443)
        self.assertTrue(req.ssl)

        req = ClientRequest('get', 'https://python.org:960/')
        self.assertEqual(req.host, 'python.org')
        self.assertEqual(req.port, 960)
        self.assertTrue(req.ssl)
Ejemplo n.º 16
0
def test_connect_with_no_limits(loop, key):
    proto = mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET', URL('http://localhost1:80'), loop=loop)

    conn = aiohttp.BaseConnector(loop=loop, limit=0, limit_per_host=0)
    conn._conns[key] = [(proto, loop.time())]
    conn._create_connection = mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result(proto)

    acquired = False
    connection1 = yield from conn.connect(req)

    @asyncio.coroutine
    def f():
        nonlocal acquired
        connection2 = yield from conn.connect(req)
        acquired = True
        assert 1 == len(conn._acquired)
        assert 1 == len(conn._acquired_per_host[key])
        connection2.release()

    task = helpers.ensure_future(f(), loop=loop)

    yield from asyncio.sleep(0.01, loop=loop)
    assert acquired
    connection1.release()
    yield from task
    conn.close()
def function2543(arg440, function1139):
    var4520 = mock.Mock()
    var4520.is_connected.return_value = True
    var1008 = ClientRequest('GET', URL('http://localhost1:80'), loop=arg440)
    var4698 = aiohttp.BaseConnector(loop=arg440, limit=1000, limit_per_host=1)
    var4698._conns[function1139] = [(var4520, arg440.time())]
    var4698._create_connection = mock.Mock()
    var4698._create_connection.return_value = helpers.create_future(arg440)
    var4698._create_connection.return_value.set_result(var4520)
    var3298 = False
    var4590 = yield from var4698.connect(var1008)

    @asyncio.coroutine
    def function301():
        nonlocal acquired
        var916 = yield from var4698.connect(var1008)
        var3298 = True
        assert (1 == len(var4698._acquired))
        assert (1 == len(var4698._acquired_per_host[function1139]))
        var916.release()

    var3195 = helpers.ensure_future(function301(), loop=arg440)
    yield from asyncio.sleep(0.01, loop=arg440)
    assert (not var3298)
    var4590.release()
    yield from asyncio.sleep(0, loop=arg440)
    assert acquired
    yield from task
    var4698.close()
def function712(arg516, function1139):
    var4045 = mock.Mock()
    var4045.is_connected.return_value = True
    var384 = ClientRequest('GET', URL('http://localhost1:80'), loop=arg516)
    var3117 = aiohttp.BaseConnector(loop=arg516, limit=0, limit_per_host=1)
    var3117._conns[function1139] = [(var4045, arg516.time())]
    var3117._create_connection = mock.Mock()
    var3117._create_connection.return_value = helpers.create_future(arg516)
    var3117._create_connection.return_value.set_result(var4045)
    var4490 = False
    var712 = yield from var3117.connect(var384)

    @asyncio.coroutine
    def function301():
        nonlocal acquired
        var993 = yield from var3117.connect(var384)
        var4490 = True
        var993.release()

    var4482 = helpers.ensure_future(function301(), loop=arg516)
    yield from asyncio.sleep(0.01, loop=arg516)
    assert (not var4490)
    var712.release()
    yield from asyncio.sleep(0, loop=arg516)
    assert acquired
    yield from task
    var3117.close()
def function2540(arg1260, function1139):
    var3583 = mock.Mock()
    var3583.is_connected.return_value = True
    var3002 = ClientRequest('GET', URL('http://localhost1:80'), loop=arg1260)
    var742 = aiohttp.BaseConnector(loop=arg1260, limit=0, limit_per_host=0)
    var742._conns[function1139] = [(var3583, arg1260.time())]
    var742._create_connection = mock.Mock()
    var742._create_connection.return_value = helpers.create_future(arg1260)
    var742._create_connection.return_value.set_result(var3583)
    var3355 = False
    var3007 = yield from var742.connect(var3002)

    @asyncio.coroutine
    def function301():
        nonlocal acquired
        var2225 = yield from var742.connect(var3002)
        var3355 = True
        assert (1 == len(var742._acquired))
        assert (1 == len(var742._acquired_per_host[function1139]))
        var2225.release()

    var2804 = helpers.ensure_future(function301(), loop=arg1260)
    yield from asyncio.sleep(0.01, loop=arg1260)
    assert acquired
    var3007.release()
    yield from task
    var742.close()
Ejemplo n.º 20
0
    def test_connect(self, ClientRequestMock):
        req = ClientRequest('GET', 'http://www.python.org')
        self.assertEqual(req.path, '/')

        loop_mock = unittest.mock.Mock()
        connector = aiohttp.ProxyConnector('http://proxy.example.com',
                                           loop=loop_mock)
        self.assertIs(loop_mock, connector._loop)

        resolve_host = unittest.mock.Mock()
        self._fake_coroutine(resolve_host, [unittest.mock.MagicMock()])
        connector._resolve_host = resolve_host

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))
        conn = self.loop.run_until_complete(connector.connect(req))
        self.assertEqual(req.path, 'http://www.python.org/')
        self.assertIs(conn._transport, tr)
        self.assertIs(conn._protocol, proto)

        # resolve_host.assert_called_once_with('proxy.example.com', 80)
        self.assertEqual(tr.mock_calls, [])

        ClientRequestMock.assert_called_with(
            'GET',
            'http://proxy.example.com',
            auth=None,
            headers={'Host': 'www.python.org'},
            loop=loop_mock)
Ejemplo n.º 21
0
 def test_auth_utf8(self):
     proxy_req = ClientRequest('GET',
                               'http://proxy.example.com',
                               auth=aiohttp.helpers.BasicAuth(
                                   'юзер', 'пасс', 'utf-8'),
                               loop=self.loop)
     self.assertIn('AUTHORIZATION', proxy_req.headers)
Ejemplo n.º 22
0
    def test_data_stream_exc_chain(self):
        fut = asyncio.Future(loop=self.loop)

        def gen():
            yield from fut

        req = ClientRequest('POST',
                            'http://python.org/',
                            data=gen(),
                            loop=self.loop)

        inner_exc = ValueError()

        @asyncio.coroutine
        def exc():
            yield from asyncio.sleep(0.01, loop=self.loop)
            fut.set_exception(inner_exc)

        asyncio. async (exc(), loop=self.loop)

        resp = req.send(self.transport, self.protocol)
        resp.connection = self.connection
        self.loop.run_until_complete(req._writer)
        self.assertTrue(self.connection.close.called)
        self.assertTrue(self.protocol.set_exception.called)
        outer_exc = self.protocol.set_exception.call_args[0][0]
        self.assertIsInstance(outer_exc, aiohttp.ClientRequestError)
        self.assertIs(inner_exc, outer_exc.__context__)
        self.assertIs(inner_exc, outer_exc.__cause__)
Ejemplo n.º 23
0
    def test_data_stream_continue(self):
        def gen():
            yield b'binary data'
            return b' result'

        req = ClientRequest('POST',
                            'http://python.org/',
                            data=gen(),
                            expect100=True,
                            loop=self.loop)
        self.assertTrue(req.chunked)
        self.assertTrue(inspect.isgenerator(req.body))

        def coro():
            yield from asyncio.sleep(0.0001, loop=self.loop)
            req._continue.set_result(1)

        asyncio. async (coro(), loop=self.loop)

        req.send(self.transport, self.protocol)
        self.loop.run_until_complete(req._writer)
        self.assertEqual(self.transport.write.mock_calls[-3:], [
            unittest.mock.call(b'binary data result'),
            unittest.mock.call(b'\r\n'),
            unittest.mock.call(b'0\r\n\r\n')
        ])
Ejemplo n.º 24
0
    def test_data_stream_exc(self):
        fut = asyncio.Future(loop=self.loop)

        def gen():
            yield b'binary data'
            yield from fut

        req = ClientRequest('POST',
                            'http://python.org/',
                            data=gen(),
                            loop=self.loop)
        self.assertTrue(req.chunked)
        self.assertTrue(inspect.isgenerator(req.body))
        self.assertEqual(req.headers['TRANSFER-ENCODING'], 'chunked')

        @asyncio.coroutine
        def exc():
            yield from asyncio.sleep(0.01, loop=self.loop)
            fut.set_exception(ValueError)

        asyncio. async (exc(), loop=self.loop)

        resp = req.send(self.transport, self.protocol)
        resp.connection = self.connection
        self.loop.run_until_complete(req._writer)
        self.assertTrue(self.connection.close.called)
        self.assertTrue(self.protocol.set_exception.called)
Ejemplo n.º 25
0
 def test_files_and_bytes_data(self):
     with self.assertRaises(ValueError):
         with self.assertWarns(DeprecationWarning):
             ClientRequest('POST',
                           'http://python.org/',
                           data=b'binary data',
                           files={'file': b'file data'})
Ejemplo n.º 26
0
 def test_headers(self):
     req = ClientRequest('get',
                         'http://python.org/',
                         headers={'Content-Type': 'text/plain'})
     self.assertIn('CONTENT-TYPE', req.headers)
     self.assertEqual(req.headers['CONTENT-TYPE'], 'text/plain')
     self.assertEqual(req.headers['ACCEPT-ENCODING'], 'gzip, deflate')
Ejemplo n.º 27
0
def test_connect_with_capacity_cancelled(loop):

    tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET',
                        URL('http://host:80'),
                        loop=loop,
                        response_class=unittest.mock.Mock())

    conn = aiohttp.BaseConnector(loop=loop, capacity=1)
    key = ('host', 80, False)
    conn._conns[key] = [(tr, proto, loop.time())]
    conn._create_connection = unittest.mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result((tr, proto))

    connection = yield from conn.connect(req)
    assert connection._transport == tr

    assert 1 == len(conn._acquired)

    with pytest.raises(asyncio.TimeoutError):
        # limit exhausted
        yield from asyncio.wait_for(conn.connect(req), 0.01, loop=loop)
    connection.close()
Ejemplo n.º 28
0
 def test_basic_auth_tuple_deprecated(self):
     with self.assertWarns(DeprecationWarning):
         req = ClientRequest('get',
                             'http://python.org',
                             auth=('nkim', '1234'))
     self.assertIn('AUTHORIZATION', req.headers)
     self.assertEqual('Basic bmtpbToxMjM0', req.headers['AUTHORIZATION'])
Ejemplo n.º 29
0
def test_close_with_acquired_connection(loop):

    tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
    proto.is_connected.return_value = True

    req = ClientRequest('GET',
                        URL('http://host:80'),
                        loop=loop,
                        response_class=unittest.mock.Mock())

    conn = aiohttp.BaseConnector(loop=loop, capacity=1)
    key = ('host', 80, False)
    conn._conns[key] = [(tr, proto, loop.time())]
    conn._create_connection = unittest.mock.Mock()
    conn._create_connection.return_value = helpers.create_future(loop)
    conn._create_connection.return_value.set_result((tr, proto))

    connection = yield from conn.connect(req)

    assert 1 == len(conn._acquired)
    conn.close()
    assert 0 == len(conn._acquired)
    assert conn.closed
    tr.close.assert_called_with()

    assert not connection.closed
    connection.close()
    assert connection.closed
Ejemplo n.º 30
0
 def test_get_with_data(self):
     for meth in ClientRequest.GET_METHODS:
         req = ClientRequest(meth,
                             'http://python.org/',
                             data={'life': '42'})
         self.assertEqual('/', req.path)
         self.assertEqual(b'life=42', req.body)