Skip to content

Commit

Permalink
Mock less
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Sep 10, 2017
1 parent a60bfb4 commit fc19160
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,36 @@ def tearDown(self):
self.loop.close()
gc.collect()

@mock.patch('aiohttp.connector.ClientRequest')
def test_connect(self, ClientRequestMock):
def test_connect(self):
req = ClientRequest(
'GET', URL('http://www.python.org'),
proxy=URL('http://proxy.example.com'),
loop=self.loop
loop=self.loop,
)
self.assertEqual(str(req.proxy), 'http://proxy.example.com')

# mock all the things!
proto = mock.Mock()
connector = aiohttp.TCPConnector(loop=self.loop)
connector._create_proxy_connection = mock.MagicMock(
side_effect=connector._create_proxy_connection)
connector._create_direct_connection = mock.MagicMock(
side_effect=connector._create_direct_connection)
connector._resolve_host = make_mocked_coro([mock.MagicMock()])

proto = mock.Mock()
self.loop.create_connection = make_mocked_coro(
(proto.transport, proto))
conn = self.loop.run_until_complete(connector.connect(req))
self.assertEqual(req.url, URL('http://www.python.org'))
self.assertIs(conn._protocol, proto)
self.assertIs(conn.transport, proto.transport)

ClientRequestMock.assert_called_with(
'GET', URL('http://proxy.example.com'),
auth=None,
headers={'Host': 'www.python.org'},
loop=self.loop)
connector._create_proxy_connection.assert_called_with(req)
((proxy_req,), _) = connector._create_direct_connection.call_args
self.assertEqual(proxy_req.method, 'GET')
self.assertEqual(proxy_req.url, URL('http://proxy.example.com'))
self.assertIsNone(proxy_req.auth)
self.assertEqual(proxy_req.headers['Host'], 'www.python.org')
self.assertIs(proxy_req.loop, self.loop)

def test_proxy_auth(self):
with self.assertRaises(ValueError) as ctx:
Expand Down

0 comments on commit fc19160

Please sign in to comment.