Skip to content

Commit

Permalink
Add the alter_live_response option
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti Kaihola committed Jul 5, 2021
1 parent c79a06f commit a9d01cb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 2 additions & 4 deletions vcr/cassette.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def __init__(
match_on=(uri, method),
before_record_request=None,
before_record_response=None,
alter_live_response=False,
custom_patches=(),
inject=False,
allow_playback_repeats=False,
Expand All @@ -191,6 +192,7 @@ def __init__(
self._before_record_request = before_record_request or (lambda x: x)
log.info(self._before_record_request)
self._before_record_response = before_record_response or (lambda x: x)
self._alter_live_response = alter_live_response
self.inject = inject
self.record_mode = record_mode
self.custom_patches = custom_patches
Expand Down Expand Up @@ -229,10 +231,6 @@ def append(self, request, response):
request = self._before_record_request(request)
if not request:
return
# Deepcopy is here because mutation of `response` will corrupt the
# real response.
response = copy.deepcopy(response)
response = self._before_record_response(response)
if response is None:
return
self.data.append((request, response))
Expand Down
3 changes: 3 additions & 0 deletions vcr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(
ignore_localhost=False,
filter_headers=(),
before_record_response=None,
alter_live_response=None,
filter_post_data_parameters=(),
match_on=("method", "scheme", "host", "port", "path", "query"),
before_record=None,
Expand Down Expand Up @@ -75,6 +76,7 @@ def __init__(
self.filter_post_data_parameters = filter_post_data_parameters
self.before_record_request = before_record_request or before_record
self.before_record_response = before_record_response
self.alter_live_response = alter_live_response
self.ignore_hosts = ignore_hosts
self.ignore_localhost = ignore_localhost
self.inject_cassette = inject_cassette
Expand Down Expand Up @@ -145,6 +147,7 @@ def add_cassette_library_dir(path):
"record_mode": kwargs.get("record_mode", self.record_mode),
"before_record_request": self._build_before_record_request(kwargs),
"before_record_response": self._build_before_record_response(kwargs),
"alter_live_response": self.alter_live_response,
"custom_patches": self._custom_patches + kwargs.get("custom_patches", ()),
"inject": kwargs.get("inject_cassette", self.inject_cassette),
"path_transformer": path_transformer,
Expand Down
21 changes: 15 additions & 6 deletions vcr/stubs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Stubs for patching HTTP and HTTPS requests"""

import copy
import logging
from http.client import (
HTTPConnection,
Expand Down Expand Up @@ -249,16 +250,24 @@ def getresponse(self, _=False, **kwargs):
)

# get the response
response = self.real_connection.getresponse()
live_response = self.real_connection.getresponse()

# put the response into the cassette
response = {
"status": {"code": response.status, "message": response.reason},
"headers": serialize_headers(response),
"body": {"string": response.read()},
live_response = {
"status": {"code": live_response.status, "message": live_response.reason},
"headers": serialize_headers(live_response),
"body": {"string": live_response.read()},
}
# Deepcopy is here because mutation of `response` will corrupt the
# real response.
response = copy.deepcopy(live_response)
response = self.cassette._before_record_response(response)

self.cassette.append(self._vcr_request, response)
return VCRHTTPResponse(response)
if self.cassette._alter_live_response:
return VCRHTTPResponse(response)
else:
return VCRHTTPResponse(live_response)

def set_debuglevel(self, *args, **kwargs):
self.real_connection.set_debuglevel(*args, **kwargs)
Expand Down

0 comments on commit a9d01cb

Please sign in to comment.