-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Session created while cassette is active is still attached to cassette when context manager exits #109
Comments
traceback: tmpdir = local('/private/var/folders/61/1x6fdz8j3xvgv5z8m0k22r8h0000gn/T/pytest-138/test_session_can_make_requests0')
tests/integration/test_requests.py:164: .tox/py34requests22/lib/python3.4/site-packages/requests/sessions.py:395: in get self = <vcr.patch.VCRRequestsHTTPConnection/private/var/folders/61/1x6fdz8j3xvgv5z8m0k22r8h0000gn/T/pytest-138/test_session_can_make_requests0/test_session_after_unpatched.yaml object at 0x10bc8c550>, _ = False
|
Hmm, is session holding a copy of the connection that needs to be unpatched? |
As I mentioned, it has a connection pool instance which can hold reference to an instance of the connection class, so even though the class has been reset in the module, the instance is already of the patched type. This also happens in the other direction. i.e. def test_session_created_before_use_cassette_is_patched(tmpdir, scheme):
url = scheme + '://httpbin.org/bytes/1024'
# Record arbitrary, random data to the cassette
with vcr.use_cassette(str(tmpdir.join('session_created_outside.yaml'))):
session = requests.session()
body = session.get(url).content
# Create a session outside of any cassette context manager
session = requests.session()
# Make a request to make sure that a connectionpool is instantiated
session.get(scheme + '://httpbin.org/get')
with vcr.use_cassette(str(tmpdir.join('session_created_outside.yaml'))):
# These should only be the same if the patching succeeded.
assert session.get(url).content == body fails. I think this is actually a somewhat concerning issue -- If you were to make a fixture that uses its own use cassette and makes some requests on a session that is then accessible in the calling test, the session would still be attached to the fixtures cassette, and it would be unusable in the test context. As a simple example (edit: 2bf23b2 fixes this (but not the other two tests but in a less than desirable way :/) @pytest.fixture
@use_cassette('login.yaml')
def user(self):
return User(session)
@use_cassette('user_test.yaml')
def test_something(user):
user.do_stuff() This test would behave strangely (if we assume that the User object does some type of initialization with the session), because the connection pool could still have some connection objects from the first fixture. |
… another failing test for kevin1024#109
I think that this might have to do with requests using a connection pool which would keep a reference to the any VCRConnection instances.
Here is a test that illustrates the issue:
#108 does not solve this and in fact has another similar issue which is that sessions created in one context do not pick up context from any new context blocks that are created (or at least their behavior is inconsistent in that respect). Here is a failing test for that issue in case you are interested:
The text was updated successfully, but these errors were encountered: