Skip to content

Commit

Permalink
Start adding support for the on_body handler
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisk committed Nov 27, 2009
1 parent 192e262 commit 168423d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
23 changes: 22 additions & 1 deletion lib/fake_web/ext/curb_patch.rb
Expand Up @@ -34,7 +34,7 @@ def initialize(url = nil, &block)
def perform_with_fakeweb
if FakeWeb.registered_uri?(:get, url)
r = FakeWeb::Registry.instance.curl_response_for(:get, url)
self.body_str = r.body_str
__process_body(r.body_str)
true
elsif FakeWeb.allow_net_connect?
perform_without_fakeweb
Expand All @@ -46,6 +46,27 @@ def perform_with_fakeweb
alias_method :perform_without_fakeweb, :perform
alias_method :perform, :perform_with_fakeweb


# TODO: shouldn't really be adding new methods. Put this somewhere else?
def __process_body(body)
# TODO: lock around this so another thread doesn't see the momentarily-nil proc?
body_handler = self.on_body
self.on_body(&body_handler) unless body_handler.nil?

if body_handler.nil?
self.body_str = body
else
self.body_str = nil
handler_return_value = body_handler.call(body)
if !handler_return_value.is_a?(Integer)
FakeWeb::Utility.rb_warn "Curl data handlers should return the number of bytes read as an Integer", caller[1]
elsif handler_return_value != body.length
# NOTE: Curb docs claim this should be an AbortedByCallbackError, but it raises a WriteError
raise Curl::Err::WriteError, "Failed writing received data to disk/application"
end
end
end

end
end
end
6 changes: 6 additions & 0 deletions lib/fake_web/utility.rb
Expand Up @@ -28,6 +28,12 @@ def self.uri_escape(*args)
end
end

# Emulate rb_warn in pure ruby
def self.rb_warn(msg, caller_line = caller[0])
file_and_line = caller_line.sub(/:in .+$/, '')
warn "#{file_and_line}: warning: #{msg}"
end

def self.puts_warning_for_net_http_around_advice_libs_if_needed
libs = {"Samuel" => defined?(Samuel)}
warnings = libs.select { |_, loaded| loaded }.map do |name, _|
Expand Down
41 changes: 39 additions & 2 deletions test/test_curb.rb
Expand Up @@ -15,10 +15,10 @@ def test_curl_easy_class_perform_with_different_body
end

def test_curl_easy_instance_perform
FakeWeb.register_uri(:get, "http://example.com", :body => "example3")
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
curl = Curl::Easy.new("http://example.com")
assert_equal true, curl.perform
assert_equal "example3", curl.body_str
assert_equal "example", curl.body_str
end

def test_easy_class_perform_with_unregistered_uri_raises
Expand All @@ -34,5 +34,42 @@ def test_easy_instance_perform_with_unregistered_uri_raises
end
end

def test_body_handler_is_used
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
curl = Curl::Easy.new("http://example.com")
body = ""
curl.on_body { |data| body << data; data.length }
curl.perform
assert_equal "example", body
assert_nil curl.body_str
end

def test_body_handler_is_preserved
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
curl = Curl::Easy.new("http://example.com")
body = ""
curl.on_body { |data| body << data; data.length }
curl.perform
assert_equal "example", body
body = ""
curl.perform
assert_equal "example", body
end

def test_perform_raises_when_body_handler_returns_wrong_number
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
curl = Curl::Easy.new("http://example.com")
curl.on_body { |data| 0 }
exception = assert_raise(Curl::Err::WriteError) { curl.perform }
assert_equal "Failed writing received data to disk/application", exception.message
end

def test_perform_raises_when_body_handler_returns_non_number
FakeWeb.register_uri(:get, "http://example.com", :body => "example")
curl = Curl::Easy.new("http://example.com")
curl.on_body { |data| data }
warning, line = capture_stderr { curl.perform }, __LINE__
assert warning.include? "test_curb.rb:#{line}: warning: Curl data handlers should return the number of bytes read as an Integer\n"
end

end

0 comments on commit 168423d

Please sign in to comment.