Skip to content

Commit

Permalink
Refactor redirect handling as Merb response doesn't support the redir…
Browse files Browse the repository at this point in the history
…ect? method. All integration specs now passing again.
  • Loading branch information
joshknowles committed Dec 30, 2008
1 parent b222d3f commit e77495b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 90 deletions.
6 changes: 5 additions & 1 deletion lib/webrat/core/session.rb
Expand Up @@ -110,14 +110,18 @@ def request_page(url, http_method, data) #:nodoc:
@http_method = http_method
@data = data

request_page(response.location, :get, data) if response.redirect?
request_page(response.location, :get, data) if redirect?

return response
end

def success_code? #:nodoc:
(200..499).include?(response_code)
end

def redirect? #:nodoc:
response_code / 100 == 3
end

def exception_caught? #:nodoc:
response_body =~ /Exception caught/
Expand Down
8 changes: 1 addition & 7 deletions spec/fakes/test_session.rb
Expand Up @@ -12,7 +12,7 @@ def doc_root
end

def response
@response ||= TestResponse.new
@response ||= Object.new
end

def response_code
Expand All @@ -31,10 +31,4 @@ def put(url, data, headers = nil)
def delete(url, data, headers = nil)
end
end

class TestResponse #:nodoc:
def redirect?
false
end
end
end
18 changes: 17 additions & 1 deletion spec/private/core/session_spec.rb
Expand Up @@ -114,12 +114,28 @@ def session.response_body
end

it "should follow redirects" do
webrat_session.response.should_receive(:redirect?).twice.and_return(true, false)
webrat_session.should_receive(:redirect?).twice.and_return(true, false)
webrat_session.response.should_receive(:location).once.and_return("/newurl")

webrat_session.request_page("/oldurl", :get, {})

webrat_session.current_url.should == "/newurl"
end
end

describe "#redirect?" do
before(:each) do
webrat_session = Webrat::Session.new
end

it "should return true if the last response was a redirect" do
webrat_session.stub!(:response_code => 301)
webrat_session.redirect?.should be_true
end

it "should return false if the last response wasn't a redirect" do
webrat_session.stub!(:response_code => 200)
webrat_session.redirect?.should be_false
end
end
end
13 changes: 7 additions & 6 deletions spec/public/click_area_spec.rb
Expand Up @@ -24,7 +24,7 @@
webrat_session.response_code = 501
lambda { click_area "Berlin" }.should raise_error(Webrat::PageLoadError)
end

[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
Expand All @@ -34,11 +34,12 @@
</map>
</html>
HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status
lambda { click_area "Berlin" }.should_not raise_error
end
end

it "should fail if the area doesn't exist" do
with_html <<-HTML
<html>
Expand All @@ -47,12 +48,12 @@
</map>
</html>
HTML

lambda {
click_area "Missing area"
}.should raise_error(Webrat::NotFoundError)
end

it "should not be case sensitive" do
with_html <<-HTML
<html>
Expand All @@ -64,7 +65,7 @@
webrat_session.should_receive(:get).with("/page", {})
click_area "berlin"
end


it "should follow relative links" do
webrat_session.stub!(:current_url => "/page")
Expand All @@ -78,7 +79,7 @@
webrat_session.should_receive(:get).with("/page/sub", {})
click_area "Berlin"
end

it "should follow fully qualified local links" do
with_html <<-HTML
<html>
Expand Down
61 changes: 31 additions & 30 deletions spec/public/click_button_spec.rb
Expand Up @@ -7,10 +7,10 @@
<form method="get" action="/login"></form>
</html>
HTML

lambda { click_button }.should raise_error(Webrat::NotFoundError)
end

it "should fail if input is not a submit button" do
with_html <<-HTML
<html>
Expand All @@ -23,7 +23,7 @@
lambda { click_button }.should raise_error(Webrat::NotFoundError)
end


it "should fail if button is disabled" do
with_html <<-HTML
<html>
Expand All @@ -35,7 +35,7 @@

lambda { click_button }.should raise_error(Webrat::DisabledFieldError)
end

it "should default to get method" do
with_html <<-HTML
<html>
Expand All @@ -47,7 +47,7 @@
webrat_session.should_receive(:get)
click_button
end

it "should assert valid response" do
with_html <<-HTML
<html>
Expand All @@ -59,7 +59,7 @@
webrat_session.response_code = 501
lambda { click_button }.should raise_error(Webrat::PageLoadError)
end

[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
Expand All @@ -69,11 +69,12 @@
</form>
</html>
HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status
lambda { click_button }.should_not raise_error
end
end

it "should submit the first form by default" do
with_html <<-HTML
<html>
Expand All @@ -88,7 +89,7 @@
webrat_session.should_receive(:get).with("/form1", {})
click_button
end

it "should not explode on file fields" do
with_html <<-HTML
<html>
Expand All @@ -100,7 +101,7 @@
HTML
click_button
end

it "should submit the form with the specified button" do
with_html <<-HTML
<html>
Expand All @@ -115,7 +116,7 @@
webrat_session.should_receive(:get).with("/form2", {})
click_button "Form2"
end

it "should use action from form" do
with_html <<-HTML
<html>
Expand All @@ -127,7 +128,7 @@
webrat_session.should_receive(:get).with("/login", {})
click_button
end

it "should use method from form" do
with_html <<-HTML
<html>
Expand All @@ -139,7 +140,7 @@
webrat_session.should_receive(:post)
click_button
end

it "should send button as param if it has a name" do
with_html <<-HTML
<html>
Expand All @@ -152,7 +153,7 @@
webrat_session.should_receive(:post).with("/login", "login" => "Login")
click_button("Login")
end

it "should not send button as param if it has no name" do
with_html <<-HTML
<html>
Expand All @@ -177,8 +178,8 @@
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"password" => "mypass"})
click_button
end
end

it "should send default hidden field values" do
with_html <<-HTML
<html>
Expand All @@ -191,7 +192,7 @@
webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
click_button
end

it "should send default text field values" do
with_html <<-HTML
<html>
Expand All @@ -204,7 +205,7 @@
webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
click_button
end

it "should not send disabled field values" do
with_html <<-HTML
<html>
Expand All @@ -221,7 +222,7 @@
webrat_session.should_receive(:get).with("/login", {})
click_button
end

it "should send default checked fields" do
with_html <<-HTML
<html>
Expand All @@ -234,7 +235,7 @@
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
click_button
end

it "should send default radio options" do
with_html <<-HTML
<html>
Expand All @@ -250,7 +251,7 @@
webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "F"})
click_button
end

it "should send correct data for rails style unchecked fields" do
with_html <<-HTML
<html>
Expand All @@ -264,7 +265,7 @@
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
click_button
end

it "should send correct data for rails style checked fields" do
with_html <<-HTML
<html>
Expand Down Expand Up @@ -302,7 +303,7 @@
"response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]})
click_button
end

it "should not send default unchecked fields" do
with_html <<-HTML
<html>
Expand All @@ -315,7 +316,7 @@
webrat_session.should_receive(:get).with("/login", {})
click_button
end

it "should send default textarea values" do
with_html <<-HTML
<html>
Expand All @@ -328,7 +329,7 @@
webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"})
click_button
end

it "should properly handle HTML entities in textarea default values" do
spec = lambda do
with_html <<-HTML
Expand All @@ -342,14 +343,14 @@
webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Peanut butter & jelly"})
click_button
end

if Webrat.on_java?
spec.call
else
pending("needs bug fix", &spec)
end
end

it "should send default selected option value from select" do
with_html <<-HTML
<html>
Expand Down Expand Up @@ -381,7 +382,7 @@
webrat_session.should_receive(:get).with("/login", "month" => "February")
click_button
end

it "should send first select option value when no option selected" do
with_html <<-HTML
<html>
Expand All @@ -397,7 +398,7 @@
webrat_session.should_receive(:get).with("/login", "month" => "1")
click_button
end

it "should handle nested properties" do
with_html <<-HTML
<html>
Expand Down Expand Up @@ -449,7 +450,7 @@
webrat_session.should_receive(:get)
click_button
end

it "should find buttons by their IDs" do
with_html <<-HTML
<html>
Expand All @@ -461,7 +462,7 @@
webrat_session.should_receive(:get)
click_button "my_button"
end

it "should find image buttons by their alt text" do
with_html <<-HTML
<html>
Expand Down

0 comments on commit e77495b

Please sign in to comment.