diff --git a/lib/capybara/driver/webkit/browser.rb b/lib/capybara/driver/webkit/browser.rb index 808ddeb..36796ae 100644 --- a/lib/capybara/driver/webkit/browser.rb +++ b/lib/capybara/driver/webkit/browser.rb @@ -143,9 +143,13 @@ class Capybara::Driver::Webkit def read_response response_length = @connection.gets.to_i - response = @connection.read(response_length) - response.force_encoding("UTF-8") if response.respond_to?(:force_encoding) - response + if response_length > 0 + response = @connection.read(response_length) + response.force_encoding("UTF-8") if response.respond_to?(:force_encoding) + response + else + "" + end end def default_proxy_options diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index df9e6a7..dfc81ee 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -158,4 +158,16 @@ describe Capybara::Driver::Webkit::Browser do @proxy_requests.size.should == 0 end end + + it "doesn't try to read an empty response" do + connection = stub("connection") + connection.stub(:puts) + connection.stub(:print) + connection.stub(:gets).and_return("ok\n", "0\n") + connection.stub(:read).and_raise(StandardError.new("tried to read empty response")) + + browser = Capybara::Driver::Webkit::Browser.new(connection) + + expect { browser.visit("/") }.not_to raise_error(/empty response/) + end end