ExVCR.Config (exvcr v0.17.0)

View Source

Assign configuration parameters.

Summary

Functions

Initializes library dir to store cassette json files.

This function can be used to filter headers from saved requests.

This function can be used to filter options.

Replace the specified pattern with placeholder. It can be used to remove sensitive data from the cassette file.

Set the flag whether to filter-out url params when recording to cassettes. (ex. if flag is true, "param=val" is removed from "http://example.com?param=val").

Skip recording cassettes for localhost requests when set

Skip recording cassettes for urls requests when set

Sets a list of headers to remove from the response

Throw error if there is no matching cassette for an HTTP request

Functions

cassette_library_dir(vcr_dir, custom_dir \\ nil)

Initializes library dir to store cassette json files.

  • vcr_dir: directory for storing recorded json file.
  • custom_dir: directory for placing custom json file.

filter_request_headers(header)

This function can be used to filter headers from saved requests.

Examples

test "replace sensitive data in request header" do
  ExVCR.Config.filter_request_headers("X-My-Secret-Token")

  use_cassette "sensitive_data_in_request_header" do
    body = HTTPoison.get!("http://localhost:34000/server?", ["X-My-Secret-Token": "my-secret-token"]).body
    assert body == "test_response"
  end

  # The recorded cassette should contain replaced data.
  cassette = File.read!("sensitive_data_in_request_header.json")
  assert cassette =~ ""X-My-Secret-Token": "***""
  refute cassette =~  ""X-My-Secret-Token": "my-secret-token""

  # Now reset the filter
  ExVCR.Config.filter_request_headers(nil)
end

filter_request_options(header)

This function can be used to filter options.

Examples

test "replace sensitive data in request options" do
  ExVCR.Config.filter_request_options("basic_auth")
  use_cassette "sensitive_data_in_request_options" do
    body = HTTPoison.get!(@url, [], [hackney: [basic_auth: {"username", "password"}]]).body
    assert body == "test_response"
  end

  # The recorded cassette should contain replaced data.
  cassette = File.read!("sensitive_data_in_request_options.json")
  assert cassette =~ ""basic_auth": "***""
  refute cassette =~  ""basic_auth": {"username", "password"}"

  # Now reset the filter
  ExVCR.Config.filter_request_options(nil)
end

filter_sensitive_data(atom)

filter_sensitive_data(pattern, placeholder)

Replace the specified pattern with placeholder. It can be used to remove sensitive data from the cassette file.

Examples

test "replace sensitive data" do
  ExVCR.Config.filter_sensitive_data("<PASSWORD>.+</PASSWORD>", "PLACEHOLDER")

  use_cassette "sensitive_data" do
    assert HTTPotion.get("http://something.example.com", []).body =~ ~r/PLACEHOLDER/
  end

  # Now clear the previous filter
  ExVCR.Config.filter_sensitive_data(nil)
end

filter_url_params(flag)

Set the flag whether to filter-out url params when recording to cassettes. (ex. if flag is true, "param=val" is removed from "http://example.com?param=val").

ignore_localhost(value)

Skip recording cassettes for localhost requests when set

ignore_urls(value)

Skip recording cassettes for urls requests when set

response_headers_blacklist(headers_blacklist)

Sets a list of headers to remove from the response

strict_mode(value)

Throw error if there is no matching cassette for an HTTP request