Browse files

Responders can store verify lambdas that are called before returning …

…the response (useful for testing).

Because the lambda is yielded with the request object, you can leverage the testing framework to do advanced checks like:

  FakeWeb::register_uri(:get, 'http://example.com/accounts/', :string => "fake response",
    :verify => lambda { |req|
      assert_equal "CUSTOM_VALUE", req['Authorization']
    }
  )
  • Loading branch information...
1 parent 708aefe commit 0390b4240c50b69e2ab9749504ffb58ed9068f5b @mislav committed Jan 9, 2009
Showing with 42 additions and 23 deletions.
  1. +8 −8 lib/fake_web.rb
  2. +4 −2 lib/fake_web/ext/net_http.rb
  3. +13 −13 lib/fake_web/registry.rb
  4. +4 −0 lib/fake_web/responder.rb
  5. +13 −0 test/test_fake_web.rb
View
16 lib/fake_web.rb
@@ -106,7 +106,9 @@ class NetConnectNotAllowedError < StandardError; end;
# FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
#
def self.register_uri(*args)
- Registry.instance.register_uri(*extract_arguments(args))
+ options = args.last.is_a?(Hash) || args.last.is_a?(Array) ? args.pop : {}
+ method, uri = extract_arguments(args)
+ Registry.instance.register_uri(method, uri, options)
end
# call-seq:
@@ -134,19 +136,17 @@ def self.registered_uri?(*args)
private
def self.extract_arguments(args)
- args.push({}) unless args.last.is_a?(Hash) or args.last.is_a?(Array)
-
case args.length
- when 2
- uri, options = *args
+ when 1
+ uri = args.first
method = :any
- when 3
- method, uri, options = *args
+ when 2
+ method, uri = *args
else
raise ArgumentError, "wrong number of arguments (expected the URI at least)"
end
- [method, uri, options]
+ [method, uri]
end
end
View
6 lib/fake_web/ext/net_http.rb
@@ -37,9 +37,11 @@ def request(request, body = nil, &block)
uri = "#{protocol}://#{self.address}:#{self.port}#{path}"
method = request.method.downcase.to_sym
- if FakeWeb.registered_uri?(method, uri)
+ if registered = FakeWeb::Registry.instance.registered_uri(method, uri)
+ responder = FakeWeb::Registry.instance.send(:pick_responder, registered)
+ responder.verify(request)
@socket = Net::HTTP.socket_type.new
- FakeWeb.response_for(method, uri, &block)
+ responder.response(&block)
elsif FakeWeb.allow_net_connect?
original_net_http_connect
original_net_http_request(request, body, &block)
View
26 lib/fake_web/registry.rb
@@ -38,19 +38,9 @@ def registered_uri(method, uri)
end
def response_for(method, uri, &block)
- responses = registered_uri(method, uri)
- return nil if responses.nil?
-
- next_response = responses.last
- responses.each do |response|
- if response.times and response.times > 0
- response.times -= 1
- next_response = response
- break
- end
- end
-
- next_response.response(&block)
+ responders = registered_uri(method, uri)
+ return nil if responders.nil?
+ pick_responder(responders).response(&block)
end
private
@@ -73,6 +63,16 @@ def sort_query_params(query)
query.split('&').sort.join('&')
end
end
+
+ def pick_responder(responders)
+ if next_responder = responders.find { |responder| responder.times > 0 }
+ next_responder.times -= 1
+ else
+ next_responder = responders.last
+ end
+
+ next_responder
+ end
end
end
View
4 lib/fake_web/responder.rb
@@ -28,6 +28,10 @@ def response(&block)
response
end
+
+ def verify(request)
+ options[:verify].call(request) if options[:verify]
+ end
private
View
13 test/test_fake_web.rb
@@ -437,4 +437,17 @@ def test_mock_rotate_responses
3.times { assert_equal 'thrice', Net::HTTP.get(uri) }
4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }
end
+
+ def test_response_hit_calls_verify
+ verify_called = false
+ FakeWeb.register_uri('http://mock/verify_called',
+ :verify => lambda { |req|
+ assert_kind_of Net::HTTPRequest, req
+ verify_called = true
+ }
+ )
+ assert !verify_called
+ Net::HTTP.start('mock') { |http| response = http.get('/verify_called') }
+ assert verify_called
+ end
end

0 comments on commit 0390b42

Please sign in to comment.