Permalink
Browse files

added functionality to register and retrieve the actual requests from…

… fakeweb
  • Loading branch information...
1 parent 85e4e67 commit fa460af047f039d269e4a64a9dc030d98af8b07c @cwyckoff committed Nov 24, 2009
Showing with 20 additions and 3 deletions.
  1. +1 −1 fakeweb.gemspec
  2. +8 −0 lib/fake_web.rb
  3. +1 −0 lib/fake_web/ext/net_http.rb
  4. +10 −2 lib/fake_web/registry.rb
View
@@ -4,7 +4,7 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{fakeweb}
+ s.name = %q{cwyckoff-fakeweb}
s.version = "1.2.6"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
View
@@ -145,6 +145,14 @@ def self.response_for(*args, &block) #:nodoc: :yields: response
end
end
+ def self.record_request(method, uri, request)
+ Registry.instance.register_request(method, uri, request)
+ end
+
+ def self.request_for(method, uri) #:nodoc: :yields: response
+ Registry.instance.request_for(method, uri)
+ end
+
# call-seq:
# FakeWeb.registered_uri?(method, uri)
#
@@ -52,6 +52,7 @@ def request(request, body = nil, &block)
if FakeWeb.registered_uri?(method, uri)
@socket = Net::HTTP.socket_type.new
+ FakeWeb.record_request(method, uri, body)
FakeWeb.response_for(method, uri, &block)
elsif FakeWeb.allow_net_connect?
connect_without_fakeweb
@@ -2,16 +2,21 @@ module FakeWeb
class Registry #:nodoc:
include Singleton
- attr_accessor :uri_map
+ attr_accessor :uri_map, :request_map
def initialize
clean_registry
end
def clean_registry
self.uri_map = Hash.new { |hash, key| hash[key] = {} }
+ self.request_map = Hash.new { |hash, key| hash[key] = {} }
end
+ def register_request(method, uri, request)
+ request_map[normalize_uri(uri)][method] = request
+ end
+
def register_uri(method, uri, options)
uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
FakeWeb::Responder.new(method, uri, option, option[:times])
@@ -22,6 +27,10 @@ def registered_uri?(method, uri)
!responses_for(method, uri).empty?
end
+ def request_for(method, uri)
+ request_map[normalize_uri(uri)][method] || (raise "No request for method #{method} and uri #{uri}")
+ end
+
def response_for(method, uri, &block)
responses = responses_for(method, uri)
return nil if responses.empty?
@@ -38,7 +47,6 @@ def response_for(method, uri, &block)
next_response.response(&block)
end
-
private
def responses_for(method, uri)

0 comments on commit fa460af

Please sign in to comment.