From a90e31d17ca08ddedbf48b0fc36cb9f2a11da757 Mon Sep 17 00:00:00 2001 From: Christopher Meiklejohn Date: Tue, 12 Jul 2011 15:31:10 -0400 Subject: [PATCH] Don't strip unless the result if non-nil; make the error message for this case specific. --- lib/capybara/driver/webkit.rb | 5 ++- lib/capybara/driver/webkit/browser.rb | 11 +++++-- spec/driver_spec.rb | 47 +++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/lib/capybara/driver/webkit.rb b/lib/capybara/driver/webkit.rb index 5eccade3..1405d653 100644 --- a/lib/capybara/driver/webkit.rb +++ b/lib/capybara/driver/webkit.rb @@ -3,7 +3,10 @@ require "capybara/driver/webkit/browser" class Capybara::Driver::Webkit - class WebkitError < StandardError + class WebkitInvalidResponseError < StandardError + end + + class WebkitNoResponseError < StandardError end attr_reader :browser diff --git a/lib/capybara/driver/webkit/browser.rb b/lib/capybara/driver/webkit/browser.rb index c616c7ac..f1d0974d 100644 --- a/lib/capybara/driver/webkit/browser.rb +++ b/lib/capybara/driver/webkit/browser.rb @@ -110,11 +110,16 @@ def attempt_connect end def check - result = @socket.gets.strip + result = @socket.gets + result.strip! if result - unless result == 'ok' - raise WebkitError, read_response + if result.nil? + raise WebkitNoResponseError, "No response received from the server." + elsif result != 'ok' + raise WebkitInvalidResponseError, read_response end + + result end def read_response diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 0adc73af..d7bce13a 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -20,7 +20,7 @@ p_id = "greeting" msg = "hello" iframe = "" - end + end body = <<-HTML @@ -56,12 +56,12 @@ it "raises error for missing frame by index" do expect { subject.within_frame(1) { } }. - to raise_error(Capybara::Driver::Webkit::WebkitError) + to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError) end it "raise_error for missing frame by id" do expect { subject.within_frame("foo") { } }. - to raise_error(Capybara::Driver::Webkit::WebkitError) + to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError) end it "returns an attribute's value" do @@ -142,7 +142,7 @@ it "raises an error for an invalid xpath query" do expect { subject.find("totally invalid salad") }. - to raise_error(Capybara::Driver::Webkit::WebkitError, /xpath/i) + to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError, /xpath/i) end it "returns an attribute's value" do @@ -237,7 +237,7 @@ it "raises an error for failing Javascript" do expect { subject.execute_script(%) }. - to raise_error(Capybara::Driver::Webkit::WebkitError) + to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError) end it "doesn't raise an error for Javascript that doesn't return anything" do @@ -626,7 +626,7 @@ wait_for_error_to_complete subject.find("//body") }. - to raise_error(Capybara::Driver::Webkit::WebkitError, %r{/error}) + to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError, %r{/error}) end def wait_for_error_to_complete @@ -657,7 +657,7 @@ def wait_for_error_to_complete it "raises a webkit error and then continues" do subject.find("//input").first.click - expect { subject.find("//p") }.to raise_error(Capybara::Driver::Webkit::WebkitError) + expect { subject.find("//p") }.to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError) subject.visit("/") subject.find("//p").first.text.should == "hello" end @@ -740,4 +740,37 @@ def wait_for_error_to_complete subject.find('id("accept")').first.text.should_not == 'text/html' end end + + context "no response app" do + before(:all) do + @app = lambda do |env| + body = <<-HTML + +
+ + HTML + [200, + { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, + [body]] + end + end + + it "raises a webkit error for the requested url" do + expect { + subject.find("//input").first.click + make_the_server_go_away + subject.find("//body") + }. + to raise_error(Capybara::Driver::Webkit::WebkitNoResponseError, %r{response}) + make_the_server_come_back + end + + def make_the_server_come_back + subject.browser.instance_variable_get(:@socket).unstub!(:gets) + end + + def make_the_server_go_away + subject.browser.instance_variable_get(:@socket).stub!(:gets).and_return(nil) + end + end end