Refactor redirect support out of RailsSession & SinatraSession and into Session#request_page

This commit is contained in:
Josh Knowles 2008-12-29 21:19:13 -05:00
parent a569738542
commit ce364d1663
8 changed files with 89 additions and 205 deletions

View File

@ -110,6 +110,8 @@ For example:
@http_method = http_method
@data = data
request_page(response.location, :get, data) if response.redirect?
return response
end

View File

@ -50,10 +50,7 @@ module Webrat
def do_request(http_method, url, data, headers) #:nodoc:
update_protocol(url)
integration_session.send(http_method, normalize_url(url), data, headers)
integration_session.follow_redirect_with_headers(headers) while integration_session.internal_redirect?
integration_session.status
end
# remove protocol, host and anchor
@ -82,27 +79,6 @@ module Webrat
end
module ActionController #:nodoc:
module Integration #:nodoc:
class Session #:nodoc:
def internal_redirect?
redirect? && response.redirect_url_match?(host)
end
def follow_redirect_with_headers(h = {})
raise "Not a redirect! #{@status} #{@status_message}" unless redirect?
h = Hash.new if h.nil?
h['HTTP_REFERER'] = request.url
location = headers["location"]
location = location.first if location.is_a?(Array)
get(location, {}, h)
status
end
end
end
IntegrationTest.class_eval do
include Webrat::Methods
include Webrat::Matchers

View File

@ -11,7 +11,6 @@ module Webrat
path, data, headers = *args
params = data.merge({:env => headers || {}})
self.__send__("#{verb}_it", path, params)
get_it(@response.location, params) while @response.redirect?
end
end
end

View File

@ -12,23 +12,29 @@ module Webrat #:nodoc:
end
def response
@response ||= Object.new
@response ||= TestResponse.new
end
def response_code
@response_code || 200
end
def get(url, data)
def get(url, data, headers = nil)
end
def post(url, data)
def post(url, data, headers = nil)
end
def put(url, data)
def put(url, data, headers = nil)
end
def delete(url, data)
def delete(url, data, headers = nil)
end
end
class TestResponse #:nodoc:
def redirect?
false
end
end
end

View File

@ -112,5 +112,14 @@ describe Webrat::Session do
lambda { webrat_session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError)
end
it "should follow redirects" do
webrat_session.response.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
end

View File

@ -6,8 +6,6 @@ describe Webrat::RailsSession do
before :each do
Webrat.configuration.mode = :rails
@integration_session = mock("integration_session")
@integration_session.stub!(:internal_redirect?)
@integration_session.stub!(:status)
end
it "should delegate response_body to the session response body" do
@ -80,44 +78,6 @@ describe Webrat::RailsSession do
end
end
context "following redirects" do
it "should use forward headers when following redirects" do
@integration_session.stub!(:post)
@integration_session.stub!(:host)
@integration_session.stub!(:status)
@integration_session.should_receive(:internal_redirect?).twice.and_return(true, false)
@integration_session.should_receive(:follow_redirect_with_headers).with("headers")
rails_session = Webrat::RailsSession.new(@integration_session)
rails_session.post("url", "data", "headers")
end
it "should follow internal redirects" do
@integration_session.stub!(:get)
@integration_session.stub!(:host)
@integration_session.stub!(:status)
@integration_session.should_receive(:internal_redirect?).twice.and_return(true, false)
@integration_session.should_receive(:follow_redirect_with_headers)
rails_session = Webrat::RailsSession.new(@integration_session)
rails_session.get("url", "data", "headers")
end
it "should not follow external redirects" do
@integration_session.stub!(:get)
@integration_session.stub!(:host)
@integration_session.stub!(:status)
@integration_session.should_receive(:internal_redirect?).and_return(false)
@integration_session.should_not_receive(:follow_redirect_with_headers)
rails_session = Webrat::RailsSession.new(@integration_session)
rails_session.get("url", "data", "headers")
end
end
it "should provide a saved_page_dir" do
Webrat::RailsSession.new(mock("integration session")).should respond_to(:saved_page_dir)
end
@ -126,65 +86,3 @@ describe Webrat::RailsSession do
Webrat::RailsSession.new(mock("integration session")).should respond_to(:doc_root)
end
end
describe ActionController::Integration::Session do
before :each do
Webrat.configuration.mode = :rails
@integration_session = ActionController::Integration::Session.new
@integration_session.stub!(:request => mock("request", :url => "http://source.url/"))
@integration_session.stub!(:response => mock("response"))
end
describe "internal_redirect?" do
it "should return false if the response is not a redirect" do
@integration_session.should_receive(:redirect?).and_return(false)
@integration_session.internal_redirect?.should == false
end
it "should return false if the response was a redirect but the response location does not match the request host" do
@integration_session.should_receive(:redirect?).and_return(true)
@integration_session.response.should_receive(:redirect_url_match?).and_return(false)
@integration_session.internal_redirect?.should == false
end
it "should return true if the response is a redirect and the response location matches the request host" do
@integration_session.should_receive(:redirect?).and_return(true)
@integration_session.response.should_receive(:redirect_url_match?).and_return(true)
@integration_session.internal_redirect?.should == true
end
end
describe "follow_redirect_with_headers" do
before do
Webrat.configuration.mode = :rails
@integration_session.stub!(:headers).and_return({ 'location' => ["/"]})
@integration_session.stub!(:redirect?).and_return true
@integration_session.stub!(:get)
end
it "should raise an exception if response wasn't a redirect" do
@integration_session.stub!(:redirect?).and_return false
lambda { @integration_session.follow_redirect_with_headers }.should raise_error
end
it "should set the HTTP referer header" do
headers = {}
@integration_session.follow_redirect_with_headers(headers)
headers["HTTP_REFERER"].should == "http://source.url/"
end
it "should GET the first location header" do
@integration_session.stub!("headers").and_return({ 'location' => ['/target'] })
@integration_session.should_receive(:get).with("/target", {}, hash_including("headers" => "foo"))
@integration_session.follow_redirect_with_headers({"headers" => "foo"})
end
it "should return the status" do
@integration_session.stub!(:status).and_return "202"
@integration_session.follow_redirect_with_headers.should == "202"
end
end
end

View File

@ -4,11 +4,6 @@ describe Webrat::SinatraSession do
before :each do
Webrat.configuration.mode = :sinatra
@sinatra_session = Webrat::SinatraSession.new
@response = mock(:response)
@response.stub!(:redirect?)
@sinatra_session.instance_variable_set("@response", @response)
end
it "should delegate get to get_it" do
@ -30,14 +25,4 @@ describe Webrat::SinatraSession do
@sinatra_session.should_receive(:delete_it).with("url", { :env => "headers" })
@sinatra_session.delete("url", {}, "headers")
end
it "should forward headers when following redirects" do
@response.should_receive(:redirect?).twice.and_return(true, false)
@response.should_receive(:location).and_return("redirect url")
@sinatra_session.should_receive(:get_it).with("original url", { :env => "headers" })
@sinatra_session.should_receive(:get_it).with("redirect url", { :env => "headers" })
@sinatra_session.get("original url", {}, "headers")
end
end

View File

@ -29,6 +29,15 @@ describe "visit" do
it "should require a visit before manipulating page" do
lambda { fill_in "foo", :with => "blah" }.should raise_error(Webrat::WebratError)
end
it "should follow redirects" do
webrat_session.response.should_receive(:redirect?).twice.and_return(true, false)
webrat_session.response.should_receive(:location).once.and_return("/newurl")
visit("/oldurl")
current_url.should == "/newurl"
end
end
describe "visit with referer" do