Repository: kevin1024/vcrpy
Pull request #702 (2023-05-18 19:43:23)

Fix for #174 to prevent filters from corrupting request
Replaces https://github.com/kevin1024/vcrpy/pull/687/

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/kevin1024/vcrpy/pull/702?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#702](https://app.codecov.io/gh/kevin1024/vcrpy/pull/702?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f56269d) into [master](https://app.codecov.io/gh/kevin1024/vcrpy/commit/1a3bc67c7c9dcede68c1b072fff2676d221b230b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (1a3bc67) will **decrease** coverage by `0.12%`.
> The diff coverage is `100.00%`.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please [install the Github App Integration](https://github.com/apps/codecov) for your organization. [Read more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).

```diff
@@            Coverage Diff             @@
##           master     #702      +/-   ##
==========================================
- Coverage   90.15%   90.03%   -0.12%     
==========================================
  Files          27       27              
  Lines        1726     1726              
  Branches      256      308      +52     
==========================================
- Hits         1556     1554       -2     
- Misses        135      137       +2     
  Partials       35       35              
```


| [Impacted Files](https://app.codecov.io/gh/kevin1024/vcrpy/pull/702?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [vcr/config.py](https://app.codecov.io/gh/kevin1024/vcrpy/pull/702?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-dmNyL2NvbmZpZy5weQ==) | `95.06% <100.00%> (ø)` | |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/kevin1024/vcrpy/pull/702/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

abramclark: > black formatting is not clean

Uhg I always use `black -S`. Should be fixed now.
hartwork: @abramclark please not that environment `py3*-aiohttp` is unfortunately a bit flaky, that's not your fault, I will restart it until green, just so you know.
abramclark: lol
hartwork: > lol

@abramclark I think crying would be more to the point :smile: In case you have time to track it down and make it rebust, that could make a great pull request, hint hint :smiley: 
hartwork: @kevin1024 @jairhenrique does this PR look like a good move to you? Any concerns?

Patch:
diff --git a/vcr/config.py b/vcr/config.py
index a991c95..5ee9f32 100644
--- a/vcr/config.py
+++ b/vcr/config.py
@@ -219,7 +219,7 @@ class VCR:
             filter_functions.extend(before_record_request)
 
         def before_record_request(request):
-            request = copy.copy(request)
+            request = copy.deepcopy(request)
             for function in filter_functions:
                 if request is None:
                     break


Test patch:
diff --git a/tests/integration/test_filter.py b/tests/integration/test_filter.py
index 5823b8b..237d0c9 100644
--- a/tests/integration/test_filter.py
+++ b/tests/integration/test_filter.py
@@ -142,3 +142,24 @@ def test_decompress_regular(tmpdir, httpbin):
         resp = urlopen(url).read()
         assert_cassette_has_one_response(cass)
     assert_is_json(resp)
+
+
+def test_before_record_request_corruption(tmpdir, httpbin):
+    """Modifying request in before_record_request should not affect outgoing request"""
+
+    def before_record(request):
+        request.headers.clear()
+        request.body = b""
+        return request
+
+    req = Request(
+        httpbin.url + "/post",
+        data=urlencode({"test": "exists"}).encode(),
+        headers={"X-Test": "exists"},
+    )
+    cass_file = str(tmpdir.join("modified_response.yaml"))
+    with vcr.use_cassette(cass_file, before_record_request=before_record):
+        resp = json.loads(urlopen(req).read())
+
+    assert resp["headers"]["X-Test"] == "exists"
+    assert resp["form"]["test"] == "exists"