From 99c3c9ae73c9d1fec6091d21e2fb9ad5f1f0394a Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Fri, 4 May 2012 16:00:21 -0400 Subject: [PATCH] Don't try to read empty responses (fixes issues on jruby) --- lib/capybara/driver/webkit/browser.rb | 10 +++++++--- spec/browser_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) 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