Skip to content

Commit

Permalink
Refactor redirect support out of RailsSession & SinatraSession and in…
Browse files Browse the repository at this point in the history
…to Session#request_page
  • Loading branch information
joshknowles committed Dec 30, 2008
1 parent a569738 commit ce364d1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 205 deletions.
64 changes: 33 additions & 31 deletions lib/webrat/core/session.rb
Expand Up @@ -8,7 +8,7 @@ module Webrat
# A page load or form submission returned an unsuccessful response code (500-599)
class PageLoadError < WebratError
end

def self.session_class
case Webrat.configuration.mode
when :rails
Expand Down Expand Up @@ -37,29 +37,29 @@ def self.session_class
STR
end
end

class Session
extend Forwardable
include Logging
include SaveAndOpenPage

attr_reader :current_url
attr_reader :elements

def initialize(context = nil) #:nodoc:
@http_method = :get
@data = {}
@default_headers = {}
@custom_headers = {}
@context = context

reset
end

def current_dom #:nodoc:
current_scope.dom
end

# For backwards compatibility -- removing in 1.0
def current_page #:nodoc:
page = OpenStruct.new
Expand All @@ -68,19 +68,19 @@ def current_page #:nodoc:
page.data = @data
page
end

def doc_root #:nodoc:
nil
end

def header(key, value)
@custom_headers[key] = value
end

def http_accept(mime_type)
header('Accept', Webrat::MIME.mime_type(mime_type))
end

def basic_auth(user, pass)
encoded_login = ["#{user}:#{pass}"].pack("m*")
header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
Expand All @@ -103,39 +103,41 @@ def request_page(url, http_method, data) #:nodoc:

save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?

reset

@current_url = url
@http_method = http_method
@data = data


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

return response
end

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

def exception_caught? #:nodoc:
response_body =~ /Exception caught/
end

def current_scope #:nodoc:
scopes.last || page_scope
end

# Reloads the last page requested. Note that this will resubmit forms
# and their data.
def reloads
request_page(@current_url, @http_method, @data)
end

webrat_deprecate :reload, :reloads


# Works like click_link, but only looks for the link text within a given selector
#
#
# Example:
# click_link_within "#user_12", "Vote"
def click_link_within(selector, link_text)
Expand All @@ -145,14 +147,14 @@ def click_link_within(selector, link_text)
end

webrat_deprecate :clicks_link_within, :click_link_within

def within(selector)
scopes.push(Scope.from_scope(self, current_scope, selector))
ret = yield(current_scope)
scopes.pop
return ret
end

# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
Expand All @@ -161,7 +163,7 @@ def within(selector)
def visit(url = nil, http_method = :get, data = {})
request_page(url, http_method, data)
end

webrat_deprecate :visits, :visit

# Subclasses can override this to show error messages without html
Expand All @@ -176,25 +178,25 @@ def scopes #:nodoc:
def page_scope #:nodoc:
@_page_scope ||= Scope.from_page(self, response, response_body)
end

def dom
page_scope.dom
end

def xml_content_type?
false
end

def simulate
return if Webrat.configuration.mode == :selenium
yield
end

def automate
return unless Webrat.configuration.mode == :selenium
yield
end

def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :set_hidden_field
def_delegators :current_scope, :submit_form
Expand All @@ -213,14 +215,14 @@ def automate
def_delegators :current_scope, :field_by_xpath
def_delegators :current_scope, :field_with_id
def_delegators :current_scope, :select_option

private

def reset
@elements = {}
@_scopes = nil
@_page_scope = nil
end

end
end
24 changes: 0 additions & 24 deletions lib/webrat/rails.rb
Expand Up @@ -50,10 +50,7 @@ def integration_session

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
Expand Down Expand Up @@ -82,27 +79,6 @@ def response #:nodoc:
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
Expand Down
1 change: 0 additions & 1 deletion lib/webrat/sinatra.rb
Expand Up @@ -11,7 +11,6 @@ class SinatraSession < RackSession #:nodoc:
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
Expand Down
32 changes: 19 additions & 13 deletions spec/fakes/test_session.rb
Expand Up @@ -2,33 +2,39 @@ module Webrat #:nodoc:
def self.session_class #:nodoc:
TestSession
end

class TestSession < Session #:nodoc:
attr_accessor :response_body
attr_writer :response_code

def doc_root
File.expand_path(File.join(".", "public"))
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, headers = nil)
end
def post(url, data)

def put(url, data, headers = nil)
end
def put(url, data)

def delete(url, data, headers = nil)
end

def delete(url, data)
end

class TestResponse #:nodoc:
def redirect?
false
end
end
end

0 comments on commit ce364d1

Please sign in to comment.