rack-vcr 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a363c46853907046d67adc464963251eecd4b276
4
- data.tar.gz: c2a672a981910a93d2cbca50a8aee7b0f74e8e9e
3
+ metadata.gz: 3825c1470087671fef46cf5a1109efd7471b371b
4
+ data.tar.gz: 21302ea609c296cf2e727550183ddccc9c2032b2
5
5
  SHA512:
6
- metadata.gz: f4f10f5006867f4b86d472f242e4e40e7f8f475e8f745439b2fcbdfc8470d0d9356d7f034379ecca26faa7f292ee0f22abaea5224f37176a75de605c121fecc2
7
- data.tar.gz: 734e6a3adcf2e8f8f0c6da178a69bb8b0fa8e2147c31e5cc8df3da5a42bf5e2b7ea7ee7f2aa72b9530ad3f13505eb9a7ff99a7f034a500c3c075e420d957ce68
6
+ metadata.gz: 45d499d8e3558ae702bc7caeb004a2c59369666f61a5c1b73430fb8882fa782c91ab755683b126f198cb2bf3344673b6dc72af120b62d29278e1cc37ab8c6c59
7
+ data.tar.gz: df1645554a7dd7dbeb84fa96788901cd80483a071d22cda77b2518a9945f6341229769ef7287ac9f74f3e241e7d3f1a4cd2329a2d66b08f9b4e6dfe28caa1bd3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.1.2 (2015/06/17)
2
+ - Add experimental `replay: true` option to replay VCR cassette on the Rack layer
3
+
1
4
  ## 0.1.1 (2015/06/17)
2
5
  - Add Rails example in doc
3
6
 
data/lib/rack/vcr.rb CHANGED
@@ -4,16 +4,23 @@ 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]
9
10
  end
10
11
 
11
12
  def call(env)
12
13
  req = Rack::Request.new(env)
13
- status, headers, body = @app.call(env)
14
- res = Rack::Response.new(body, status, headers)
15
- Transaction.capture(req, res)
16
- [status, headers, body]
14
+ transaction = Transaction.new(req)
15
+
16
+ if @replay && transaction.can_replay?
17
+ transaction.replay
18
+ else
19
+ status, headers, body = @app.call(env)
20
+ res = Rack::Response.new(body, status, headers)
21
+ transaction.capture(res)
22
+ [status, headers, body]
23
+ end
17
24
  end
18
25
  end
19
26
  end
@@ -1,22 +1,28 @@
1
1
  module Rack
2
2
  class VCR
3
3
  class Transaction
4
- def self.capture(*args)
5
- new(*args).record
4
+ def initialize(req)
5
+ @req = req
6
6
  end
7
7
 
8
- def initialize(req, res)
9
- @req, @res = req, res
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 record
13
- ::VCR.record_http_interaction(::VCR::HTTPInteraction.new(vcr_request, vcr_response))
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
- ::VCR::Request.new(@req.request_method, @req.url, try_read(@req.body), request_headers)
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
@@ -27,6 +33,14 @@ module Rack
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
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class VCR
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-vcr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuhiko Miyagawa