How to ignore the request body match for an specific host in rspec VCR?

I have ElasticSearch and Kibana integration with my rails application, and i use they for log and measure requests to external API's. I don't want that VCR match the body for ElasticSearch requests records, because there are a "current time" (created_at) field in every log, that breaks VCR older records.

This is my current configuration

VCR.configure do |c|
  c.ignore_localhost = true
  c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'

  vcr_mode = ENV['VCR_MODE'] =~ /rec/i ? :all : :once

  c.hook_into :webmock
  c.default_cassette_options[:record] = vcr_mode
  c.configure_rspec_metadata!
end

This is the log method in my custom REST client.

  def log(method, params, response, comments:)
    Rails.logger.info(response.inspect)

    Elastic::LogServices
      .log_request(comments,
                   method: method,
                   source: ENV['HOSTNAME'],
                   url: response.env.url.to_s,
                   header: response.env.request_headers,
                   body: params,
                   type: :out,
                   response_header: response.headers,
                   response_body: response.body,
                   response_status: response.status)
  end

In the log services...

    def self.log_request(comments = '',
                         created_at: nil,
                         method:,
                         source:,
                         url:,
                         header:,
                         body:,
                         type:,
                         response_header:,
                         response_body: '?',
                         response_status:,
                         format: 'REST')
      log =
        Elastic::Request::Log
        .new(created_at: created_at,
             method: method,
             source: source,
             url: url,
             header: header,
             body: JSON.pretty_generate(body),
             type: type,
             response:
              {
                header: response_header,
                body: JSON.pretty_generate(response_body),
                status: response_status
              },
             format: format,
             comments: comments)

      Elastic::Request::LogJob.perform_async(log)
    end

This field is in the log entity class

        @created_at = created_at || Time.now.utc.iso8601

Also, i dont want to Mock the "Time.now.utc.iso8601" method, because there are a lot of tests that i have to change. The application do a lot of requests to others APIs.

Anyway to configure VCR to ignore the body match for all "elasticsearch:9200" requests?

Upvotes: 2

Views: 1725

Answers (1)

Saurabh Jain
Saurabh Jain

Reputation: 31

You can try:

VCR.configure do |c|
  c.ignore_localhost = true
  c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
  c.register_request_matcher :body do |request1, request2|
    if request1.uri.include?('<your_host>')
      true
    else
      CGI.escape(request1.body) == CGI.escape(request2.body)
    end
  end

  c.hook_into :webmock
  c.configure_rspec_metadata!
end

Upvotes: 1

Related Questions