parroty / exvcr

HTTP request/response recording library for elixir, inspired by VCR.
MIT License
720 stars 131 forks source link

`match_request_on :headers`, but specifying what headers it should match #134

Open IceDragon200 opened 6 years ago

IceDragon200 commented 6 years ago

Is it possible to use match_request_on :headers, but then specify which headers it should match?

If not then I suggest something like:

use_cassette "my_cassette", match_requests_on: [headers: [only: ["content-type", "soapaction"]]] do
  # do stuff
end
@type t :: [
  {:match_requests_on, [
    :headers |
    {:headers [
      only: [String.t],
      except: [String.t]
    ]}
  ]}
]

The default :headers option would still be kept if no filtering is required.

The reasoning behind this request is to ignore some dynamic headers that would be difficult to remove/disable just for testing (e.g. a user-agent which uses the current application version), but always invalidates recorded cassettes because of their nature.

bryanhuntesl commented 5 years ago

Is it possible to use match_request_on :headers, but then specify which headers it should match?

If not then I suggest something like:

use_cassette "my_cassette", match_requests_on: [headers: [only: ["content-type", "soapaction"]]] do
  # do stuff
end
@type t :: [
  {:match_requests_on, [
    :headers |
    {:headers [
      only: [String.t],
      except: [String.t]
    ]}
  ]}
]

The default :headers option would still be kept if no filtering is required.

The reasoning behind this request is to ignore some dynamic headers that would be difficult to remove/disable just for testing (e.g. a user-agent which uses the current application version), but always invalidates recorded cassettes because of their nature.

Good for testing ETag / "If-None-Match" interactions

bryanhuntesl commented 5 years ago

I've hit this as well - I would have like to have verified this worked - it's pretty essential to the project (github event poller).

https://github.com/esl/ex_github_poller/blob/0774b23079baf66acecf4bab6d96595075d8a6ef/test/ex_github_poller_test.exs#L27-L38

  test "list events etag " do
    ExVCR.Config.filter_request_headers("Authorization")
    use_cassette "list_events_etag" , match_requests_on: [ :headers, :query , :request_body ] do

      x = ExGithubPoller.events("bryanhuntesl", "test_repo", %ExGithubPoller.Param{} )
      assert length(x.events) == 152

      y = ExGithubPoller.events("bryanhuntesl", "test_repo", %ExGithubPoller.Param{etag: x.etag} )
      assert length(y.events) == 0
      assert y.limit_remaining == x.limit_remaining

    end
  end