rack-vcr 0.1.1 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +32 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +2 -0
- data/README.md +68 -5
- data/lib/rack/vcr.rb +24 -5
- data/lib/rack/vcr/transaction.rb +26 -10
- data/lib/rack/vcr/version.rb +1 -1
- data/rack-vcr.gemspec +5 -5
- metadata +14 -15
- data/.travis.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dd81d08f1fea87a51788e471948f8209f83a3ca27d7b1e9476a2442e36910038
|
4
|
+
data.tar.gz: 1ae3ef6efcce039cf0de32f252aa432514ffe038087aa0273470dc77f6838c78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 957402e260aa2976b59e84260172e296984451c6c5d09591ab03f94e1f0bb28ef63aa5a9fba0d29da484c02ae89e6cb85b522359fb62234781f187df7dc5a509
|
7
|
+
data.tar.gz: a89328c6aade966b0fa30af74ed5b04f995c984ede7bbf14282a5bc91968c25660834614e3975b6e5f1048bbc57f5284eb343092805e419d6fcd860c0088caa1
|
@@ -0,0 +1,32 @@
|
|
1
|
+
name: Run tests
|
2
|
+
on: [push, pull_request]
|
3
|
+
|
4
|
+
jobs:
|
5
|
+
test:
|
6
|
+
runs-on: ubuntu-latest
|
7
|
+
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby_version:
|
11
|
+
- 2.4
|
12
|
+
- 2.5
|
13
|
+
- 2.6
|
14
|
+
- 2.7
|
15
|
+
rack_version:
|
16
|
+
- ~> 1.6
|
17
|
+
- ~> 2.0.9
|
18
|
+
- ~> 2.1.4
|
19
|
+
- ~> 2.2.3
|
20
|
+
|
21
|
+
steps:
|
22
|
+
- uses: actions/checkout@v2
|
23
|
+
- uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
bundler-cache: true
|
26
|
+
ruby-version: ${{ matrix.ruby_version }}
|
27
|
+
- name: Run tests on ruby ${{ matrix.ruby_version }} with rack ${{ matrix.rack_version }}
|
28
|
+
run: |
|
29
|
+
bundle check || bundle update
|
30
|
+
bundle exec rake
|
31
|
+
env:
|
32
|
+
RACK_VERSION: ${{ matrix.rack_version }}
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## 0.1.6 (2020/10/31)
|
2
|
+
- Support Rack 2.1+ (#10)
|
3
|
+
|
4
|
+
## 0.1.5 (2017/07/11)
|
5
|
+
- Documentation fixes
|
6
|
+
- Support VCR 3+
|
7
|
+
|
8
|
+
## 0.1.4 (2015/06/18)
|
9
|
+
- Add :record option to override the default :new_episodes
|
10
|
+
|
11
|
+
## 0.1.3 (2015/06/18)
|
12
|
+
- Document replay/cassette option and add tests for them
|
13
|
+
|
14
|
+
## 0.1.2 (2015/06/17)
|
15
|
+
- Add experimental `replay: true` option to replay VCR cassette on the Rack layer
|
16
|
+
|
1
17
|
## 0.1.1 (2015/06/17)
|
2
18
|
- Add Rails example in doc
|
3
19
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -20,9 +20,9 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
### Rails
|
23
|
+
### Capturing in Rails
|
24
24
|
|
25
|
-
In `config/
|
25
|
+
In `config/initializers/rack_vcr.rb`:
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
if Rails.env.test?
|
@@ -37,6 +37,20 @@ VCR.configure do |config|
|
|
37
37
|
config.cassette_library_dir = 'doc/cassettes'
|
38
38
|
end
|
39
39
|
|
40
|
+
RSpec.configure do |config|
|
41
|
+
config.around(:each, type: :request) do |example|
|
42
|
+
host! "yourapp.hostname"
|
43
|
+
name = example.full_description.gsub /[^\w\-]/, '_'
|
44
|
+
VCR.use_cassette(name, record: :all) do
|
45
|
+
example.run
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
The above example might not work if you were using RSpec 2.x, in which case you might need to write as follows:
|
52
|
+
|
53
|
+
```ruby
|
40
54
|
RSpec.configure do |config|
|
41
55
|
config.around(:each, type: :request) do |ex|
|
42
56
|
host! "yourapp.hostname"
|
@@ -48,9 +62,12 @@ RSpec.configure do |config|
|
|
48
62
|
end
|
49
63
|
```
|
50
64
|
|
51
|
-
|
65
|
+
Read more about the changes around `example` on [RSpec blog post](http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/).
|
66
|
+
|
67
|
+
### Capturing in Sinatra/Rack
|
68
|
+
|
69
|
+
In `spec/spec_helper.rb`:
|
52
70
|
|
53
|
-
To capture HTTP interactions, enable VCR configuration in addition to this middleware, in the spec:
|
54
71
|
|
55
72
|
```ruby
|
56
73
|
require 'rack/test'
|
@@ -76,7 +93,53 @@ describe 'My Web App' do
|
|
76
93
|
# Now you get vcr_cassettes/hello.yml saved
|
77
94
|
end
|
78
95
|
end
|
79
|
-
|
96
|
+
```
|
97
|
+
|
98
|
+
### Replaying
|
99
|
+
|
100
|
+
Rack::VCR also supports *replaying* recorded VCR cassettes. It means you can record the HTTP interactions with the real app (on CI), then use the cassette to run a fake/mock API server using Rack::VCR!
|
101
|
+
|
102
|
+
To replay cassettes, enable Rack::VCR with `:replay` option in `config.ru` or its equivalent.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
VCR.configure do |config|
|
106
|
+
config.cassette_library_dir = "/path/to/cassettes"
|
107
|
+
end
|
108
|
+
|
109
|
+
Rack::Builder.new do
|
110
|
+
use Rack::VCR, replay: true,
|
111
|
+
cassette: "test", record: :new_episodes
|
112
|
+
run MyApp
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
With the above setting, Rack::VCR will try to locate the cassette named "test" to replay if the request matches with what's recorded, and fall through to the original application if it's not there. It also records the result to the cassette for further requests with the `:record` option set to `:new_episodes`.
|
117
|
+
|
118
|
+
You can set `:record` option to `:none` for example, to only serve what's already recorded in the cassette. The default value for `:record` option is `:new_episodes`.
|
119
|
+
|
120
|
+
To customize the cassette name in runtime, you can write a custom piece of Rack middleware around Rack::VCR to wrap the application in `VCR.use_cassette` with its own `:record` option.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
class CassetteLocator
|
124
|
+
def initialize(app)
|
125
|
+
@app = app
|
126
|
+
end
|
127
|
+
|
128
|
+
def call(env)
|
129
|
+
cassette = ... # determine cassette from env
|
130
|
+
VCR.use_cassette(cassette, record: :none) do
|
131
|
+
@app.call(env)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
Rack::Builder.new do
|
137
|
+
use CassetteLocator
|
138
|
+
use Rack::VCR, replay: true
|
139
|
+
run MyApp
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
80
143
|
|
81
144
|
## Notes
|
82
145
|
|
data/lib/rack/vcr.rb
CHANGED
@@ -4,16 +4,35 @@ require "vcr"
|
|
4
4
|
|
5
5
|
module Rack
|
6
6
|
class VCR
|
7
|
-
def initialize(app)
|
7
|
+
def initialize(app, options = {})
|
8
8
|
@app = app
|
9
|
+
@replay = options[:replay]
|
10
|
+
@cassette = options[:cassette]
|
11
|
+
@record = options[:record] || :new_episodes
|
9
12
|
end
|
10
13
|
|
11
14
|
def call(env)
|
15
|
+
if @cassette
|
16
|
+
::VCR.use_cassette(@cassette, record: @record) do
|
17
|
+
run_request(env)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
run_request(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def run_request(env)
|
12
25
|
req = Rack::Request.new(env)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
26
|
+
transaction = Transaction.new(req)
|
27
|
+
|
28
|
+
if @replay && transaction.can_replay?
|
29
|
+
transaction.replay
|
30
|
+
else
|
31
|
+
status, headers, body = @app.call(env)
|
32
|
+
res = Rack::Response.new(body, status, headers)
|
33
|
+
transaction.capture(res)
|
34
|
+
[status, headers, body]
|
35
|
+
end
|
17
36
|
end
|
18
37
|
end
|
19
38
|
end
|
data/lib/rack/vcr/transaction.rb
CHANGED
@@ -1,39 +1,55 @@
|
|
1
1
|
module Rack
|
2
2
|
class VCR
|
3
3
|
class Transaction
|
4
|
-
def
|
5
|
-
|
4
|
+
def initialize(req)
|
5
|
+
@req = req
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
@
|
8
|
+
def capture(res)
|
9
|
+
@res = res
|
10
|
+
::VCR.record_http_interaction(::VCR::HTTPInteraction.new(vcr_request, vcr_response))
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
-
::VCR.
|
13
|
+
def can_replay?
|
14
|
+
::VCR.http_interactions.has_interaction_matching?(vcr_request)
|
15
|
+
end
|
16
|
+
|
17
|
+
def replay
|
18
|
+
to_rack_response(::VCR.http_interactions.response_for(vcr_request))
|
14
19
|
end
|
15
20
|
|
16
21
|
private
|
17
22
|
|
18
23
|
def vcr_request
|
19
|
-
|
24
|
+
@vcr_request ||=
|
25
|
+
::VCR::Request.new(@req.request_method, @req.url, try_read(@req.body), request_headers)
|
20
26
|
end
|
21
27
|
|
22
28
|
def vcr_response
|
23
29
|
::VCR::Response.new(
|
24
30
|
::VCR::ResponseStatus.new(@res.status, nil),
|
25
31
|
@res.headers,
|
26
|
-
@res.body.join(''),
|
32
|
+
@res.body.to_enum.to_a.join(''),
|
27
33
|
)
|
28
34
|
end
|
29
35
|
|
36
|
+
def to_rack_response(res)
|
37
|
+
[
|
38
|
+
res.status.code,
|
39
|
+
Hash[res.headers.map {|k, v| [k, v.join("\n")] }],
|
40
|
+
[res.body],
|
41
|
+
]
|
42
|
+
end
|
43
|
+
|
30
44
|
def request_headers
|
31
45
|
headers_hash_from_env.merge(content_field_hash)
|
32
46
|
end
|
33
47
|
|
34
48
|
def headers_hash_from_env
|
35
|
-
fields = @req.env
|
36
|
-
|
49
|
+
fields = @req.env
|
50
|
+
.map { |k, v| [k.to_s, v] }
|
51
|
+
.select {|k, v| k.start_with?('HTTP_') }
|
52
|
+
.collect { |k, v| [normalize_header_field(k), v] }
|
37
53
|
Hash[fields]
|
38
54
|
end
|
39
55
|
|
data/lib/rack/vcr/version.rb
CHANGED
data/rack-vcr.gemspec
CHANGED
@@ -19,13 +19,13 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "vcr", "
|
22
|
+
spec.add_dependency "vcr", ">= 2.9"
|
23
23
|
|
24
|
-
spec.add_development_dependency "bundler", "
|
25
|
-
spec.add_development_dependency "rake", "
|
24
|
+
spec.add_development_dependency "bundler", ">= 1.10"
|
25
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
26
26
|
spec.add_development_dependency "rspec"
|
27
27
|
|
28
|
-
spec.add_development_dependency "sinatra", "
|
28
|
+
spec.add_development_dependency "sinatra", ">= 1.4"
|
29
29
|
spec.add_development_dependency "rack-test", "~> 0.6"
|
30
|
-
spec.add_development_dependency "webmock", "~>
|
30
|
+
spec.add_development_dependency "webmock", "~> 3"
|
31
31
|
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-vcr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuhiko Miyagawa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: vcr
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2.9'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.9'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.10'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
@@ -70,14 +70,14 @@ dependencies:
|
|
70
70
|
name: sinatra
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '1.4'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '1.4'
|
83
83
|
- !ruby/object:Gem::Dependency
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '3'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '3'
|
111
111
|
description: This Rack middleware records incoming Rack request and responses in a
|
112
112
|
VCR compatible format.
|
113
113
|
email:
|
@@ -118,9 +118,9 @@ executables:
|
|
118
118
|
extensions: []
|
119
119
|
extra_rdoc_files: []
|
120
120
|
files:
|
121
|
+
- ".github/workflows/test.yml"
|
121
122
|
- ".gitignore"
|
122
123
|
- ".rspec"
|
123
|
-
- ".travis.yml"
|
124
124
|
- CHANGELOG.md
|
125
125
|
- Gemfile
|
126
126
|
- LICENSE.txt
|
@@ -151,8 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
151
|
- !ruby/object:Gem::Version
|
152
152
|
version: '0'
|
153
153
|
requirements: []
|
154
|
-
|
155
|
-
rubygems_version: 2.4.6
|
154
|
+
rubygems_version: 3.0.3
|
156
155
|
signing_key:
|
157
156
|
specification_version: 4
|
158
157
|
summary: Record incoming Rack request as VCR cassettes
|
data/.travis.yml
DELETED