vcr 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/.document +5 -0
  2. data/.gitignore +22 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +156 -0
  5. data/Rakefile +60 -0
  6. data/VERSION +1 -0
  7. data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette1.yml +43 -0
  8. data/features/fixtures/vcr_cassettes/1.8.6/cucumber_tags/replay_cassette2.yml +45 -0
  9. data/features/fixtures/vcr_cassettes/1.8.6/nested_replay_cassette.yml +23 -0
  10. data/features/fixtures/vcr_cassettes/1.8.6/not_the_real_response.yml +43 -0
  11. data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette1.yml +43 -0
  12. data/features/fixtures/vcr_cassettes/1.8.7/cucumber_tags/replay_cassette2.yml +45 -0
  13. data/features/fixtures/vcr_cassettes/1.8.7/nested_replay_cassette.yml +23 -0
  14. data/features/fixtures/vcr_cassettes/1.8.7/not_the_real_response.yml +43 -0
  15. data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette1.yml +43 -0
  16. data/features/fixtures/vcr_cassettes/1.9.1/cucumber_tags/replay_cassette2.yml +61 -0
  17. data/features/fixtures/vcr_cassettes/1.9.1/nested_replay_cassette.yml +31 -0
  18. data/features/fixtures/vcr_cassettes/1.9.1/not_the_real_response.yml +43 -0
  19. data/features/record_response.feature +55 -0
  20. data/features/replay_recorded_response.feature +53 -0
  21. data/features/step_definitions/vcr_steps.rb +88 -0
  22. data/features/support/env.rb +55 -0
  23. data/lib/vcr.rb +48 -0
  24. data/lib/vcr/cassette.rb +89 -0
  25. data/lib/vcr/config.rb +19 -0
  26. data/lib/vcr/cucumber_tags.rb +38 -0
  27. data/lib/vcr/fake_web_extensions.rb +18 -0
  28. data/lib/vcr/net_http_extensions.rb +39 -0
  29. data/lib/vcr/recorded_response.rb +4 -0
  30. data/spec/cassette_spec.rb +185 -0
  31. data/spec/config_spec.rb +27 -0
  32. data/spec/cucumber_tags_spec.rb +71 -0
  33. data/spec/fake_web_extensions_spec.rb +26 -0
  34. data/spec/fixtures/1.8.6/cassette_spec/example.yml +78 -0
  35. data/spec/fixtures/1.8.7/cassette_spec/example.yml +78 -0
  36. data/spec/fixtures/1.9.1/cassette_spec/example.yml +77 -0
  37. data/spec/net_http_extensions_spec.rb +40 -0
  38. data/spec/recorded_response_spec.rb +25 -0
  39. data/spec/spec.opts +2 -0
  40. data/spec/spec_helper.rb +22 -0
  41. data/spec/support/temp_cache_dir.rb +16 -0
  42. data/spec/vcr_spec.rb +95 -0
  43. metadata +154 -0
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe VCR::Config do
4
+ describe '#cache_dir=' do
5
+ temp_dir(File.expand_path(File.dirname(__FILE__) + '/fixtures/config_spec'))
6
+
7
+ it 'creates the directory if it does not exist' do
8
+ lambda { VCR::Config.cache_dir = @temp_dir }.should change { File.exist?(@temp_dir) }.from(false).to(true)
9
+ end
10
+
11
+ it 'does not raise an error if given nil' do
12
+ lambda { VCR::Config.cache_dir = nil }.should_not raise_error
13
+ end
14
+ end
15
+
16
+ describe '#default_cassette_record_mode' do
17
+ VCR::Cassette::VALID_RECORD_MODES.each do |mode|
18
+ it "allows #{mode}" do
19
+ lambda { VCR::Config.default_cassette_record_mode = mode }.should_not raise_error
20
+ end
21
+ end
22
+
23
+ it "does not allow :not_a_record_mode" do
24
+ lambda { VCR::Config.default_cassette_record_mode = :not_a_record_mode }.should raise_error(ArgumentError)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe VCR::CucumberTags do
4
+ before(:each) do
5
+ @args = { :before => [], :after => [] }
6
+ @blocks = { :before => [], :after => [] }
7
+ end
8
+
9
+ def Before(*args, &block)
10
+ @args[:before] << args
11
+ @blocks[:before] << block
12
+ end
13
+
14
+ def After(*args, &block)
15
+ @args[:after] << args
16
+ @blocks[:after] << block
17
+ end
18
+
19
+ describe '#tag' do
20
+ [:before, :after].each do |hook|
21
+ it "sets up a cucumber #{hook} hook for the given tag that creates a new cassette" do
22
+ VCR.cucumber_tags { |t| t.tag 'tag_test' }
23
+
24
+ @args[hook].should == [['@tag_test']]
25
+
26
+ if hook == :before
27
+ VCR.should_receive(:create_cassette!).with('cucumber_tags/tag_test', {})
28
+ else
29
+ VCR.should_receive(:destroy_cassette!)
30
+ end
31
+ @blocks[hook].should have(1).block
32
+ @blocks[hook].first.call
33
+ end
34
+
35
+ it "sets up separate hooks for each tag, passing the given options to each cassette" do
36
+ VCR.cucumber_tags { |t| t.tag 'tag_test1', 'tag_test2', :record => :none }
37
+ @args[hook].should == [['@tag_test1'], ['@tag_test2']]
38
+
39
+ if hook == :before
40
+ VCR.should_receive(:create_cassette!).with('cucumber_tags/tag_test1', { :record => :none }).once
41
+ VCR.should_receive(:create_cassette!).with('cucumber_tags/tag_test2', { :record => :none }).once
42
+ else
43
+ VCR.should_receive(:destroy_cassette!).twice
44
+ end
45
+ @blocks[hook].should have(2).blocks
46
+ @blocks[hook].each { |b| b.call }
47
+ end
48
+
49
+ it "works with tags that start with an @" do
50
+ VCR.cucumber_tags { |t| t.tag '@tag_test' }
51
+ @args[hook].should == [['@tag_test']]
52
+
53
+ if hook == :before
54
+ VCR.should_receive(:create_cassette!).with('cucumber_tags/tag_test', {})
55
+ else
56
+ VCR.should_receive(:destroy_cassette!)
57
+ end
58
+ @blocks[hook].should have(1).block
59
+ @blocks[hook].first.call
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '.tags' do
65
+ it 'returns the list of cucumber tags' do
66
+ VCR.cucumber_tags { |t| t.tag 'tag1' }
67
+ VCR.cucumber_tags { |t| t.tags 'tag7', 'tag12' }
68
+ VCR::CucumberTags.tags[-3, 3].should == %w(@tag1 @tag7 @tag12)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "FakeWebExtensions" do
4
+ describe "#remove_from_registry with (:get, 'http://example.com')" do
5
+ before(:each) do
6
+ FakeWeb.register_uri(:get, 'http://example.com', :body => "Example dot com!")
7
+ FakeWeb.register_uri(:post, 'http://example.com', :body => "Example dot com!")
8
+ FakeWeb.register_uri(:get, 'http://google.com', :body => "Google dot com!")
9
+ @remove_example_dot_com = lambda { FakeWeb.remove_from_registry(:get, 'http://example.com') }
10
+ end
11
+
12
+ it "removes the :get http://example.com registration" do
13
+ @remove_example_dot_com.should change { FakeWeb.registered_uri?(:get, 'http://example.com') }.from(true).to(false)
14
+ end
15
+
16
+ it "does not remove the :post http://example.com registration" do
17
+ FakeWeb.registered_uri?(:post, 'http://example.com').should be_true
18
+ @remove_example_dot_com.should_not change { FakeWeb.registered_uri?(:post, 'http://example.com') }
19
+ end
20
+
21
+ it "does not affect other registered uris" do
22
+ FakeWeb.registered_uri?(:get, 'http://google.com').should be_true
23
+ @remove_example_dot_com.should_not change { FakeWeb.registered_uri?(:get, 'http://google.com') }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,78 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ method: :get
4
+ uri: http://example.com:80/
5
+ response: !ruby/object:Net::HTTPOK
6
+ body: |
7
+ <HTML>
8
+ <HEAD>
9
+ <TITLE>Example Web Page</TITLE>
10
+ </HEAD>
11
+ <body>
12
+ <p>You have reached this web page by typing &quot;example.com&quot;,
13
+ &quot;example.net&quot;,
14
+ or &quot;example.org&quot; into your web browser.</p>
15
+ <p>These domain names are reserved for use in documentation and are not available
16
+ for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
17
+ 2606</a>, Section 3.</p>
18
+ </BODY>
19
+ </HTML>
20
+
21
+
22
+ body_exist: true
23
+ code: "200"
24
+ header:
25
+ last-modified:
26
+ - Tue, 15 Nov 2005 13:24:10 GMT
27
+ connection:
28
+ - Keep-Alive
29
+ date:
30
+ - Thu, 25 Feb 2010 07:52:38 GMT
31
+ content-type:
32
+ - text/html; charset=UTF-8
33
+ etag:
34
+ - "\"24ec5-1b6-4059a80bfd280\""
35
+ server:
36
+ - Apache/2.2.3 (CentOS)
37
+ content-length:
38
+ - "438"
39
+ age:
40
+ - "3009"
41
+ accept-ranges:
42
+ - bytes
43
+ http_version: "1.1"
44
+ message: OK
45
+ read: true
46
+ socket:
47
+ - !ruby/struct:VCR::RecordedResponse
48
+ method: :get
49
+ uri: http://example.com:80/foo
50
+ response: !ruby/object:Net::HTTPNotFound
51
+ body: |
52
+ <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
53
+ <html><head>
54
+ <title>404 Not Found</title>
55
+ </head><body>
56
+ <h1>Not Found</h1>
57
+ <p>The requested URL /foo was not found on this server.</p>
58
+ <hr>
59
+ <address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>
60
+ </body></html>
61
+
62
+ body_exist: true
63
+ code: "404"
64
+ header:
65
+ connection:
66
+ - close
67
+ content-type:
68
+ - text/html; charset=iso-8859-1
69
+ date:
70
+ - Thu, 25 Feb 2010 07:52:38 GMT
71
+ server:
72
+ - Apache/2.2.3 (CentOS)
73
+ content-length:
74
+ - "277"
75
+ http_version: "1.1"
76
+ message: Not Found
77
+ read: true
78
+ socket:
@@ -0,0 +1,78 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ method: :get
4
+ uri: http://example.com:80/
5
+ response: !ruby/object:Net::HTTPOK
6
+ body: |
7
+ <HTML>
8
+ <HEAD>
9
+ <TITLE>Example Web Page</TITLE>
10
+ </HEAD>
11
+ <body>
12
+ <p>You have reached this web page by typing &quot;example.com&quot;,
13
+ &quot;example.net&quot;,
14
+ or &quot;example.org&quot; into your web browser.</p>
15
+ <p>These domain names are reserved for use in documentation and are not available
16
+ for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
17
+ 2606</a>, Section 3.</p>
18
+ </BODY>
19
+ </HTML>
20
+
21
+
22
+ body_exist: true
23
+ code: "200"
24
+ header:
25
+ etag:
26
+ - "\"24ec5-1b6-4059a80bfd280\""
27
+ last-modified:
28
+ - Tue, 15 Nov 2005 13:24:10 GMT
29
+ connection:
30
+ - Keep-Alive
31
+ content-type:
32
+ - text/html; charset=UTF-8
33
+ date:
34
+ - Thu, 25 Feb 2010 07:53:51 GMT
35
+ server:
36
+ - Apache/2.2.3 (CentOS)
37
+ content-length:
38
+ - "438"
39
+ age:
40
+ - "9260"
41
+ accept-ranges:
42
+ - bytes
43
+ http_version: "1.1"
44
+ message: OK
45
+ read: true
46
+ socket:
47
+ - !ruby/struct:VCR::RecordedResponse
48
+ method: :get
49
+ uri: http://example.com:80/foo
50
+ response: !ruby/object:Net::HTTPNotFound
51
+ body: |
52
+ <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
53
+ <html><head>
54
+ <title>404 Not Found</title>
55
+ </head><body>
56
+ <h1>Not Found</h1>
57
+ <p>The requested URL /foo was not found on this server.</p>
58
+ <hr>
59
+ <address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>
60
+ </body></html>
61
+
62
+ body_exist: true
63
+ code: "404"
64
+ header:
65
+ content-type:
66
+ - text/html; charset=iso-8859-1
67
+ connection:
68
+ - close
69
+ server:
70
+ - Apache/2.2.3 (CentOS)
71
+ date:
72
+ - Thu, 25 Feb 2010 07:53:52 GMT
73
+ content-length:
74
+ - "277"
75
+ http_version: "1.1"
76
+ message: Not Found
77
+ read: true
78
+ socket:
@@ -0,0 +1,77 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ :method: :get
4
+ :uri: http://example.com:80/
5
+ :response: !ruby/object:Net::HTTPOK
6
+ body: |
7
+ <HTML>
8
+ <HEAD>
9
+ <TITLE>Example Web Page</TITLE>
10
+ </HEAD>
11
+ <body>
12
+ <p>You have reached this web page by typing &quot;example.com&quot;,
13
+ &quot;example.net&quot;,
14
+ or &quot;example.org&quot; into your web browser.</p>
15
+ <p>These domain names are reserved for use in documentation and are not available
16
+ for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
17
+ 2606</a>, Section 3.</p>
18
+ </BODY>
19
+ </HTML>
20
+
21
+ body_exist: true
22
+ code: "200"
23
+ header:
24
+ server:
25
+ - Apache/2.2.3 (Red Hat)
26
+ last-modified:
27
+ - Tue, 15 Nov 2005 13:24:10 GMT
28
+ etag:
29
+ - "\"b300b4-1b6-4059a80bfd280\""
30
+ accept-ranges:
31
+ - bytes
32
+ content-type:
33
+ - text/html; charset=UTF-8
34
+ connection:
35
+ - Keep-Alive
36
+ date:
37
+ - Mon, 25 Jan 2010 18:00:32 GMT
38
+ age:
39
+ - "2090"
40
+ content-length:
41
+ - "438"
42
+ http_version: "1.1"
43
+ message: OK
44
+ read: true
45
+ socket:
46
+ - !ruby/struct:VCR::RecordedResponse
47
+ :method: :get
48
+ :uri: http://example.com:80/foo
49
+ :response: !ruby/object:Net::HTTPNotFound
50
+ body: |
51
+ <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
52
+ <html><head>
53
+ <title>404 Not Found</title>
54
+ </head><body>
55
+ <h1>Not Found</h1>
56
+ <p>The requested URL /foo was not found on this server.</p>
57
+ <hr>
58
+ <address>Apache/2.2.3 (CentOS) Server at example.com Port 80</address>
59
+ </body></html>
60
+
61
+ body_exist: true
62
+ code: "404"
63
+ header:
64
+ date:
65
+ - Mon, 25 Jan 2010 18:00:32 GMT
66
+ server:
67
+ - Apache/2.2.3 (CentOS)
68
+ content-length:
69
+ - "277"
70
+ connection:
71
+ - close
72
+ content-type:
73
+ - text/html; charset=iso-8859-1
74
+ http_version: "1.1"
75
+ message: Not Found
76
+ read: true
77
+ socket:
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "NetHttpExtensions" do
4
+ before(:all) do
5
+ @orig_allow_net_connect = FakeWeb.allow_net_connect?
6
+ FakeWeb.allow_net_connect = true
7
+ end
8
+
9
+ after(:all) do
10
+ FakeWeb.allow_net_connect = @orig_allow_net_connect
11
+ end
12
+
13
+ before(:each) do
14
+ @current_cassette = mock
15
+ VCR.stub!(:current_cassette).and_return(@current_cassette)
16
+ FakeWeb.clean_registry
17
+ end
18
+
19
+ describe 'a request that is not registered with FakeWeb' do
20
+ it 'calls #store_recorded_response! on the current cassette' do
21
+ recorded_response = VCR::RecordedResponse.new(:get, 'http://example.com:80/', :example_response)
22
+ VCR::RecordedResponse.should_receive(:new).with(:get, 'http://example.com:80/', an_instance_of(Net::HTTPOK)).and_return(recorded_response)
23
+ @current_cassette.should_receive(:store_recorded_response!).with(recorded_response)
24
+ Net::HTTP.get(URI.parse('http://example.com'))
25
+ end
26
+
27
+ it 'does not have an error if there is no current cassette' do
28
+ VCR.stub!(:current_cassette).and_return(nil)
29
+ lambda { Net::HTTP.get(URI.parse('http://example.com')) }.should_not raise_error
30
+ end
31
+ end
32
+
33
+ describe 'a request that is registered with FakeWeb' do
34
+ it 'does not call #store_recorded_response! on the current cassette' do
35
+ FakeWeb.register_uri(:get, 'http://example.com', :body => 'example.com response')
36
+ @current_cassette.should_not_receive(:store_recorded_response!)
37
+ Net::HTTP.get(URI.parse('http://example.com'))
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe VCR::RecordedResponse do
4
+ describe '#==' do
5
+ before(:each) do
6
+ @r1 = VCR::RecordedResponse.new(:get, 'http://example.com', :the_example_dot_come_response)
7
+ end
8
+
9
+ it 'returns true for 2 responses with the same method, uri and response' do
10
+ @r1.should == VCR::RecordedResponse.new(@r1.method, @r1.uri, @r1.response)
11
+ end
12
+
13
+ it 'returns false for 2 responses with different methods' do
14
+ @r1.should_not == VCR::RecordedResponse.new(:post, @r1.uri, @r1.response)
15
+ end
16
+
17
+ it 'returns false for 2 responses with different uris' do
18
+ @r1.should_not == VCR::RecordedResponse.new(@r1.method, 'http://example.com/path', @r1.response)
19
+ end
20
+
21
+ it 'returns false for 2 responses with different responses' do
22
+ @r1.should_not == VCR::RecordedResponse.new(@r1.method, @r1.uri, :another_response)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'vcr'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ begin
9
+ require 'ruby-debug'
10
+ Debugger.start
11
+ Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
12
+ rescue LoadError
13
+ # ruby-debug wasn't available so neither can the debugging be
14
+ end
15
+
16
+ # Requires supporting files with custom matchers and macros, etc,
17
+ # in ./support/ and its subdirectories.
18
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
19
+
20
+ Spec::Runner.configure do |config|
21
+ config.extend TempCacheDir
22
+ end