From 79ff59feae205d5dc882f184ffcb59235919db23 Mon Sep 17 00:00:00 2001 From: Tye Wang Date: Thu, 3 Sep 2015 14:35:28 -0400 Subject: [PATCH 1/2] Attributes set on VCRConnection now also get set on the real_connection --- tests/unit/test_stubs.py | 7 +++++++ vcr/stubs/__init__.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/unit/test_stubs.py diff --git a/tests/unit/test_stubs.py b/tests/unit/test_stubs.py new file mode 100644 index 0000000..7e51450 --- /dev/null +++ b/tests/unit/test_stubs.py @@ -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' diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index 19d57f1..f266ecf 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -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''' From ac510097e0a9640d2e525c17accb2de2bb3875a3 Mon Sep 17 00:00:00 2001 From: Tye Wang Date: Fri, 18 Sep 2015 12:19:17 -0400 Subject: [PATCH 2/2] Add TODO and elaborate on comment --- vcr/stubs/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index f266ecf..0d9af5d 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -323,10 +323,15 @@ class VCRConnection(object): 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. + + TODO: Separately setting the attribute on the two instances is not + ideal. We should switch to a proxying implementation. """ try: setattr(self.real_connection, name, value) - except AttributeError: # raised if real_connection has not been set yet + except AttributeError: + # raised if real_connection has not been set yet, such as when + # we're setting the real_connection itself for the first time pass super(VCRConnection, self).__setattr__(name, value)