netrecorder 0.1.5 → 0.2.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.
@@ -37,26 +37,26 @@ Use recorded cache with fakeweb:
37
37
  == Cucumber Example
38
38
 
39
39
  see http://cukes.info for more info on testing with Cucumber
40
+ # Find me in features/support/netrecorder.rb
40
41
 
41
- # features/support/env.rb
42
42
  NetRecorder.config do |config|
43
- config.cache_file = File.join(RAILS_ROOT, 'features', 'support', 'fakeweb')
44
-
45
- if ENV['RECORD_NET_CALLS']
43
+ config.cache_file = "#{File.dirname(__FILE__)}/../support/fakeweb"
44
+ if ENV['RECORD_WEB']
46
45
  config.record_net_calls = true
47
46
  else
48
- FakeWeb.allow_net_connect = false
49
47
  config.fakeweb = true
48
+ FakeWeb.allow_net_connect = false
50
49
  end
51
- end
52
-
53
- # features/support/hooks.rb
50
+ end
51
+
54
52
  at_exit do
55
- NetRecorder.cache! if NetRecorder.recording?
53
+ if NetRecorder.recording?
54
+ NetRecorder.cache!
55
+ end
56
56
  end
57
-
58
- record mode
57
+
58
+ record mode (command line)
59
59
  >> rake features RECORD_NET_CALLS=true
60
60
 
61
- cache mode
61
+ cache mode (command line)
62
62
  >> rake features
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('netrecorder', '0.1.5') do |p|
5
+ Echoe.new('netrecorder', '0.2.0') do |p|
6
6
  p.description = "Record network responses for easy stubbing of external calls"
7
7
  p.url = "http://github.com/chrisyoung/netrecorder"
8
8
  p.author = "Chris Young"
@@ -44,7 +44,7 @@ Given /^(?:a clear cache|I delete the cache)$/ do
44
44
  end
45
45
 
46
46
  Then /^the cache should contain the example body$/ do
47
- NetRecorder.fakes.first[1][:body].first[:body].should be_the_example_dot_com_response
47
+ NetRecorder.fakes.first[1]['global'][:response].first[:response].body.should be_the_example_dot_com_response
48
48
  end
49
49
 
50
50
  Given /^a cached example page$/ do
@@ -58,7 +58,7 @@ Then /^the cache should be empty$/ do
58
58
  end
59
59
 
60
60
  Then /^the example entry should have (.+) responses$/ do |count|
61
- NetRecorder.fakes.first[1][:body].length.should == count.to_i
61
+ NetRecorder.fakes.first[1]['global'][:response].length.should == count.to_i
62
62
  end
63
63
 
64
64
  Given /^I have turned on fakeweb$/ do
@@ -9,10 +9,16 @@ module NetRecorder
9
9
  # request is overridden and the request and response are stored as a hash that can be written to a cache file
10
10
  def request(req, body = nil, &block)
11
11
  response = alias_for_request(req, body)
12
+
13
+ return response unless NetRecorder.recording?
14
+
15
+ scope = NetRecorder.scope || 'global'
12
16
  path = "http://#{req.bauth if req.bauth}#{req['host']}#{req.path}"
13
- existing_fake = @@fakes.select {|fake| fake[0] == path && fake[1][:method] == req.method}.first
14
- existing_fake[1][:body] << {:body => response.body.to_s} and return response if existing_fake
15
- @@fakes << [path, {:method => req.method, :body => [{:body => response.body.to_s}]}]
17
+
18
+ existing_fake = @@fakes.detect{|fake| fake[0] == path && fake[1][scope] && fake[1][scope][:method] == req.method}
19
+ existing_fake[1][scope][:response] << {:response => response} and return response if existing_fake
20
+
21
+ @@fakes << [path, {scope => {:method => req.method, :response => [{:response => response}]}}]
16
22
  yield response if block
17
23
  response
18
24
  end
@@ -36,7 +42,7 @@ def fakes_cache #:nodoc:
36
42
  if File.exist?(NetRecorder.cache_file)
37
43
  File.open(NetRecorder.cache_file, "r") do |f|
38
44
  YAML.load(f.read)
39
- end
45
+ end
40
46
  end
41
47
 
42
48
  return fakes if fakes.class == Hash
@@ -49,12 +49,28 @@ module NetRecorder
49
49
  end
50
50
  Net::HTTP.clear_netrecorder_cache!
51
51
  end
52
+
53
+ def self.register_scope(name)
54
+ fakeweb(name)
55
+ end
56
+
57
+ def self.scope=(name)
58
+ @@scope = name
59
+ end
60
+
61
+ def self.scope
62
+ return @@scope if defined?(@@scope)
63
+ 'global'
64
+ end
52
65
 
53
66
  private
67
+
54
68
  # load the cache and register all of the urls with fakeweb
55
- def self.fakeweb
69
+ def self.fakeweb(scope='global')
56
70
  fakes.each do |fake|
57
- FakeWeb.register_uri(fake[1][:method].downcase.to_sym, fake[0], fake[1][:body])
71
+ if fake[1][scope]
72
+ FakeWeb.register_uri(fake[1][scope][:method].downcase.to_sym, fake[0], fake[1][scope][:response])
73
+ end
58
74
  end
59
75
  end
60
76
 
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{netrecorder}
5
- s.version = "0.1.5"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Chris Young"]
9
- s.date = %q{2010-01-23}
9
+ s.date = %q{2010-01-24}
10
10
  s.description = %q{Record network responses for easy stubbing of external calls}
11
11
  s.email = %q{beesucker@gmail.com}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/config.rb", "lib/http.rb", "lib/http_header.rb", "lib/netrecorder.rb"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netrecorder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Young
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-23 00:00:00 -07:00
12
+ date: 2010-01-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency