epignosisx / vcr.net

Record your .NET Core test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
MIT License
10 stars 1 forks source link

DefaultRequestMatcher IgnoreHeaders not working #1

Closed tb-x closed 4 years ago

tb-x commented 4 years ago

In my test case, I'm trying to use the RecordMode.Once to record the cassettes in the first run and then replay the payloads during consecutive runs. The API that I'm recording is using API key authentication in headers. I don't want to store the API Key in a cassette in source control and I don't want to compare this header during playbacks.

I tried to use the IgnoreHeaders property of DefaultRequestMatcher but it is not used during the recording (which conceptually makes sense for your design). On the other hand, when replaying, the IgnoreHeaders seems to be used as a blacklist, but it should be a whitelist for comparison.

if (IgnoreHeaders.Contains(recordedHeader.Key))
    return false; // <---

should be:

if (IgnoreHeaders.Contains(recordedHeader.Key))
    continue; // <---
  1. Is my thinking correct?
  2. Is there a way to ignore a header when recording cassettes?
epignosisx commented 4 years ago

Thanks for reporting this issue! About your questions:

1- You are right. I'll make the change. 2- The recording can be modified by creating a custom ICassetteStorage:

public class CustomStorage : ICassetteStorage
{
    private FileSystemCassetteStorage _storage;

    public CustomStorage(DirectoryInfo dir)
    {
        _storage = new FileSystemCassetteStorage(dir);
    }

    public void Save(string name, IEnumerable<HttpInteraction> httpInteractions)
    {
        //modify httpInteractions at will, then delegate to the default storage impl for actual storage
        _storage.Save(name, httpInteractions);
    }

    ...
}

Then from your test replace this line with the custom storage implementation: https://github.com/epignosisx/vcr.net/blob/master/sample/SampleWebApp.IntegrationTest/HomeControllerTest.cs#L22

epignosisx commented 4 years ago

@tb-x I published version 0.1.3 to Nuget. Should be shortly listed.