Skip to content
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

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

Open
akaihola opened this issue Mar 16, 2023 · 1 comment

Comments

@akaihola
Copy link

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):

  • OS: Linux (NixOS 22.11)
  • Python version: 3.11.2
  • pytest-recording version: 0.12.2
  • pytest version: 7.2.2
@akaihola
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants