vcrpy icon indicating copy to clipboard operation
vcrpy copied to clipboard

filterheaders doesn't work for cookie / set-cookie

Open valentijnscholten opened this issue 3 years ago • 1 comments

Tried to filter out cookies to prevent them from being recorded. Filtering on other headers works fine, but cookie and set-cookie keep appearing in the recordings.

my_vcr = vcr.VCR(
    record_mode='all', path_transformer=VCR.ensure_suffix('.yaml'), 
    filter_headers=['Set-Cookie', 'Cookie'],
    cassette_library_dir='dojo/unittests/vcr/jira/',
    # filters headers doesn't seem to work for cookies, so use callbacks to filter cookies from being recorded
    before_record_request=before_record_request,
    before_record_response=before_record_response,
)

Also tried lowercase, different order, etc. In the end I implemented the customer request/response filters to remove the cookie headers. Am I misiing the correct way to filter cookies?

valentijnscholten avatar Nov 14 '20 21:11 valentijnscholten

filter_headers applies to request headers, so only Cookie can be taken care of with it. Set-Cookie on the other hand is a response header, and thus needs to be done with before_record_response. This appears to work for me with pytest-vcr and pytest-recording:

def remove_set_cookie(response):
    response["headers"].pop("Set-Cookie", None)
    return response

@pytest.fixture(scope="module")
def vcr_config():
    return {
        "before_record_response": remove_set_cookie,
        "filter_headers": ["Cookie"],
    }

scop avatar Apr 15 '21 20:04 scop