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 +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/rack/vcr.rb +12 -5
- data/lib/rack/vcr/transaction.rb +21 -7
- data/lib/rack/vcr/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3825c1470087671fef46cf5a1109efd7471b371b
|
4
|
+
data.tar.gz: 21302ea609c296cf2e727550183ddccc9c2032b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45d499d8e3558ae702bc7caeb004a2c59369666f61a5c1b73430fb8882fa782c91ab755683b126f198cb2bf3344673b6dc72af120b62d29278e1cc37ab8c6c59
|
7
|
+
data.tar.gz: df1645554a7dd7dbeb84fa96788901cd80483a071d22cda77b2518a9945f6341229769ef7287ac9f74f3e241e7d3f1a4cd2329a2d66b08f9b4e6dfe28caa1bd3
|
data/CHANGELOG.md
CHANGED
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
data/lib/rack/vcr/transaction.rb
CHANGED
@@ -1,22 +1,28 @@
|
|
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
|
@@ -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
|
data/lib/rack/vcr/version.rb
CHANGED