kiwicom / pytest-recording

A pytest plugin that allows recording network interactions via VCR.py
MIT License
441 stars 35 forks source link

`@pytest.mark.vcr(before_record_response=...)` behaves differently when recording and playing back #100

Open akaihola opened 1 year ago

akaihola commented 1 year ago

Describe the bug If I use a before_record_response= callback which modifies the response, the original response is used when the response is being recorded, and the modified response when it's being played back.

To Reproduce Steps to reproduce the behavior:

  1. On this test file test_modified_response.py:

    import pytest
    import requests
    
    def modify_response(response):
        response["body"]["string"] = b"Modified response"
        return response
    
    @pytest.mark.vcr(before_record_response=[modify_response])
    def test_modified_response():
        response = requests.get(
            "https://github.com/kiwicom/pytest-recording",
            headers={"Accept-Encoding": "identity"},  # prevent compression
        )
        assert response.content == b"Modified response"
  2. Run this command:
    pytest --record-mode=once test_modified_response.py
  3. Observe that the test fails, while the recorded cassette .yaml file has the modified content:
      response:
        body:
          string: Modified response
  4. Run this command:
    pytest test_modified_response.py
  5. Observe that the test passes

Expected behavior For the example above, both test runs should pass.

More generally, the modified response should be returned to the calling HTTP client also when it's being recorded.

Environment (please complete the following information):

akaihola commented 1 year ago

This is actually probably better fixed on the VCRpy side – see kevin1024/vcrpy#544.