From 675787a579dfea2923a38718ed01d7662972338e Mon Sep 17 00:00:00 2001 From: Jackson Pires Date: Sun, 19 Nov 2017 14:49:50 -0300 Subject: [PATCH] VCR --- test_app/Gemfile | 1 + test_app/Gemfile.lock | 2 + .../vcr_cassettes/jsonplaceholder/posts.yml | 67 +++++++++++++++++++ test_app/spec/httparty/httparty_spec.rb | 12 ++-- test_app/spec/spec_helper.rb | 5 ++ 5 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 test_app/spec/fixtures/vcr_cassettes/jsonplaceholder/posts.yml diff --git a/test_app/Gemfile b/test_app/Gemfile index bf05971..6bf9fbb 100644 --- a/test_app/Gemfile +++ b/test_app/Gemfile @@ -42,6 +42,7 @@ group :development, :test do gem 'faker' gem 'httparty' gem 'webmock' + gem 'vcr' end group :development do diff --git a/test_app/Gemfile.lock b/test_app/Gemfile.lock index c07c4ae..d6f432d 100644 --- a/test_app/Gemfile.lock +++ b/test_app/Gemfile.lock @@ -191,6 +191,7 @@ GEM thread_safe (~> 0.1) uglifier (3.2.0) execjs (>= 0.3.0, < 3) + vcr (3.0.3) web-console (3.5.1) actionview (>= 5.0) activemodel (>= 5.0) @@ -229,6 +230,7 @@ DEPENDENCIES turbolinks (~> 5) tzinfo-data uglifier (>= 1.3.0) + vcr web-console (>= 3.3.0) webmock diff --git a/test_app/spec/fixtures/vcr_cassettes/jsonplaceholder/posts.yml b/test_app/spec/fixtures/vcr_cassettes/jsonplaceholder/posts.yml new file mode 100644 index 0000000..cdb8022 --- /dev/null +++ b/test_app/spec/fixtures/vcr_cassettes/jsonplaceholder/posts.yml @@ -0,0 +1,67 @@ +--- +http_interactions: +- request: + method: get + uri: https://jsonplaceholder.typicode.com/posts/2 + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Sat, 18 Nov 2017 23:08:27 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Set-Cookie: + - __cfduid=d5eb1c242fb719f5fbdd8ec67f05ca7b61511046507; expires=Sun, 18-Nov-18 + 23:08:27 GMT; path=/; domain=.typicode.com; HttpOnly + X-Powered-By: + - Express + Vary: + - Accept-Encoding + Access-Control-Allow-Credentials: + - 'true' + Cache-Control: + - public, max-age=14400 + Pragma: + - no-cache + Expires: + - Sun, 19 Nov 2017 03:08:27 GMT + X-Content-Type-Options: + - nosniff + Etag: + - W/"116-jnDuMpjju89+9j7e0BqkdFsVRjs" + Via: + - 1.1 vegur + Cf-Cache-Status: + - HIT + Server: + - cloudflare-nginx + Cf-Ray: + - 3bfe97811d494cae-GRU + body: + encoding: ASCII-8BIT + string: |- + { + "userId": 1, + "id": 2, + "title": "qui est esse", + "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" + } + http_version: + recorded_at: Sat, 18 Nov 2017 23:08:27 GMT +recorded_with: VCR 3.0.3 diff --git a/test_app/spec/httparty/httparty_spec.rb b/test_app/spec/httparty/httparty_spec.rb index 56a201e..2093f72 100644 --- a/test_app/spec/httparty/httparty_spec.rb +++ b/test_app/spec/httparty/httparty_spec.rb @@ -1,10 +1,12 @@ describe 'HTTParty' do it 'content-type' do - stub_request(:get, "https://jsonplaceholder.typicode.com/posts/2"). - to_return(status: 200, body: "", headers: { 'content-type': 'application/json'}) + #stub_request(:get, "https://jsonplaceholder.typicode.com/posts/2"). + # to_return(status: 200, body: "", headers: { 'content-type': 'application/json'}) - response = HTTParty.get('https://jsonplaceholder.typicode.com/posts/2') - content_type = response.headers['content-type'] - expect(content_type).to match(/application\/json/) + VCR.use_cassette("jsonplaceholder/posts") do + response = HTTParty.get('https://jsonplaceholder.typicode.com/posts/2') + content_type = response.headers['content-type'] + expect(content_type).to match(/application\/json/) + end end end diff --git a/test_app/spec/spec_helper.rb b/test_app/spec/spec_helper.rb index 2187376..4486034 100644 --- a/test_app/spec/spec_helper.rb +++ b/test_app/spec/spec_helper.rb @@ -1,6 +1,11 @@ # Webmock gem require 'webmock/rspec' +VCR.configure do |config| + config.cassette_library_dir = "spec/fixtures/vcr_cassettes" + config.hook_into :webmock +end + # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause