Issue using VCR to use Webmock to Mock URLs in Ruby App

341 views
Skip to first unread message

steve87....@gmail.com

unread,
Feb 27, 2014, 11:07:05 AM2/27/14
to vcr-...@googlegroups.com
I want to be able to record specific requests and have ALL the requests/responses be logged to the SAME file if the URL host is dev1-pb1.  See my code below.  This code definitely ignores all requests that do NOT have dev1-pb1 as the URI.host.  When I make the first RESTful call  http://dev1-pb1:8080/pb-client-wrapper/model/123446, it creates the pb_mock_data file with this request and response in it which I expect.   When I make a subsequent request where the model id changes such as http://dev1-pb1:8080/pb-client-wrapper/model/634005, it throws the error below.  I can get it to work creating unique mock files.  Please advise!  Thanks!  

NOTE:  Ideally, I would like to have a regex in the mock yaml file where ALL requests matching a pattern the URL.   I tried putting a path_regex entry in the mock yaml file, but that didn't resolve the issue.    Can this be done?

In an initializer that loads when my ruby app starts, I have the following code:

if FullSite::Application.config.mock_pb_requests == true
  VCR.configure do |c|
    c.ignore_request do |request|
      URI(request.uri).host != 'dev1-pb1'
    end
    c.allow_http_connections_when_no_cassette = true
    c.cassette_library_dir = 'test/mocked_data/pb-client-wrapper'
    c.hook_into :webmock
    c.around_http_request do |request|
      VCR.use_cassette("pb_mock_data", &request)
    end
  end
end

Error on second request where the model id changes:

An HTTP request has been made that VCR does not know how to handle:

There is currently no cassette in use. There are a few ways
you can configure VCR to handle this request:

  * If you're surprised VCR is raising this error
    and want insight about how VCR attempted to handle the request,
    you can use the debug_logger configuration option to log more details [1].
  * If you want VCR to record this request and play it back during future test
    runs, you should wrap your test (or this portion of your test) in a
    `VCR.use_cassette` block [2].
  * If you only want VCR to handle requests made while a cassette is in use,
    configure `allow_http_connections_when_no_cassette = true`. VCR will
    ignore this request since it is made when there is no cassette [3].
  * If you want VCR to ignore this request (and others like it), you can
    set an `ignore_request` callback [4].

================================================================================

Myron Marston

unread,
Feb 27, 2014, 11:16:28 AM2/27/14
to vcr-...@googlegroups.com, steve87....@gmail.com
On Thursday, February 27, 2014 8:07:05 AM UTC-8, steve87....@gmail.com wrote:
I want to be able to record specific requests and have ALL the requests/responses be logged to the SAME file if the URL host is dev1-pb1.  See my code below.  This code definitely ignores all requests that do NOT have dev1-pb1 as the URI.host.  When I make the first RESTful call  http://dev1-pb1:8080/pb-client-wrapper/model/123446, it creates the pb_mock_data file with this request and response in it which I expect.   When I make a subsequent request where the model id changes such as http://dev1-pb1:8080/pb-client-wrapper/model/634005, it throws the error below.  I can get it to work creating unique mock files.  Please advise!  Thanks!  

The default record mode is :once, which allows a cassette to be recorded once, and then will only allow it to be played back in the future:


It sounds like you want to use the :new_episodes record mode:

 
NOTE:  Ideally, I would like to have a regex in the mock yaml file where ALL requests matching a pattern the URL.   I tried putting a path_regex entry in the mock yaml file, but that didn't resolve the issue.    Can this be done?

You can control which requests match using custom request matchers:

steve87....@gmail.com

unread,
Feb 27, 2014, 12:55:46 PM2/27/14
to vcr-...@googlegroups.com, steve87....@gmail.com
Thanks Myron for your response!   Got it working like I wanted per your recommendations!  -Steve
Reply all
Reply to author
Forward
0 new messages