Debian Codegraph @

tests/test_create_connection.py

raw symbols SCIP
1 try:
2 from unittest import mock
3 except (ImportError, AttributeError):
4 import mock
5
6 import pytest
7 import socket
8 import rfc6555
9 from .test_utils import requires_network
10
11
12 class _BasicCreateConnectionTests(object):
13 @requires_network
14 def test_create_connection_google(self):
15 sock = rfc6555.create_connection(('www.google.com', 80))
16
17 @pytest.mark.parametrize('timeout', [None, 5.0])
18 def test_create_connection_has_proper_timeout(self, timeout):
19 sock = rfc6555.create_connection(('www.google.com', 80), timeout=timeout)
20
21 assert sock.gettimeout() == timeout
22
23 def test_create_connection_with_source_address_calls_bind(self):
24 sock = mock.Mock()
25 with mock.patch('socket.socket') as fake_socket:
26 fake_socket.return_value = sock
27
28 sock.getsockopt.return_value = 0
29 sock.connect_ex.return_value = 0
30 sock.gettimeout.return_value = None
31
32 try:
33 rfc6555.create_connection(('::1', 0), source_address=('::1', 123))
34 except Exception:
35 pass
36
37 sock.bind.assert_called_with(('::1', 123))
38
39 def test_getaddr_info_empty_list(self):
40 with mock.patch('socket.getaddrinfo') as fake_getaddrinfo:
41 fake_getaddrinfo.return_value = []
42
43 with pytest.raises(socket.error):
44 rfc6555.create_connection(('::1', 0))
45
46 @requires_network
47 def test_create_connection_cached_value(self):
48 sock = rfc6555.create_connection(('www.google.com', 80))
49 sock2 = rfc6555.create_connection(('www.google.com', 80))
50
51
52 class TestCreateConnectionTestRFC6555Default(_BasicCreateConnectionTests):
53 pass
54
55
56 class TestCreateConnectionTestRFC6555Enabled(_BasicCreateConnectionTests):
57 def setup_method(self, test_method):
58 rfc6555.RFC6555_ENABLED = True
59
60
61 class TestCreateConnectionTestRFC6555Disabled(_BasicCreateConnectionTests):
62 def setup_method(self, test_method):
63 rfc6555.RFC6555_ENABLED = False