netrecorder 0.2.2 → 0.2.3

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.
data/Manifest CHANGED
@@ -2,7 +2,9 @@ Manifest
2
2
  README.rdoc
3
3
  Rakefile
4
4
  features/manage_cache.feature
5
+ features/manage_scoping.feature
5
6
  features/step_definitions/manage_cache_steps.rb
7
+ features/step_definitions/manage_scoping_steps.rb
6
8
  features/support/env.rb
7
9
  lib/config.rb
8
10
  lib/fake.rb
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('netrecorder', '0.2.2') do |p|
5
+ Echoe.new('netrecorder', '0.2.3') 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"
@@ -13,7 +13,7 @@ Scenario: Cache a page
13
13
  Scenario: Cache a page with a Net::HTTP request with a block
14
14
  Given caching is turned on
15
15
  And a clear cache
16
- When I visit "http://www.example.com" using a Net::HTTP request with a block
16
+ When I visit "http://www.example.com" using a Net::HTTP request with a block
17
17
  And I save the cache
18
18
  Then the cache should contain the example body
19
19
  And the Net::HTTP request should return the response
@@ -0,0 +1,20 @@
1
+ Feature: Manage scoping
2
+ In order to allow users to register scopes that are not dependent on order (limit of fakeweb)
3
+ As a user
4
+ I want to be able to manage scopes
5
+
6
+ Scenario: Store responses in reverse order
7
+ # Seems to be an error with fakeweb that keeps it from re-registering a url after clearing the registry - so I'm using google.com here instead of example.com
8
+ Given caching is turned on
9
+ And a clear cache
10
+ And I scope the request to "first scope"
11
+ And I visit "http://www.google.com"
12
+ And I scope the request to "second scope"
13
+ And I wait 3 seconds
14
+ And I visit "http://www.google.com"
15
+ And I save the cache
16
+ And I have registered "second scope"
17
+ And I visit "http://www.google.com"
18
+ Given I have registered "first scope"
19
+ And I visit "http://www.google.com"
20
+ Then the first response should be later than the second
@@ -24,10 +24,12 @@ And /^I save the cache$/ do
24
24
  end
25
25
 
26
26
  And /^I visit "([^\"]*)"$/ do |url|
27
- Net::HTTP.get URI.parse(url)
27
+ uri = URI.parse(url)
28
+ @last_response = @response
29
+ @response = Net::HTTP.start(uri.host, uri.port) {|http| http.get('/')}
28
30
  end
29
31
 
30
- When /^I visit "([^\"]*)" using a Net::HTTP request with a block$/ do |url|
32
+ When /^I visit "([^\"]*)" using a Net::HTTP request with a block$/ do |url|
31
33
  uri = URI.parse(url)
32
34
  @last_response = Net::HTTP.new(uri.host, uri.port).start do |http|
33
35
  http.get(uri.path.empty? ? '/' : uri.path)
@@ -71,4 +73,5 @@ end
71
73
  Then /^I should not hit the web if i visit the example page$/ do
72
74
  FakeWeb.allow_net_connect = false
73
75
  Proc.new{Net::HTTP.get URI.parse('http://www.example.com/')}.should_not raise_error
76
+ FakeWeb.allow_net_connect = true
74
77
  end
@@ -0,0 +1,18 @@
1
+ Given /^I have registered "([^\"]*)"$/ do |scope|
2
+ NetRecorder.register_scope(scope)
3
+ FakeWeb.allow_net_connect = false
4
+ end
5
+
6
+ Then /^the first response should be later than the second$/ do
7
+ first_date = DateTime.parse(@last_response['date'])
8
+ second_date = DateTime.parse(@response['date'])
9
+ raise 'the first response is not more recent' if first_date < second_date
10
+ end
11
+
12
+ Given /^I scope the request to "([^\"]*)"$/ do |scope|
13
+ NetRecorder.scope = scope
14
+ end
15
+
16
+ Given /^I wait (.+) seconds$/ do |count|
17
+ sleep count.to_i
18
+ end
@@ -1,3 +1,4 @@
1
+ # Helper module to tuck away some ugly file operations
1
2
  module Fake
2
3
  Fake::REQUEST = 0
3
4
  Fake::RESPONSE = 1
@@ -9,8 +9,11 @@ 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?
12
+
13
+ unless NetRecorder.recording?
14
+ yield response and return if block
15
+ return response
16
+ end
14
17
 
15
18
  scope = NetRecorder.scope || 'global'
16
19
  path = "http://#{req.bauth if req.bauth}#{req['host']}#{req.path}"
@@ -22,10 +25,11 @@ module NetRecorder
22
25
  end
23
26
 
24
27
  if existing_fake
25
- existing_fake[Fake::RESPONSE][scope][:response] << {:response => response} and return response
28
+ existing_fake[Fake::RESPONSE][scope][:response] << {:response => response}
29
+ else
30
+ @@fakes << [path, {scope => {:method => req.method, :response => [{:response => response}]}}]
26
31
  end
27
-
28
- @@fakes << [path, {scope => {:method => req.method, :response => [{:response => response}]}}]
32
+
29
33
  yield response if block
30
34
  response
31
35
  end
@@ -9,7 +9,6 @@ require "#{File.dirname(__FILE__)}/fake"
9
9
 
10
10
  # NetRecorder - the global namespace
11
11
  module NetRecorder
12
-
13
12
  # the path to the cache file
14
13
  def self.cache_file
15
14
  @@config.cache_file
@@ -64,11 +63,12 @@ private
64
63
 
65
64
  # load the cache and register all of the urls with fakeweb
66
65
  def self.fakeweb(scope='global')
66
+ FakeWeb.clean_registry
67
67
  fakes.each do |fake|
68
+ next unless fake[Fake::RESPONSE][scope]
68
69
  method = fake[Fake::RESPONSE][scope][:method].downcase.to_sym
69
70
  response = fake[Fake::RESPONSE][scope][:response]
70
71
  request = fake[Fake::REQUEST]
71
-
72
72
  FakeWeb.register_uri(method, request, response)
73
73
  end
74
74
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{netrecorder}
5
- s.version = "0.2.2"
5
+ s.version = "0.2.3"
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"]
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
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/fake.rb", "lib/http.rb", "lib/http_header.rb", "lib/netrecorder.rb"]
13
- s.files = ["Manifest", "README.rdoc", "Rakefile", "features/manage_cache.feature", "features/step_definitions/manage_cache_steps.rb", "features/support/env.rb", "lib/config.rb", "lib/fake.rb", "lib/http.rb", "lib/http_header.rb", "lib/netrecorder.rb", "netrecorder.gemspec"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "features/manage_cache.feature", "features/manage_scoping.feature", "features/step_definitions/manage_cache_steps.rb", "features/step_definitions/manage_scoping_steps.rb", "features/support/env.rb", "lib/config.rb", "lib/fake.rb", "lib/http.rb", "lib/http_header.rb", "lib/netrecorder.rb", "netrecorder.gemspec"]
14
14
  s.homepage = %q{http://github.com/chrisyoung/netrecorder}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Netrecorder", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
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.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Young
@@ -70,7 +70,9 @@ files:
70
70
  - README.rdoc
71
71
  - Rakefile
72
72
  - features/manage_cache.feature
73
+ - features/manage_scoping.feature
73
74
  - features/step_definitions/manage_cache_steps.rb
75
+ - features/step_definitions/manage_scoping_steps.rb
74
76
  - features/support/env.rb
75
77
  - lib/config.rb
76
78
  - lib/fake.rb