Browse Source

Added aiosocks.open_connection (asyncio.open_connection analog)

main
nibrag 8 years ago
parent
commit
94c1683955
2 changed files with 33 additions and 0 deletions
  1. +10
    -0
      aiosocks/__init__.py
  2. +23
    -0
      tests/test_create_connect.py

+ 10
- 0
aiosocks/__init__.py View File

@@ -85,3 +85,13 @@ def create_connection(protocol_factory, proxy, proxy_auth, dst, *,
raise

return protocol.app_transport, protocol.app_protocol


@asyncio.coroutine
def open_connection(proxy, proxy_auth, dst, *, remote_resolve=True,
loop=None, limit=DEFAULT_LIMIT, **kwds):
_, protocol = yield from create_connection(
None, proxy, proxy_auth, dst, reader_limit=limit,
remote_resolve=remote_resolve, loop=loop, **kwds)

return protocol.reader, protocol.writer

+ 23
- 0
tests/test_create_connect.py View File

@@ -109,3 +109,26 @@ class TestCreateConnection(unittest.TestCase):
None, addr, auth, dst, loop=loop_mock
)
self.loop.run_until_complete(conn)

@mock.patch('aiosocks.asyncio.Future')
def test_open_connection(self, future_mock):
addr = aiosocks.Socks5Addr('localhost')
auth = aiosocks.Socks5Auth('usr', 'pwd')
dst = ('python.org', 80)

transp, proto = mock.Mock(), mock.Mock()
reader, writer = mock.Mock(), mock.Mock()

proto.app_protocol.reader, proto.app_protocol.writer = reader, writer

loop_mock = mock.Mock()
loop_mock.create_connection = fake_coroutine((transp, proto))

fut = fake_coroutine(True)
future_mock.side_effect = fut.side_effect

conn = aiosocks.open_connection(addr, auth, dst, loop=loop_mock)
r, w = self.loop.run_until_complete(conn)

self.assertIs(reader, r)
self.assertIs(writer, w)

Loading…
Cancel
Save