Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: chrisk/fakeweb
...
head fork: chubas/fakeweb
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
15 lib/fake_web/registry.rb
@@ -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,
@@ -100,4 +107,4 @@ def sort_query_params(query)
end
end
-end
+end
View
20 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)
@@ -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]
@@ -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 }
View
39 test/test_yield_matches_in_regexes.rb
@@ -0,0 +1,39 @@
+require File.join(File.dirname(__FILE__), "test_helper")
+
+class TestYieldingMatches < Test::Unit::TestCase
+
+ def test_fakeweb_accepting_procs_as_body
+ assert_nothing_raised do
+ FakeWeb.register_uri(:get, %r|http://example\.com/test_example/(\d+)|, :body => Proc.new{|a| a})
+ response = Net::HTTP.start('example.com') { |query| query.get('/test_example/42') }
+ end
+ end
+
+ def test_raise_error_when_sending_procs_and_not_regexes
+ exception = assert_raise ArgumentError do # FIXME: Which exception should be thrown?
+ FakeWeb.register_uri(:get, "http://example.com/test_example/", :body => Proc.new{|a| a})
+ end
+ end
+
+ def test_yielded_matches_are_capture_groups
+ expected_args = nil
+ proc = Proc.new do |*yielded_args|
+ expected_args = yielded_args
+ yielded_args.join(', ')
+ end
+ FakeWeb.register_uri(:get, %r|http://example\.com/([^/]*)/(?:[^/]*)/([^/]*)|, :body => proc)
+ response = Net::HTTP.start('example.com') { |query| query.get('/foo/bar/baz') }
+ assert_equal 'foo, baz', response.body
+ assert_equal ['foo', 'baz'], expected_args
+ end
+
+ def test_not_interferes_with_normal_response_bodies
+ FakeWeb.register_uri(:get, %r|http://example.com/test_example/(\d+)|, :body => Proc.new{|a| a })
+ FakeWeb.register_uri(:get, "http://example.com/test_example/string_body", :body => "String")
+ FakeWeb.register_uri(:get, %r|http://example.com/test_example/regexp_body|, :body => "Regexp")
+ assert_equal '42', Net::HTTP.start('example.com') { |query| query.get('/test_example/42') }.body
+ assert_equal 'String', Net::HTTP.start('example.com') { |query| query.get('/test_example/string_body') }.body
+ assert_equal 'Regexp', Net::HTTP.start('example.com') { |query| query.get('/test_example/regexp_body') }.body
+ end
+
+end

No commit comments for this range

Something went wrong with that request. Please try again.