]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Attributes set on VCRConnection now also get set on the real_connection
authorTye Wang <tye.wang@venmo.com>
Thu, 3 Sep 2015 18:35:28 +0000 (14:35 -0400)
committerTye Wang <tye.wang@venmo.com>
Thu, 3 Sep 2015 18:35:28 +0000 (14:35 -0400)
tests/unit/test_stubs.py [new file with mode: 0644]
vcr/stubs/__init__.py

diff --git a/tests/unit/test_stubs.py b/tests/unit/test_stubs.py
new file mode 100644 (file)
index 0000000..7e51450
--- /dev/null
@@ -0,0 +1,7 @@
+from vcr.stubs import VCRHTTPSConnection
+
+class TestVCRConnection(object):
+    def test_setting_of_attributes_get_propogated_to_real_connection(self):
+        vcr_connection = VCRHTTPSConnection('www.examplehost.com')
+        vcr_connection.ssl_version = 'example_ssl_version'
+        assert vcr_connection.real_connection.ssl_version == 'example_ssl_version'
index 19d57f1ef79d6b581ec1db077015bd9fbb118d28..f266ecfa96401fe3e723e2f2b7d49e6ca53b99dc 100644 (file)
@@ -315,6 +315,22 @@ class VCRConnection(object):
         with force_reset():
             self.real_connection = self._baseclass(*args, **kwargs)
 
+    def __setattr__(self, name, value):
+        """
+        We need to define this because any attributes that are set on the
+        VCRConnection need to be propogated to the real connection.
+
+        For example, urllib3 will set certain attributes on the connection,
+        such as 'ssl_version'. These attributes need to get set on the real
+        connection to have the correct and expected behavior.
+        """
+        try:
+            setattr(self.real_connection, name, value)
+        except AttributeError: # raised if real_connection has not been set yet
+            pass
+
+        super(VCRConnection, self).__setattr__(name, value)
+
 
 class VCRHTTPConnection(VCRConnection):
     '''A Mocked class for HTTP requests'''