Search code examples
ruby-on-railsrubyrspecvcr

Working with mutable values in vcr


I have a vcr casette like this

---
http_interactions:
- request:
    method: post
    uri: https://control.msg91.com/api/sendotp.php?message=Your%20otp%20is%200805&mobile=919446733017&otp=0805&sender=TESTER
    body:
      encoding: UTF-8
      string: ''
    headers:
      Accept-Encoding:
      - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
      Accept:
      - "*/*"
      User-Agent:
      - Ruby
      Host:
      - control.msg91.com
      Cache-Control:
      - no-cache
  response:
    status:
      code: 200
      message: OK
    headers:
      Server:
      - nginx
      Date:
      - Sat, 08 Jul 2017 13:54:17 GMT
      Content-Type:
      - text/html
      Transfer-Encoding:
      - chunked
      Connection:
      - keep-alive
      Set-Cookie:
      - PHPSESSID=od25ntah2t8nf47i947dmevbv0; expires=Sun, 09-Jul-2017 13:54:17 GMT;
        Max-Age=86400; path=/; HttpOnly
      Expires:
      - Thu, 19 Nov 1981 08:52:00 GMT
      Cache-Control:
      - no-store, no-cache, must-revalidate, post-check=0, pre-check=0
      Pragma:
      - no-cache
      X-Frame-Options:
      - SAMEORIGIN
      X-Xss-Protection:
      - 1; mode=block
    body:
      encoding: UTF-8
      string: '{"message":"376768737871373032333335","type":"success"}'
    http_version: 
  recorded_at: Sat, 08 Jul 2017 13:54:17 GMT
recorded_with: VCR 3.0.3

In the uri there is otp and message fields which are random values on each requests. As these two are mutable(always changing) how can I ignore these 2 fields in vcr so that vcr works normal?


Solution

  • Looks like vcr has a built in solution available for this.

    Adding this to the VCR config block works

      config.default_cassette_options = {
          :match_requests_on => [:method,
                                 VCR.request_matchers.uri_without_params(:otp, :message)]
      }
    

    Here otp and message are the two things that I need to ignore