Refactor redirect handling as Merb response doesn't support the redirect? method. All integration specs now passing again.

This commit is contained in:
Josh Knowles 2008-12-29 22:45:55 -05:00
parent b222d3fde3
commit e77495bc04
7 changed files with 108 additions and 90 deletions

View File

@ -110,7 +110,7 @@ For example:
@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
@ -118,6 +118,10 @@ For example:
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/

View File

@ -12,7 +12,7 @@ module Webrat #:nodoc:
end
def response
@response ||= TestResponse.new
@response ||= Object.new
end
def response_code
@ -31,10 +31,4 @@ module Webrat #:nodoc:
def delete(url, data, headers = nil)
end
end
class TestResponse #:nodoc:
def redirect?
false
end
end
end

View File

@ -114,7 +114,7 @@ describe Webrat::Session do
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, {})
@ -122,4 +122,20 @@ describe Webrat::Session do
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

View File

@ -24,7 +24,7 @@ describe "click_area" do
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
@ -34,11 +34,12 @@ describe "click_area" do
</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>
@ -47,12 +48,12 @@ describe "click_area" do
</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>
@ -64,7 +65,7 @@ describe "click_area" do
webrat_session.should_receive(:get).with("/page", {})
click_area "berlin"
end
it "should follow relative links" do
webrat_session.stub!(:current_url => "/page")
@ -78,7 +79,7 @@ describe "click_area" do
webrat_session.should_receive(:get).with("/page/sub", {})
click_area "Berlin"
end
it "should follow fully qualified local links" do
with_html <<-HTML
<html>

View File

@ -7,10 +7,10 @@ describe "click_button" do
<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>
@ -23,7 +23,7 @@ describe "click_button" do
lambda { click_button }.should raise_error(Webrat::NotFoundError)
end
it "should fail if button is disabled" do
with_html <<-HTML
<html>
@ -35,7 +35,7 @@ describe "click_button" do
lambda { click_button }.should raise_error(Webrat::DisabledFieldError)
end
it "should default to get method" do
with_html <<-HTML
<html>
@ -47,7 +47,7 @@ describe "click_button" do
webrat_session.should_receive(:get)
click_button
end
it "should assert valid response" do
with_html <<-HTML
<html>
@ -59,7 +59,7 @@ describe "click_button" do
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
@ -69,11 +69,12 @@ describe "click_button" do
</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>
@ -88,7 +89,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/form1", {})
click_button
end
it "should not explode on file fields" do
with_html <<-HTML
<html>
@ -100,7 +101,7 @@ describe "click_button" do
HTML
click_button
end
it "should submit the form with the specified button" do
with_html <<-HTML
<html>
@ -115,7 +116,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/form2", {})
click_button "Form2"
end
it "should use action from form" do
with_html <<-HTML
<html>
@ -127,7 +128,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/login", {})
click_button
end
it "should use method from form" do
with_html <<-HTML
<html>
@ -139,7 +140,7 @@ describe "click_button" do
webrat_session.should_receive(:post)
click_button
end
it "should send button as param if it has a name" do
with_html <<-HTML
<html>
@ -152,7 +153,7 @@ describe "click_button" do
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>
@ -177,8 +178,8 @@ describe "click_button" do
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>
@ -191,7 +192,7 @@ describe "click_button" do
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>
@ -204,7 +205,7 @@ describe "click_button" do
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>
@ -221,7 +222,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/login", {})
click_button
end
it "should send default checked fields" do
with_html <<-HTML
<html>
@ -234,7 +235,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
click_button
end
it "should send default radio options" do
with_html <<-HTML
<html>
@ -250,7 +251,7 @@ describe "click_button" do
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>
@ -264,7 +265,7 @@ describe "click_button" do
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>
@ -302,7 +303,7 @@ describe "click_button" do
"response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]})
click_button
end
it "should not send default unchecked fields" do
with_html <<-HTML
<html>
@ -315,7 +316,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/login", {})
click_button
end
it "should send default textarea values" do
with_html <<-HTML
<html>
@ -328,7 +329,7 @@ describe "click_button" do
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
@ -342,14 +343,14 @@ describe "click_button" do
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>
@ -381,7 +382,7 @@ describe "click_button" do
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>
@ -397,7 +398,7 @@ describe "click_button" do
webrat_session.should_receive(:get).with("/login", "month" => "1")
click_button
end
it "should handle nested properties" do
with_html <<-HTML
<html>
@ -449,7 +450,7 @@ describe "click_button" do
webrat_session.should_receive(:get)
click_button
end
it "should find buttons by their IDs" do
with_html <<-HTML
<html>
@ -461,7 +462,7 @@ describe "click_button" do
webrat_session.should_receive(:get)
click_button "my_button"
end
it "should find image buttons by their alt text" do
with_html <<-HTML
<html>

View File

@ -10,7 +10,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "Save & go back"
end
it "should use get by default" do
with_html <<-HTML
<html>
@ -30,7 +30,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text", :method => :get
end
it "should click link on substring" do
with_html <<-HTML
<html>
@ -40,7 +40,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "ink tex", :method => :get
end
it "should click delete links" do
with_html <<-HTML
<html>
@ -50,8 +50,8 @@ describe "click_link" do
webrat_session.should_receive(:delete).with("/page", {})
click_link "Link text", :method => :delete
end
it "should click post links" do
with_html <<-HTML
<html>
@ -61,7 +61,7 @@ describe "click_link" do
webrat_session.should_receive(:post).with("/page", {})
click_link "Link text", :method => :post
end
it "should click put links" do
with_html <<-HTML
<html>
@ -71,7 +71,7 @@ describe "click_link" do
webrat_session.should_receive(:put).with("/page", {})
click_link "Link text", :method => :put
end
it "should click links by regexp" do
with_html <<-HTML
<html>
@ -81,8 +81,8 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link /link [a-z]/i
end
it "should click links by id" do
it "should click links by id" do
with_html <<-HTML
<html>
<a id="link_text_link" href="/page">Link text</a>
@ -91,8 +91,8 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "link_text_link"
end
it "should click links by id regexp" do
it "should click links by id regexp" do
with_html <<-HTML
<html>
<a id="link_text_link" href="/page">Link text</a>
@ -101,7 +101,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link /_text_/
end
it "should click rails javascript links with authenticity tokens" do
with_html <<-HTML
<html>
@ -122,7 +122,7 @@ describe "click_link" do
webrat_session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
click_link "Posts"
end
it "should click rails javascript delete links" do
with_html <<-HTML
<html>
@ -143,7 +143,7 @@ describe "click_link" do
webrat_session.should_receive(:delete).with("/posts/1", {})
click_link "Delete"
end
it "should click rails javascript post links" do
with_html <<-HTML
<html>
@ -159,7 +159,7 @@ describe "click_link" do
webrat_session.should_receive(:post).with("/posts", {})
click_link "Posts"
end
it "should click rails javascript post links without javascript" do
with_html <<-HTML
<html>
@ -175,7 +175,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/posts", {})
click_link "Posts", :javascript => false
end
it "should click rails javascript put links" do
with_html <<-HTML
<html>
@ -196,7 +196,7 @@ describe "click_link" do
webrat_session.should_receive(:put).with("/posts", {})
click_link "Put"
end
it "should fail if the javascript link doesn't have a value for the _method input" do
with_html <<-HTML
<html>
@ -213,12 +213,12 @@ describe "click_link" do
return false;">Link</a>
</html>
HTML
lambda {
click_link "Link"
}.should raise_error(Webrat::WebratError)
end
it "should assert valid response" do
with_html <<-HTML
<html>
@ -228,7 +228,7 @@ describe "click_link" do
webrat_session.response_code = 501
lambda { click_link "Link text" }.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
@ -236,23 +236,24 @@ describe "click_link" do
<a href="/page">Link text</a>
</html>
HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status
lambda { click_link "Link text" }.should_not raise_error
end
end
it "should fail is the link doesn't exist" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
lambda {
click_link "Missing link"
}.should raise_error(Webrat::NotFoundError)
end
it "should not be case sensitive" do
with_html <<-HTML
<html>
@ -262,7 +263,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "LINK TEXT"
end
it "should match link substrings" do
with_html <<-HTML
<html>
@ -272,7 +273,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
end
it "should work with elements in the link" do
with_html <<-HTML
<html>
@ -282,7 +283,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
end
it "should match the first matching link" do
with_html <<-HTML
<html>
@ -293,7 +294,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page1", {})
click_link "Link text"
end
it "should choose the shortest link text match" do
with_html <<-HTML
<html>
@ -301,22 +302,22 @@ describe "click_link" do
<a href="/page2">Link</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
click_link "Link"
end
it "should treat non-breaking spaces as spaces" do
with_html <<-HTML
<html>
<a href="/page1">This&nbsp;is&nbsp;a&nbsp;link</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page1", {})
click_link "This is a link"
end
it "should not match on non-text contents" do
pending "needs fix" do
with_html <<-HTML
@ -325,12 +326,12 @@ describe "click_link" do
<a href="/page2">Location</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
click_link "Location"
end
end
it "should click link within a selector" do
with_html <<-HTML
<html>
@ -340,7 +341,7 @@ describe "click_link" do
</div>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
click_link_within "#container", "Link"
end
@ -366,7 +367,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page/sub", {})
click_link "Jump to sub page"
end
it "should follow fully qualified local links" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
@ -377,7 +378,7 @@ describe "click_link" do
webrat_session.should_receive(:get).with("http://subdomain.example.com/page/sub", {})
click_link "Jump to sub page"
end
it "should follow fully qualified local links to example.com" do
with_html <<-HTML
<html>
@ -398,28 +399,28 @@ describe "click_link" do
webrat_session.should_receive(:get).with("/page?foo=bar", {})
click_link "Jump to foo bar"
end
it "should matches_text? on regexp" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
@ -428,15 +429,15 @@ describe "click_link" do
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text not include &nbsp;" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
it "should not matches_text? on wrong text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U")
@ -464,5 +465,5 @@ describe "click_link" do
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
end

View File

@ -21,6 +21,7 @@ describe "visit" do
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status
lambda { visit("/") }.should_not raise_error
end
@ -31,7 +32,7 @@ describe "visit" do
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")
visit("/oldurl")