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 @http_method = http_method
@data = data @data = data
request_page(response.location, :get, data) if response.redirect? request_page(response.location, :get, data) if redirect?
return response return response
end end
@ -119,6 +119,10 @@ For example:
(200..499).include?(response_code) (200..499).include?(response_code)
end end
def redirect? #:nodoc:
response_code / 100 == 3
end
def exception_caught? #:nodoc: def exception_caught? #:nodoc:
response_body =~ /Exception caught/ response_body =~ /Exception caught/
end end

View File

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

View File

@ -114,7 +114,7 @@ describe Webrat::Session do
end end
it "should follow redirects" do 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.response.should_receive(:location).once.and_return("/newurl")
webrat_session.request_page("/oldurl", :get, {}) webrat_session.request_page("/oldurl", :get, {})
@ -122,4 +122,20 @@ describe Webrat::Session do
webrat_session.current_url.should == "/newurl" webrat_session.current_url.should == "/newurl"
end end
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 end

View File

@ -34,6 +34,7 @@ describe "click_area" do
</map> </map>
</html> </html>
HTML HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status webrat_session.response_code = status
lambda { click_area "Berlin" }.should_not raise_error lambda { click_area "Berlin" }.should_not raise_error
end end

View File

@ -69,6 +69,7 @@ describe "click_button" do
</form> </form>
</html> </html>
HTML HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status webrat_session.response_code = status
lambda { click_button }.should_not raise_error lambda { click_button }.should_not raise_error
end end

View File

@ -236,6 +236,7 @@ describe "click_link" do
<a href="/page">Link text</a> <a href="/page">Link text</a>
</html> </html>
HTML HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status webrat_session.response_code = status
lambda { click_link "Link text" }.should_not raise_error lambda { click_link "Link text" }.should_not raise_error
end end

View File

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