Skip to content

Commit

Permalink
Implemented :body accepting Proc object that yields the match objects…
Browse files Browse the repository at this point in the history
… when passing a regexp as :uri
  • Loading branch information
chubas committed Jul 16, 2009
1 parent d20498d commit 78228c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
15 changes: 11 additions & 4 deletions lib/fake_web/registry.rb
Expand Up @@ -66,9 +66,16 @@ def uri_map_matches(method, uri)
uri = normalize_uri(uri.to_s).to_s
uri = Utility.strip_default_port_from_uri(uri)

matches = uri_map.select { |registered_uri, method_hash|
registered_uri.is_a?(Regexp) && uri.match(registered_uri) && method_hash.has_key?(method)
}
matches = uri_map.select do |registered_uri, method_hash|
if registered_uri.is_a?(Regexp)
match = uri.match(registered_uri)
if match and method_hash.has_key?(method)
responder = method_hash[method]
responder.each{|r| r.match = match }
true
end
end
end

if matches.size > 1
raise MultipleMatchingRegexpsError,
Expand Down Expand Up @@ -100,4 +107,4 @@ def sort_query_params(query)
end

end
end
end
20 changes: 12 additions & 8 deletions lib/fake_web/responder.rb
@@ -1,19 +1,22 @@
module FakeWeb
class Responder #:nodoc:

attr_accessor :method, :uri, :options, :times
attr_accessor :method, :uri, :options, :times, :match
KNOWN_OPTIONS = [:body, :exception, :response, :status].freeze

def initialize(method, uri, options, times)
self.method = method
self.uri = uri
self.options = options
self.times = times ? times : 1
self.times = times || 1

if options.has_key?(:file) || options.has_key?(:string)
print_file_string_options_deprecation_warning
options[:body] = options.delete(:file) || options.delete(:string)
end
if options[:body].is_a?(Proc) and not uri.is_a?(Regexp)
raise ArgumentError.new("Regexp required if passing Proc as response argument")
end
end

def response(&block)
Expand All @@ -39,15 +42,16 @@ def response(&block)
private

def headers_extracted_from_options
options.reject {|name, _| KNOWN_OPTIONS.include?(name) }.map { |name, value|
options.reject {|name, _| KNOWN_OPTIONS.include?(name) }.map do |name, value|
[name.to_s.split("_").map { |segment| segment.capitalize }.join("-"), value]
}
end
end

def body
return '' unless options.has_key?(:body)

if !options[:body].include?("\0") && File.exists?(options[:body]) && !File.directory?(options[:body])
if options[:body].is_a?(Proc)
options[:body].call(*@match.captures)
elsif !options[:body].include?("\0") && File.exists?(options[:body]) && !File.directory?(options[:body])
File.read(options[:body])
else
options[:body]
Expand All @@ -62,9 +66,9 @@ def baked_response
r = Net::HTTPResponse.read_new(socket)

# Store the oiriginal transfer-encoding
saved_transfer_encoding = r.instance_eval {
saved_transfer_encoding = r.instance_eval do
@header['transfer-encoding'] if @header.key?('transfer-encoding')
}
end

# read the body of response.
r.instance_eval { @header['transfer-encoding'] = nil }
Expand Down

0 comments on commit 78228c7

Please sign in to comment.