Vcr doesn't know how to handle this request

Hi I am trying to check google auth with cucumber using vcr with tag.

Everything is going well until the token expires. I guess when it expires it will

enter image description here

But I have a file with this content

http_interactions:
- request:
  method: post
  uri: https://accounts.google.com/o/oauth2/token
  body:

      

If I allow vcr to record new requests, the contents of that cassette changes. I don't understand why, if the method and uri don't change POST to https://accounts.google.com/o/oauth2/token .

I changed the tag to record new episodes and now the test is passing ... I don't know.

I run the test again and now I have this happening when I POST to the url token:

Completed 500 Internal Server Error in 449ms

Psych::BadAlias (Unknown alias: 70317249293120):

      

+3


source to share


3 answers


Perhaps you have some parameters inside the post that are different for each request? If so, you can tell the VCR to ignore these parameters by adding match_requests_on: [:method, VCR.request_matchers.uri_without_params("your_param")]

a VCR to your configuration.



Analyze your query in depth and find out what parameters are changing. You can tell the VCR meets other criteria as well, take a look here https://www.relishapp.com/vcr/vcr/v/2-4-0/docs/request-matching

+1


source


Ok, here's the solution ... The problem arises, as I said in the comment, from updating the token. When using oauth, you have a token that may or may not have expired. If you run the test and the token is fresh, this request is not called. But if the token has expired it has to update it and thus vcr throws an error. To solve this problem, what I did was add the updated token token to the ignored vcr requests:

VCR.configure do |c|
  c.cassette_library_dir = 'fixtures/vcr_cassettes'
  c.hook_into :webmock # or :fakeweb
  c.ignore_request {|request| request.uri == 'https://accounts.google.com/o/oauth2/token' }
end

      



This is not the best solution as sometimes the token is updated in tests ... but this is the best solution I could find ...

+1


source


I was getting the same problem with the same url. The problem for me was that my code was trying to make the same call https://accounts.google.com/o/oauth2/token

more than once.

One of the possible solutions mentioned in the VCR error message tells you the solution:

The cassette contains an HTTP interaction that matches this request, but has already been played. If you want the same HTTP interaction to play multiple times, set the parameter:allow_playback_repeats

In my case, adding this option fixed the problem as it tells the VCR to revert to its 1.x functionality without rewriting duplicate requests, but simply replaying the result of a previously recorded duplicate request.

I am using Cucumber, so my solution was as follows features/support/vcr.rb

::

VCR.cucumber_tags do |t|
  t.tag  '@vcr', use_scenario_name: true
  t.tag  '@new_episodes', record: :new_episodes
  t.tag  '@allow_playback_repeats', use_scenario_name: true, allow_playback_repeats: true, record: :new_episodes
end

      

Pay attention to the tag @allow_playback_repeats

. I just tagged my script with this tag and after that everything worked correctly:

@allow_playback_repeats
Scenario: Uploading a video initiates an upload to YouTube

      

Note that this doesn't work if you specify both @vcr

and @allow_playback_repeats

.

If you are using RSpec, you need to tailor the solution accordingly, but should be as simple as :

it "does something", :vcr => { allow_playback_repeats: true } do
   ...
end

      

0


source







All Articles